1、环境目录结构
# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── environments
│ ├── dev
│ │ ├── group_vars
│ │ │ └── zabbix
│ │ └── hosts
│ ├── prod
│ │ └── group_vars
│ └── test
│ └── group_vars
├── files
│ └── zabbix
│ ├── zabbix-agent-3.4.10-1.el6.x86_64.rpm
│ ├── zabbix-agent-3.4.10-1.el7.x86_64.rpm
│ ├── zabbix-sender-3.4.10-1.el6.x86_64.rpm
│ └── zabbix-sender-3.4.10-1.el7.x86_64.rpm
├── playbooks
│ └── zabbix
│ └── zabbix_agent_install.yml
├── roles
└── templates
└── zabbix
└── zabbix_agentd.conf.j2
2、相关的文件内容
# cat /etc/ansible/environments/dev/group_vars/zabbix
---
zabbix_server_ip: 192.168.1.254
# cat /etc/ansible/environments/dev/hosts
[lvs]
192.168.1.125
[nginx]
192.168.1.126
[zabbix:children]
lvs
nginx
# cat /etc/ansible/playbooks/zabbix/zabbix_agent_install.yml
---
- hosts: lvs:nginx
user: root
gather_facts: True
tasks:
- name: copy zabbix-agent to remote host
copy: src=/etc/ansible/files/zabbix/zabbix-agent-3.4.10-1.el6.x86_64.rpm dest=/tmp/zabbix-agent-3.4.10-1.el6.x86_64.rpm
- name: install zabbix agent on centos 6.x
yum:
name: /tmp/zabbix-agent-3.4.10-1.el6.x86_64.rpm
state: present
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"
- name: configure zabbix agent
template: src=/etc/ansible/templates/zabbix/zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
notify:
- restart zabbix-agent
- name: start zabbix-agent
service: name=zabbix-agent state=started enabled=yes
handlers:
- name: restart zabbix-agent
service: name=zabbix-agent state=restarted
# egrep -v "^$|^#" /etc/ansible/templates/zabbix/zabbix_agentd.conf.j2
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server={{ zabbix_server_ip }}
ListenPort=10050
ListenIP={{ ansible_default_ipv4.address }}
ServerActive={{ zabbix_server_ip }}
Hostname={{ ansible_hostname }}
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1
3、playbook 执行方法
# ansible-playbook playbooks/zabbix/zabbix_agent_install.yml
PLAY [lvs:nginx] ************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************
ok: [192.168.1.125]
ok: [192.168.1.126]
TASK [copy zabbix-agent to remote host] *************************************************************************************************************************************************************************
ok: [192.168.1.125]
changed: [192.168.1.126]
TASK [install zabbix agent on centos 6.x] ***********************************************************************************************************************************************************************
ok: [192.168.1.125]
changed: [192.168.1.126]
TASK [configure zabbix agent] ***********************************************************************************************************************************************************************************
ok: [192.168.1.125]
changed: [192.168.1.126]
TASK [start zabbix-agent] ***************************************************************************************************************************************************************************************
ok: [192.168.1.125]
changed: [192.168.1.126]
RUNNING HANDLER [restart zabbix-agent] **************************************************************************************************************************************************************************
changed: [192.168.1.126]
PLAY RECAP ******************************************************************************************************************************************************************************************************
192.168.1.125 : ok=5 changed=0 unreachable=0 failed=0
192.168.1.126 : ok=6 changed=5 unreachable=0 failed=0
4、再次确认 zabbix-agent 服务是否正常运行
# ansible -i /etc/ansible/environments/dev/hosts all -m shell -a "/etc/init.d/zabbix-agent status"
192.168.1.125 | SUCCESS | rc=0 >>
zabbix_agentd (pid 1283) is running...
192.168.1.126 | SUCCESS | rc=0 >>
zabbix_agentd (pid 1793) is running...