Ansible的目录树结构如下:
#tree zabbix/
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包
zabbix_agentd.conf.j2:编译安装完zabbix,copy的zabbix_agentd.conf文件,略微添加点东西,后面会展示。
zabbix_agentd:文件也是编译安装完zabbix,copy的启动脚本文件,修改了里面的zabbix_home变量。
zabbix playbook文件如下:
cat zabbix.yml
-hosts:all
roles:
-zabbix
tasks文件内容如下:
#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的内容如下:
#cat main.yaml
zabbix_home:/usr/local
zabbix_agentd.conf.j2模板的文件内容如下:
#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。
对于某些特定的主机,需要批量推送特定的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