Ansible的目錄樹結構以下:nginx
# tree zabbix/shell
zabbix/ ├── defaults ├── files │ └── zabbix.tar.gz ├── handlers ├── meta ├── tasks │ └── main.yml ├── templates │ └── zabbix_agentd.conf.j2 └── vars └── main.yaml
zabbix.tar.gz:tar包是編譯安裝完zabbix後打的tar包tomcat
zabbix_agentd.conf.j2:編譯安裝完zabbix,copy的zabbix_agentd.conf文件,略微添加點東西,後面會展現。bash
zabbix_agentd:文件也是編譯安裝完zabbix,copy的啓動腳本文件,修改了裏面的zabbix_home變量。tcp
zabbix playbook文件以下:ide
cat zabbix.yml - hosts: all roles: - zabbix
tasks文件內容以下:spa
# cat main.yml - name: scp zabbix 到 client copy: src=zabbix.tar.gz dest={{ zabbix_home }} - name: scp zabbix_aegntd 到 client copy: src=/tmp/zabbix_agentd dest=/etc/init.d/ mode=777 - name: tar zxf zabbix.tar.gz 解壓 shell: cd {{ zabbix_home }} && tar -zxf zabbix.tar.gz - name: Copy zabbix_agent.conf Config file template: > src=zabbix_agentd.conf.j2 dest=/usr/local/zabbix/etc/zabbix_agentd.conf mode=0644 owner=root group=root - name: restart zabbix service: name=zabbix_agentd state=restarted
vars的內容以下:rest
# cat main.yaml zabbix_home: /usr/local
zabbix_agentd.conf.j2模板的文件內容以下:ip
#grep -v '^#' zabbix_agentd.conf.j2 | grep -v '^$' LogFile=/tmp/zabbix_agentd.log EnableRemoteCommands=1 Server=10.74.246.70 ListenPort=10050 ServerActive=10.74.246.70 Hostname={{ ansible_default_ipv4.address }} AllowRoot=1 Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf UnsafeUserParameters=1 UserParameter=tomcat_port,/usr/local/zabbix/share/zabbix/alertscripts/tomcat_port.sh $1 UserParameter=tcp[*],/usr/local/zabbix/share/zabbix/alertscripts/tcp_connection.sh $1 UserParameter=nginx_port,/usr/local/zabbix/share/zabbix/alertscripts/nginx_port.sh $1 UserParameter=erro_log[*],/usr/local/zabbix/share/zabbix/alertscripts/error_log.sh $1
而後就能夠定義hosts文件,批量操做安裝zabbix_agent。it
對於某些特定的主機,須要批量推送特定的zabbix_agentd.conf文件,能夠修改完zabbix_agentd.conf.j2模板,而後使用ansible推送:
#ansible-playbook zabbix.yml --start-at-task='Copy zabbix_agent.conf Config file'
固然也能夠指定推送那些機器,後面-l指定就行
#ansible-playbook zabbix.yml --start-at-task='Copy zabbix_agent.conf Config file' -l 192.168.121.128