本次以多台机器需部署zabbix客户端为例:
机器先做免密互信,ansible主机上执行ssh-keygen,一路回车,然后将公钥发送给需管理的主机:
ssh-copy-id root@IP
1、编辑hosts文件,添加需配置的主机IP,并测试连通性
[root@oxidized ansible]# vim /etc/ansible/hosts
[all]
10.10.80.110
10.10.80.111
10.10.80.112
10.10.80.114
10.10.80.115
10.10.80.116
[root@oxidized ansible]# ansible all -m ping
10.10.80.111 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
。。。
。。。
。。。
2、编写playbook剧本
[root@oxidized ansible]# cat zabbix.yml
- name: install zabbix
hosts: all
tasks:
- name: copy zabbix-agent2_rpm
copy:
src: /root/zabbix-agent2-6.4.0-release1.el7.x86_64.rpm
dest: /root/zabbix-agent2-6.4.0-release1.el7.x86_64.rpm
- name: copy zabbix.sh
copy:
src: /etc/ansible/zabbix-install.sh
dest: /root/zabbix-install.sh
- name: install zabbix_agent2
shell: sh /root/zabbix-install.sh
[root@oxidized ansible]# cat zabbix-install.sh
#!/bin/bash
cd /etc/yum.repos.d/ && mkdir bak
mv *.repo bak/
cat >>/etc/yum.repos.d/local.repo <<EOF
[base]
name=Nexus
baseurl=http://10.10.200.20:8081/repository/yumHosted/
enabled=1
gpgcheck=0
EOF
yum makecache fast
cd /root && yum install -y zabbix-agent2-6.4.0-release1.el7.x86_64.rpm
systemctl start zabbix-agent2 && systemctl start zabbix-agent2
systemctl status zabbix-agent2
if [ $? = 0 ];then
echo 'zabbix has installed sucessfully!'
else
echo 'zabbix is not running,please check it!'
exit 1
fi
sed -i 's#Server=127.0.0.1#Server=10.10.80.101#g' /etc/zabbix/zabbix_agent2.conf
sed -i 's#ServerActive=127.0.0.1#ServerActive=10.10.80.101#g' /etc/zabbix/zabbix_agent2.conf
sed -i 's#Hostname=Zabbix server#Hostname=localhost#g' /etc/zabbix/zabbix_agent2.conf
systemctl restart zabbix-agent2
if [ $? = 0 ];then
echo 'zabbix has restarted sucessfully!'
else
echo 'zabbix has restarted failed,please check it!'
exit 1
fi
3、执行playbook
[root@oxidized ansible]# ansible-playbook zabbix.yml
查看执行结果0fails即成功。