zabbix/
├── hosts ##定義的主機列表
├── install_zabbix_agent.yml ##安裝入口文件
└── roles
├── install_zabbix_agent
├── files
│ ├── zabbix-release-3.2-1.el7.noarch.rpm
│ └── zabbix-release_3.2-1+trusty_all.deb
├── tasks
│ └── main.yml ##安裝文件
├── templates
│ ├── zabbix-agentd.conf.j2
│ └── zabbix-agentd.conf.j2.bak
└── vars
└── main.yml
- hosts: xiaozhan
roles:
- install_zabbix_agent
vars:
- zabbix_centos: zabbix-release-3.2-1.el7.noarch.rpm
- zabbix_ubuntu: zabbix-release_3.2-1+trusty_all.deb
- name: copy zabbix file
copy: src={{ zabbix_centos }} dest=/root/
when: ansible_distribution == 'CentOS'
- name: rpm -ivh zabbix-agent
shell: rpm -ivh /root/{{ zabbix_centos }}
when: ansible_distribution == 'CentOS'
- name: install zabbix-agent
shell: yum -y install zabbix-agent
when: ansible_distribution == 'CentOS'
- name: copy zabbix-agent.conf
template: src=zabbix-agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
when: ansible_distribution == 'CentOS'
- name: start zabbix-agent
shell: systemctl start zabbix-agent
when: ansible_distribution == 'CentOS'
- name: copy zabbix file
copy: src={{ zabbix_ubuntu }} dest=/root/
when: ansible_distribution == 'Ubuntu'
- name: install zabbix
shell: dpkg -i /root/{{ zabbix_ubuntu }}
when: ansible_distribution == 'Ubuntu'
- name: apt-get update
shell: apt-get update
when: ansible_distribution == 'Ubuntu'
- name: install zabbix
shell: apt-get install zabbix-agent
when: ansible_distribution == 'Ubuntu'
- name: copy zabbix-agent.conf
template: src=zabbix-agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
when: ansible_distribution == 'Ubuntu'
- name: start zabbix
service: name=zabbix-agent state=restarted
when: ansible_distribution == 'Ubuntu'
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=serverip
ServerActive=serverip
HostnameItem=system.hostname ##自動獲取主機名
Include=/etc/zabbix/zabbix_agentd.d/
zabbix_server_ip: *.*.*.*
ansible-playbook -i hosts install_zabbix_agent.yml