Ansible部署zabbix-agent

playbook目录

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

install_zabbix_agent.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

roles/install_zabbix_agent/tasks/main.yml

- 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'

roles/install_zabbix_agent/templates/zabbix-agentd.conf.j2

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/

roles/install_zabbix_agent/vars/main.yml

zabbix_server_ip: *.*.*.*

执行

ansible-playbook -i hosts install_zabbix_agent.yml
 
原文地址:https://www.cnblogs.com/Z-style/p/7279476.html