ansible编写playbook批量安装zabbix-agent
1.playbook的编写
centos: 先下载好一个agent rpm包用于发送给被控机,部署前会判断agent有没有安装
注:server IP需自行修改
---
- hosts: jenkins
vars:
host_IP: "{{ ansible_default_ipv4.address }}"
host_name: "{{ ansible_hostname }}"
zabbix_pk: zabbix-agent-4.4.9-1.el7.x86_64.rpm
zabbix_conf: /etc/zabbix/zabbix_agentd.conf
tasks:
- name: Whether to install
shell: rpm -qa | grep zabbix-agent | wc -l
register: rpm_num
- name: Send the zabbix-agent package
copy: src=/tmp/{{zabbix_pk}} dest=/tmp/{{zabbix_pk}}
when: rpm_num.stdout == "0"
- name: Install zabbix-agent
shell: rpm -Uvh --nodeps /tmp/{{zabbix_pk}}
when: rpm_num.stdout == "0"
- name: Modify the Zabbix-agent configuration
shell: sed -i 's/\# EnableRemoteCommands=0/EnableRemoteCommands=1/;/Server/s/127.0.0.1/server.zabbix.com/g;s/Hostname=Zabbix server/Hostname={{ host_name }}{{ host_IP }}/' "{{ zabbix_conf }}"
when: rpm_num.stdout == "0"
- name: Give Zabbix sudo permissions
shell: chmod u+w /etc/sudoers && sed -i '/root\tALL/azabbix\tALL=(ALL:ALL) NOPASSWD:ALL' /etc/sudoers && chmod u-w /etc/sudoers
when: rpm_num.stdout == "0"
- name: Add Zabbix DNS resolution
shell: grep server.zabbix.com /etc/hosts || echo "192.168.2.10 server.zabbix.com" >> /etc/hosts
when: rpm_num.stdout == "0"
- name: Start the zabbix-agent
- name: service status
shell: ss -tlnp | grep :10050 | wc -l
register: port_status
- name: Show port_status info
debug: var=port_status.stdout verbosity=0
# - name: service stop
# shell: systemctl stop zabbix-agent
# when: rpm_num.stdout == "0" and port_status.stdout != "0"
# - name: reboot in a minute
# shell: shutdown -r 1 &
# when: rpm_num.stdout == "0" and port_status.stdout != "0"
2.centos7跟centos6
差不多
全文只需改动 :
包名:zabbix_pk: zabbix-agent-4.4.9-1.el7.x86_64.rpm
关闭服务:
- name: service stop
shell: systemctl stop zabbix-agent
3.执行
# ansible-playbook -i agent_hosts zabbix_agent-el6.yml
4.hosts文件
# cat agent_hosts
[agent]
192.168.2.1[1:3]:33322 ansible_ssh_pass=123456