如何在Centos7安装配置Ansible自动运维工具

中文网站:https://ansible-tran.readthedocs.io/en/latest/index.html

1.管理主机要求
(1).Python 2.6 或 Python 2.7
(2).增加ulimit值,执行命令:
launchctl limit maxfiles 1024 2048

2.安装
#Centos安装Ansible需要先安装epel源,执行命令:
yum install epel-release

#安装ansible,执行命令:
yum install ansible

3.配置密匙
#在服务端生成公匙私匙对,直接回车即可,不用设置密钥密码,执行命令:
ssh-keygen -t rsa

#避免在建立SSH连接时,重复输入密码,执行命令:
ssh-agent bash
ssh-add /home/yunwei/.ssh/id_rsa

#然后将公钥(id_rsa.pub)拷贝到客户端机器上,执行命令:
ssh-copy-id -p8800 yunwei@192.168.1.100

#在服务端本机也要操作,执行命令:
cat /home/yunwei/.ssh/id_rsa.pub >> /home/yunwei/.ssh/authorized_keys
chmod 600 /home/yunwei/.ssh/authorized_keys

#在服务端机器测试ssh连接客户端是否可以登录,执行命令:
ssh -p8800 yunwei@192.168.1.100

4.配置文件
#ansible的主要配置文件
(1).ansible应用程序的主配置文件:/etc/ansible/ansible.cfg
(2).Host Inventory定义管控主机:/etc/ansible/hosts

#修改/etc/ansible/hosts文件,执行命令:
vi /etc/ansible/hosts
内容如下所示:
[testServer]
#指定远程IP,端口及用户
192.168.1.100 ansible_ssh_port=8800 ansible_ssh_user=yunwei
#也可用IP:PORT方法设置端口
192.168.1.101:8800 ansible_ssh_user=yunwei
#本机不需要走ssh协议,可以设定连接为本地模式;也不需要指定端口及用户
#127.0.0.1 ansible_ssh_port=8800 ansible_ssh_user=yunwei
127.0.0.1 ansible_connection=local

#测试ansible连通性,执行命令:
ansible all -m ping

#指定某个用户登录,执行命令:
ansible all -m ping -u yunwei

#指定某个用户登录某个主机组,如登录testServer主机组,执行命令:
ansible testServer -m ping -u yunwei

5.禁用公匙认证提示信息,执行命令
export ANSIBLE_HOST_KEY_CHECKING=False

#或修改配置/etc/ansible/ansible.cfg文件,找到host_key_checking属性,把前面的注释去掉,执行命令:
vi /etc/ansible/ansible.cfg
修改内容如下所示:
[defaults]
host_key_checking = False

6.批量复制公匙到远程服务器脚本
#安装expect工具包,执行命令:
yum -y install expect
#安装netcat监听端口工具包,执行命令
yum -y install nc

#单条记录脚本(2个参数)
用法: ./copy_single_rsa.sh 192.168.1.101 8800
脚本copy_single_rsa.sh内容如下所示:

#!/usr/bin/expect
set passwd “yunwei@baD\$ai5e”
set ip [lindex $argv 0]
set port [lindex $argv 1]

set timeout 30

spawn ssh-copy-id -p$port yunwei@$ip
expect {
“yes/no” { send “yes\r”;exp_continue }
“password:” { send “$passwd\r” }
}
expect eof

#批量IP输入脚本(2个参数)
用法:./copy_ips_rsa.sh 192 168 1 1 230 8800
灵活脚本copy_ips_rsa.sh内容如下所示

#!/usr/bin/expect -f
set passwd “yunwei@baD\$ai5e”
#ip4start–IP第4位起始,ip4end–IP第4位结束
set ip4start [lindex $argv 0 ]
set ip4end [lindex $argv 1 ]

set timeout 10
for {set y $ip4start} {$y < $ip4end} {incr y} {
spawn ssh-copy-id -p8800 yunwei@192.168.1.$y
expect {
“yes/no” { send “yes\r”;exp_continue }
“password:” { send “$passwd\r” }
}
}

#按范围灵活输入脚本(6个参数)
用法:./copyrsa.sh 192 168 10 1 230 8800
灵活脚本copyrsa.sh内容如下所示

#!/usr/bin/expect
set passwd “yunwei@baD\$ai5e”

#获取控制台输入参
#ip0–ip地址第1位,ip1–ip地址第2位,ip2–ip地址第3位
#ip4start–ip地址第4位范围起始,ip4end–ip地址第4位范围结束
#port–端口
set ip0 [lindex $argv 0]
set ip1 [lindex $argv 1]
set ip2 [lindex $argv 2]
set ip4start [lindex $argv 3]
set ip4end [lindex $argv 4]
set port [lindex $argv 5]

#设置控制台输入超时时间,单位为秒
set timeout 30

for {set y $ip4start} {$y < $ip4end} {incr y} {
spawn nc -v $ip0.$ip1.$ip2.$y $port
expect {
“Ncat: Connection timed out” {
send_user “跳过 $ip0.$ip1.$ip2.$y 原因: Connection timed out。\n”;
continue
}
“Ncat: Connection refused” {
send_user “跳过 $ip0.$ip1.$ip2.$y 原因: Connection refused。\n”;
continue
}
“Ncat: No route to host” {
send_user “跳过 $ip0.$ip1.$ip2.$y 原因: No route to host。\n”;
continue
}
“can’t be established” {
send_user “跳过 $ip0.$ip1.$ip2.$y 原因: can’t be established。\n”;
continue
}
“Ncat: Connected to” {
send_user “继续执行下面$ip0.$ip1.$ip2.$y”;
spawn ssh-copy-id -p$port yunwei@$ip0.$ip1.$ip2.$y
expect {
“yes/no” { send “yes\r”;exp_continue }
“password:” { send “$passwd\r” }
“already installed” {
send_user “跳过 $ip0.$ip1.$ip2.$y 原因: already installed。\n”;
continue
}
}
eof {
send_user “eof\r”
}
}
eof {
send_user “eof\r”
}
}
expect eof
}

7.Ansible常用命令(yunwei为组名或IP,替换成自己的就行)
在 Linux 中,我们可以通过 ansible-doc -l 命令查看到当前 ansible 都支持哪些模块,通过 ansible-doc -s 模块名 又可以查看该模块有哪些参数可以使用

(1).command
#默认省略
ansible yunwei -a “df -h”
ansible yunwei -m command -a “df -h”

(2).shell
#执行远程主机的脚本
ansible yunwei -m shell -a “ps aux|grep zabbix”
ansible yunwei -m shell -a “echo yunwei_linux >>/tmp/123.txt”
ansible yunwei -m shell -a “cat /tmp/123.txt”

(3).script
#执行主控端脚本
ansible yunwei -m script -a “/home/abc.sh”

(4).copy
#复制文件到远程服务器
ansible yunwei -m copy -a “src=/etc/ansible/ansible.cfg dest=/tmp/ansible.cfg owner=yunwei group=yunwei mode=0644”
ansible yunwei -m copy -a “src=/etc/hosts dest=/tmp/”

(5).cron
ansible yunwei -m cron -a “name=ceshi minute=*/5 job=’/bin/bash /usr/sbin/ntpdate ntp1.aliyun.com>/dev/null 2>&1′”
ansible yunwei -m cron -a “name=ceshi state=absent”

(6).ping
#检查主机连接
ansible yunwei -m ping

(7).yum
#在节点上安装namp
ansible yunwei -m yum -a “name=namp state=installed”
#在节点上安装httpd
ansible yunwei -m yum -a “name=httpd state=present”

(8).service
#在节点上停止crond服务
ansible yunwei -m service -a “name=crond state=stopped”
#在节点上启动crond服务
ansible yunwei -m service -a “name=crond state=started”
#在节点上启动服务,并开机自启动
ansible yunwei -m service -a “name=httpd state=started enabled=yes”

(9).file
#创建软链接
ansible yunwei -m file -a “src=/etc/hosts dest=/tmp/hosts state=link”
#删除软链接
ansible yunwei -m file -a “path=/tmp/hosts state=absent”

(10).group
#指定节点上创建一个组名为aaa,gid为2017的组
ansible yunwei -m group -a “gid=2017 name=aaa”

(11).user
#在节点上创建一个用户aaa,组为aaa
ansible yunwei -m user -a “name=aaa groups=aaa state=present”
#删除用户示例
ansible yunwei -m user -a “name=aaa groups=aaa remove=yes”

(12).raw
#在节点上运行hostname
ansible yunwei -m raw -a “hostname|tee”

(13).get_url
#将指定url上的文件下载到/tmp下
ansible yunwei -m get_url -a “url=http://192.168.1.100/favicon.ico dest=/tmp”

(14).synchronize
#将主控方/ansible/a目录推送到指定节点的/tmp目录下
ansible yunwei -m synchronize -a “src=/ansible/a dest=/tmp/ compress=yes”
#将yunwei节点的/tmp/a目录拉取到主控节点的/ansible目录下
ansible yunwei -m synchronize -a “mode=pull src=/tmp/a dest=/ansible/”

参数:
mode=push 模式:推送模式push和拉取模式pull;默认都是推送push
delete=yes 使两边的内容一样(即以推送方为主)
compress=yes 开启压缩,默认为开启
–exclude=.git 忽略同步.git结尾的文件

8.yaml剧本编写规则
(1).pyYAML剧本编写规则
缩进:yaml使用一个固定的缩进风格表示数据层结构关系,saltstack需要每个缩进级别由两个空格组成,一定不能使用tab键。
冒号:每个冒号后面一定有一个空格(以冒号结尾不需要空格,表示文件路径的模板可以不需要空格)。
短横线:表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分。

(2).编写剧本过程 编写好之后先 -C 测试 然后再执行
执行命令:vim /home/playbook/test.yml
– hosts: 172.16.1.31
tasks:
– command: uptime

(3).检查语法–syntax-check
执行命令:ansible-playbook –syntax-check /home/playbook/test.yml

(4).测试运行(加-C参数)
执行命令:ansible-playbook -C /home/playbook/test.yml

(5).运行
执行命令:ansible-playbook /home/playbook/test.yml

(6).追加文本内容 使用shell模块
执行命令:vim /home/playbook/test.yml
– hosts: 172.16.1.31
tasks:
– shell: echo “yunwei-linux” >>/etc/hosts

9.创建roles的步骤
(1).创建全局变量目录,执行命令:
mkdir -pv /home/playbook/group_vars/
touch /home/playbook/group_vars/all

(2).在每个角色命令的目录中分别创建files、handlers、tasks、templates、meta、defaults和vars目录,用不到的目录可以创建为空目录,但不可以不创建,执行命令:
mkdir -pv /home/playbook/roles/{test,zabbix_agent}/{tasks,files,templates,meta,handlers,vars,defaults}

注意事项:roles后面大括号内为角色名称,多个角色名称用英文号号分割
这个 playbook 为一个角色 ‘x’ 指定了如下的行为:
如果 roles/x/tasks/main.yml 存在, 其中列出的 tasks 将被添加到 play 中
如果 roles/x/handlers/main.yml 存在, 其中列出的 handlers 将被添加到 play 中
如果 roles/x/vars/main.yml 存在, 其中列出的 variables 将被添加到 play 中
如果 roles/x/meta/main.yml 存在, 其中列出的 “角色依赖” 将被添加到 roles 列表中 (1.3 and later)
所有 copy tasks 可以引用 roles/x/files/ 中的文件,不需要指明文件的路径。
所有 script tasks 可以引用 roles/x/files/ 中的脚本,不需要指明文件的路径。
所有 template tasks 可以引用 roles/x/templates/ 中的文件,不需要指明文件的路径。
所有 include tasks 可以引用 roles/x/tasks/ 中的文件,不需要指明文件的路径。

(3).在每个角色的handlers、tasks、meta、defaults、vars目录下创建main.yml文件,执行命令:
touch /home/playbook/roles/{test,zabbix_agent}/{defaults,vars,tasks,meta,handlers}/main.yml