ansible自动部署 zabbix-agent 的模块

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible自动部署 zabbix-agent 的模块相关的知识,希望对你有一定的参考价值。

ansible自动部署 zabbix-agent 模块  的准备阶段  

ansible所在的服务端可以免密钥登录所被部署的机器称为客户端。

免密钥的做法

服务端 ssh-keygen  一路回车生成密钥对 

ssh-copy-id 指定IP 将公钥发给指定的ip 即可 

ssh-copy-id 192.168.1.18 


下面红色是代表文件或目录  黑色字体代表是内容 


使用了 roles 方法  整体的目录结构是

/etc/ansible/zabbix-agent.yml

[[email protected] ansible]# pwd

/etc/ansible


/etc/ansibl/hosts

[[email protected] ansible]# cat hosts


192.168.1.145  #centso 系统 

192.168.1.147  #   centos 系统   加入 ansible 的客户端ip  本次示例中是这几个 ip  

192.168.1.148  #ubuntu 系统


[[email protected] ansible]# cat zabbix-agent.yml 

---

- hosts: all

  roles:

    - zabbix-agent



/etc/ansible/roles


[[email protected] ansible]#  ls

zabbix-agent


/etc/ansible/zabbix-agent



[[email protected] zabbix-agent]# ls

files  handlers  tasks  templates


/etc/ansible/zabbix-agent/files  


[[email protected] zabbix-agent]# ls files/

zabbix


/etc/ansible/zabbix-agent/handlers


[[email protected] zabbix-agent]# ls handlers/

main.yml


/etc/ansible/zabbix-agent/tasks



[[email protected] zabbix-agent]# ls tasks/

files.yml  main.yml  package.yml  service.yml



/etc/ansible/zabbix-agnet/templates



[[email protected] zabbix-agent]# ls templates/

zabbix_agentd.conf



/etc/ansible/zabbix-agent/files/zabbix/conf.d


[[email protected] zabbix-agent]# ls files/zabbix/conf.d/  #zabbix-agent的配置文件 键值

impression.conf  imsecret.conf  mystar.conf  resonance.conf  tagme.conf  userReg.conf


/etc/ansible/zabbix-agent/files/zabbix/scripts 要复制去客户端的文件


[[email protected] zabbix-agent]# ls files/zabbix/scripts/  下边是zabbix 可执行的脚本用来监控 

/etc/ansible/zabbix-agent/handlers/main.yml  这个是开启zabbix-agent的 


[[email protected] zabbix-agent]# cat handlers/main.yml 

---

- name: start zabbix_agentd

  service: name=zabbix-agentd state=started


/etc/ansible/zabbix-agent/tasks/files.yml 将 /etc/ansible/zabbix-agent/files/* 复制到客户端的配置


[[email protected] zabbix-agent]# cat tasks/files.yml 

---

- name: copy centos template conf file

  template: src=zabbix_agentd.conf  dest=/etc/

  when: ansible_os_family == "RedHat" #判断系统 


- name: copy ubuntu template conf file

  template: src=zabbix_agentd.conf  dest=/etc/zabbix/

  when: ansible_os_family == "Debian"


- name: crete conf.d and scripts

  file: path={{ item }} state=directory owner=root group=root mode=0755

  with_items:

  - /etc/zabbix/scripts

  - /etc/zabbix/conf.d

  - /etc/zabbix/scripts1


- name: copy the scripts/file

  copy: src=zabbix/scripts/ dest=/etc/zabbix/scripts/ mode=0755


- name: copy the scripts1/file 

  copy: src=zabbix/scripts1/ dest=/etc/zabbix/scripts1/ mode=0755


- name: copy the conf.d/file 

  copy: src=zabbix/conf.d/ dest=/etc/zabbix/conf.d/


/etc/ansible/zabbix-agent/tasks/main.yml 主的配置 在同级目录下会先执行这个main


[[email protected] zabbix-agent]# cat tasks/main.yml 

---

- include: ‘package.yml‘

- include: ‘files.yml‘

- include: ‘service.yml‘



/etc/ansible/zabbix-agent/tasks/package.yml 安装zabbix-agent的配置


[[email protected] zabbix-agent]# cat tasks/package.yml 

---

- name: useraddd the zabbix

  user: name=zabbix


- name: install epel-release

  yum: name=epel-release.noarch state=latest

  when: ansible_os_family == "RedHat"


- name: install epel-release and  zabbix_agent_package

  yum: name={{ item }} state=present

  with_items:

  - zabbix22-agent-2.2.18-1.el6.x86_64

  - zabbix22-2.2.18-1.el6.x86_64

  when: ansible_os_family == "RedHat"


- name: apt-get install to ubuntu 

  apt: name={{ item }} state=present

  with_items:

  - zabbix-agent 

  when: ansible_os_family == "Debian"



/etc/ansible/zabbix-agent/tasks/service.yml   开启脚本 万无一失  2重开启


[[email protected] zabbix-agent]# cat tasks/service.yml 

---

- name: start zabbix-agentd

  service: name=zabbix-agentd state=started

  when: ansible_os_family == "RedHat"


- name: start ubuntu zabbix-agent

  service: name=zabbix-agent state=started

  when: ansible_os_family == "Debian"



/etc/ansible/zabbix-agent/templates/zabbix_agentd.conf  配置文件的模板放在这里 



[[email protected] zabbix-agent]# cat templates/zabbix_agentd.conf 

PidFile=/var/run/zabbix/zabbix_agentd.pid

LogFile=/var/log/zabbix/zabbix_agentd.log

LogFileSize=100

Server=192.168.1.100  #zabbix 服务端所在的 ip 

ServerActive=192.168.1.100  # zabbix-agent 主动模式 配置 

Hostname={{ ansible_hostname }}  #这个是系统变量

Include=/etc/zabbix/conf.d/

UnsafeUserParameters=1

# UserParameter  自定义的 监控项 


具体执行 

[[email protected] ansible]# pwd

/etc/ansible

[[email protected] ansible]# ansible-playbook zabbix-agent.yml  

[[email protected] ansible]# ansible-playbook zabbix-agent.yml   -vvvv

加  -vvv  可以看到更详细的过程  其实是 没必要的  


本文出自 “手有余香” 博客,请务必保留此出处http://19941018.blog.51cto.com/11889001/1949589

以上是关于ansible自动部署 zabbix-agent 的模块的主要内容,如果未能解决你的问题,请参考以下文章

使用ansible-playbook部署zabbix-agent-4.0

ansible批量部署zabbix-agent

ansible之block实践--自动根据centos版本安装zabbix-agent

zabbix批量部署Windows和Linux的agent

ansible-playbook roles 安装zabbix-agent 监控 php

ansible-playbook agent实例