目录结构
# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
├── roles
│ └── zabbix-agentd
│ ├── files
│ │ ├── install_zabbix.sh
│ │ └── zabbix-3.4.1.tar.gz
│ ├── handlers
│ ├── tasks
│ │ └── mail.yaml
│ └── templates
└── zabbix_agentd.yaml
zabbix.yaml:
cat /etc/ansible/zabbix.yaml
- name: Install Zabbix-agentd
hosts: zabbix
remote_user: root
roles:
- zabbix
main.yaml:
cat /etc/ansible/roles/zabbix/tasks/main.yaml
- name: copy zabbix_tar_gz to client
copy: src=zabbix-3.4.1.tar.gz dest=/root/zabbix-3.4.1.tar.gz
- name: copy install_shell to client
copy: src=install_zabbix.sh dest=/root/install_zabbix.sh
- name:
shell: /bin/bash /root/install_zabbix.sh
install_zabbix.sh:
cat /etc/ansible/roles/zabbix/files/install_zabbix.sh
#!/bin/bash
/usr/bin/yum install wget vim tree gcc gcc-c++ openssh-clients pcre -y
/usr/sbin/groupadd -g 201 zabbix
/usr/sbin/useradd -g zabbix -u 201 -s /sbin/nologin zabbix
tar xf zabbix-3.4.1.tar.gz
cd zabbix-3.4.1
./configure --prefix=/usr/local/zabbix --enable-agent
make && make install
/bin/mkdir /var/log/zabbix
/bin/chown zabbix.zabbix /var/log/zabbix
/bin/cp misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
/bin/chmod 755 /etc/init.d/zabbix_agentd
/bin/sed -i "s@BASEDIR=/usr/local@BASEDIR=/usr/local/zabbix@g" /etc/init.d/zabbix_agentd
/bin/ln -s /usr/local/zabbix/etc/ /etc/zabbix
/bin/ln -s /usr/local/zabbix/bin/* /usr/bin
/bin/ln -s /usr/local/zabbix/sbin/* /usr/sbin/
/bin/sed -i "s@Server=127.0.0.1@Server=42.62.114.243@g" /etc/zabbix/zabbix_agentd.conf
/bin/sed -i "s@ServerActive=127.0.0.1@ServerActive=42.62.114.243:10051@g" /etc/zabbix/zabbix_agentd.conf
/bin/sed -i "s@tmp/zabbix_agentd.log@var/log/zabbix/zabbix_agentd.log@g" /etc/zabbix/zabbix_agentd.conf
/bin/sed -i "s@^# UnsafeUserParameters=0@UnsafeUserParameters=1\n@g" /etc/zabbix/zabbix_agentd.conf
service zabbix_agentd start
chkconfig --add zabbix_agentd
chkconfig zabbix_agentd on
运行脚本:
ansible-playbook zabbix_agentd.yam
结果: