ansible部署

ansible的特性:基于Python语言实现,由paramiko,PyYAML和jinjia2三个关键模块 部署简单,agentless
 默认使用ssh协议
        (1) 基于秘钥认证方式
        (2)在inventory文件中配置账号密码
主从模式:
        master:ansible,ssh client
        slave:ssh server

支持自定义模块:支持各种编程语言
支持Playbook
基于“模块”完成各种“任务”

首先,安装ansible,在管理端和被管理端配置秘钥信息;

/etc/ansible/hosts文件下,配置被管理端的组信息。

yum list all *ansible*
yum info ansible
yum install ansible -y
rpm -ql ansible

安装依赖于epel源
配置文件:/etc/ansible/ansible.cfg
invertory:/etc/ansible/hosts

ls /etc/ansible   
ansible.cfg hosts roles

ansible.cfg 是 Ansible 工具的配置文件;
hosts 用来配置被管理的机器;
roles 是一个目录,playbook 将使用它
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1、Ansible 管理机与被管理机做秘钥认证
ssh-keygen        # 生成秘钥
ssh-copy-id -i /root/.ssh/id_rsa.pub "-p 22 root@ip地址"     # 将公钥写入被管理机
ssh root@ip地址 'ntpdate 另一个ip地址'               #同步时间和另一个服务器时间相同。
2、hosts 文件添加被管理机
vim /etc/ansible/hosts

[Client]
192.168.163.2
192.168.163.3
[webservers]
192.168.163.2
[dbservers]
192.168.163.3

如何查看模块帮助;

man ansible-doc
ansible-doc -l             查看ansible支持的所有的模块。
ansible-doc -s MODULE_NAME 查看某些模块如何使用的。
ansible-doc -s yum        查看yum如何使用
ansible-doc -s command

ansible命令应用基础:
语法:ansible <host-pattern> [-f forks] [-m module_name] [-a args]
host-pattern:对哪些主机生效。
-f forks:一批处理多少主机,启动的并发线程数。
-m module_name:要使用的模块。
-a args:模块特有的参数。

一.常见模块:
ansible-doc -s MODULE_NAME

1) command:命令模块,默认模块,用于远程执行命令
ansible all -a 'data'
ansible 192.168.133.2 -m command -a 'data'    #出现success表示成功。
192.168.12.129 | SUCCESS |rc=0 >>
TUE JUN 23 16:56:41 CST 2018

ansible webservers -m command -a 'data'
ansible all -m command -a 'data'
ansible 192.168.133.2 -m command -a 'tail -2 /etc/passwd'

2)cron模块

cron:
    state:
        present:安装
        absent:移除

ansible-doc -s cron:查看如何使用参数,该参数-a后面添加(minute,job,name皆为参数的内容。)
比如在webservers集群中写个定时计划任务:每隔10分钟输出hello

ansible webservers -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"'        
ansible webservers -a 'crontab -l'  查看同步任务列表
ansible webservers -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state=absent'
移除同步任务。默认没有state是安装。

3)user用户模块
name=:指明创建用户的名字。
ansible-doc -s user
ansible all -m user -a 'name="user"'
ansible all -m user -a 'name="user" state=absent'

4)group模块
ansible-doc -s group
ansible webservers -m group -a 'name=mysql gid=306 system=yes'
ansible webservers -m user -a 'name=mysql uid=306 system=yes group=mysql'

5)copy文件模块
src=:定义本地源文件路径
dest=:定义远程目标文件路径
content:取代src=,表示直接用此处生成的信息生成为目标文件的内容。
ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible ower=root mode=640'
ansible all -m copy -a 'connect="Hello Ansible Hi Mageedu" dest=/tmp/test.ansible'  是转译。

6)file:设定文件属性
path=:指定文件路径,可以使用name或dest来替换

创建文件符号链接:
    src=:指明源文件
    path=:指明符号链接文件路径

ansible all -m file -a 'ower=mysql group=mysql mode=644 path=/tmp/fstab.ansible'
创建文件链接:
ansible all -m file -a 'path=/tmp/fstab.link src=/tmp/fastab.ansible state=link'

7)ping:测试主机是否能连接
ansible -a 'ping'

8)service:指定运行状态
enabled=:是否开机自动启动,取值为true或者false
name=:服务名称
state=:状态,取值为started,stopped,restarted

ansible-doc -s service
ansible webservers -a 'service httpd status'
ansible webservers -a 'chkconfig --list httpd'
ansible webservers -m service -a 'enabled=true name=httpd state=started'

9)shell:在远程主机上运行命令
尤其是用到管道等功能的复杂命令时候。
ansible all -m user -a 'name=user1'
ansible all -m command -a 'echo passwd |passwd --stdin user1'
在远程主机上查看发现密码没改变,是因为他默认可能认为是本机的命令
ansible all -m shell -a 'echo passwd |passwd --stdin user1'

10)script:本地脚本命令复制到远程并且运行之。该shell脚本只支持相对路径

cat test.sh
#!/bin/bash
echo "hello ansible" > /tmp/script.ansible
useradd user2
cp -r /tmp/test.sh ./ 该shell脚本只支持相对路径
vim /tmp/test.sh
chmod +x /tmp/test.sh
ansible all -m script -a 'test.sh'

11)yum:安装程序包
name=:指明要安装的程序包,可以带上版本号
state=:present,lastest表示安装,absent表示卸载
ansible all -m yum -a 'name=zsh'
ansible all -m yum -a 'name=zsh state=absent'

12)setup:收集远程主机的facts
每个被管理的节点在接受并运行管理命令之前,会将自己的主机相关信息,如操作系统,ip地址等报告给远程的ansible主机。
ansible 192.168.133.4 -m setup    
nginx配置文件中有个work_processes=物理核心数(processor_core * processor_count)-1或者-2

13)get_url 模块(实现在远程主机下载指定 URL 到本地,支持 sha256sum 文件校验)
ansible Client -m get_utl -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes"

14) stat 模块(获取远程文件状态信息,atime/ctime/mtime/md5/uid/gid 等信息)
ansible Client -m stat -a "path=/etc/syctl.conf"

15) mount 模块(远程主机分区挂载)
ansible Client -m mount -a "name=/mnt/data src=/dev/sd0 fstype=ext4 opts=ro state=present"

综合
ansible-doc -s 模块名

command
    -a 'COMMAND'
user
    -a 'name= state={present|absent}' system= uid= '
group
    -a 'name= gid= state= system='
cron
    -a 'name= minute= hour= day= mounth= weekday= job= user= state='
copy
    -a 'dest= src= mode= owner= group= '
file
    -a 'path= mode= owner= group= state={directory|link|present|absent} src='
ping
    无参数
yum
    -a 'name= state={present|lastest|absent}'
service
    -a 'name- state={started|stopped|restarted} enabled='
shell
    -a 'COMMAND'
script
    -a '/path/to/script'
setup

原文地址:https://www.cnblogs.com/fengzhongzhuzu/p/9129732.html