ansible-playbook批量添加zabbix客户端
下载zabbix agent客户端
[ansible@hk-elk-redis1 playbook]$ sudo wget http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-agent-3.0.4-1.el7.x86_64.rpm [ansible@hk-elk-redis1 playbook]$ sudo wget http://repo.zabbix.com/zabbix/3.0/rhel/6/x86_64/zabbix-agent-3.0.4-1.el6.x86_64.rpm
[ansible@hk-elk-redis1 inventory]$ cat /etc/ansible/inventory/cc [hosts] 10.20.11.201 10.20.11.202 [zabbix] 10.20.11.201 10.20.11.202
cat /opt/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=512 EnableRemoteCommands=0 LogRemoteCommands=0 Server=1.1.1.1 Hostname=localhost ListenIP=127.0.0.1 ListenPort=10050 ServerActive=1.1.1.1
[ansible@hk-elk-redis1 playbook]$ cat zabbix-agent.yaml --- - hosts: zabbix gather_facts: True remote_user: ansible tasks: - name: Centos6 system zabbix-agent install copy: src=/opt/zabbix-agent-3.0.4-1.el6.x86_64.rpm dest=/tmp/zabbix-agent-3.0.4-1.el6.x86_64.rpm when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "6" - name: Centos7 system zabbix-agent install copy: src=/opt/zabbix-agent-3.0.4-1.el7.x86_64.rpm dest=/tmp/zabbix-agent-3.0.4-1.el7.x86_64.rpm when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "7" - name: yum install zabbix-agent shell: cd /tmp;yum -y localinstall zabbix-agent-3.0.4-1*.rpm - name: rm zabbix-zabbix shell: cd /tmp;rm -rf zabbix-3.0.4-1*.rpm - name: rm last zabbix_agentd.conf shell: cd /etc/zabbix;mv zabbix_agentd.conf zabbix_agentd.conf.bak #- name: copy new zabbix_agentd.conf # template: > # src=zabbix_agentd.conf.j2 # dest=/etc/zabbix/zabbix_agentd.conf # owner=root # group=root # mode=0644 - name: copy new zabbix_agentd.conf copy: src=/opt/zabbix_agentd.conf dest=/etc/zabbix/zabbix_agentd.conf owner=root group=root mode=0644 - name: configure Server IP shell: sed -i 's/Server=Zabbix server/Server=1.1.1.1/' /etc/zabbix/zabbix_agentd.conf - name: configure Hostname shell: hostname=`hostname`;sed -i "s/Hostname=localhost/Hostname=${hostname}/" /etc/zabbix/zabbix_agentd.conf - name: Centos6 configure ListenIP shell: host=`ifconfig |awk -F '[ :]+' 'NR==2 {print $4}'`;sed -i "s/ListenIP=127.0.0.1/ListenIP=${host}/" /etc/zabbix/zabbix_agentd.conf when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "6" - name: Centos7 configure ListenIP shell: host=`ifconfig |awk -F '[ :]+' 'NR==2 {print $3}'`;sed -i "s/ListenIP=127.0.0.1/ListenIP=${host}/" /etc/zabbix/zabbix_agentd.conf #shell: host=`ifconfig |awk -F '[ :]+' 'NR==2 {print $3}'`;sed -i "s/ListenIP=127.0.0.1/ListenIP={{ ansible_all_ipv4_addresses }}/" /etc/zabbix/zabbix_agentd.conf when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "7" - name: configure ServerActive shell: sed -i 's/ServerActive=Zabbix server/ServerActive=1.1.1.1/' /etc/zabbix/zabbix_agentd.conf - name: Centos6 system start zabbix-agent shell: /etc/init.d/zabbix-agent start;chkconfig zabbix-agent on when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "6" - name: Centos7 system start zabbix-agent shell: systemctl enable zabbix-agent;systemctl restart zabbix-agent.service when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "7"