本次以多台机器需部署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 zabbixhosts: alltasks:- name: copy zabbix-agent2_rpmcopy:src: /root/zabbix-agent2-6.4.0-release1.el7.x86_64.rpmdest: /root/zabbix-agent2-6.4.0-release1.el7.x86_64.rpm- name: copy zabbix.shcopy:src: /etc/ansible/zabbix-install.shdest: /root/zabbix-install.sh- name: install zabbix_agent2shell: 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
EOFyum 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 ];thenecho 'zabbix has installed sucessfully!'
elseecho '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 ];thenecho 'zabbix has restarted sucessfully!'
elseecho 'zabbix has restarted failed,please check it!'exit 1
fi
3、执行playbook
[root@oxidized ansible]# ansible-playbook zabbix.yml
查看执行结果0fails即成功。