大湖之子 发表于 2015-12-24 12:39:55

Ansible deploy zabbix agent

真的是好久好久没写了学习笔记了,最近一直都在看 ansible,觉得自己需要学习一个集群配置管理工具,既能批量执行命令,又可以配置管理服务,还好 ansible 横空出世,呵呵。 官方文档看完了,决定试试ansible的效果。于是,最近的hadoop扩容的ambari前期准备工作,和zabbix-agent的批量安装都用anbsible 做的,写好role,批量执行下部署agnt, 在结合 zabbix auto registration, 一气呵成,下面简单介绍下,zabbix-agent 的整个部署流程。

                     首先梳理下,我们此次安装部署的要求:
                     (1) 安装zabbix-agent 客户端
                     (2) 配置正确 zabbi-agentd.conf 文件
                     (3) 分发自定义的userpatermeter.conf,以及对应需求的脚本
                     (4) 启动服务,自动注册 zabbix server

                     介绍下role目录结构,以及相关task
         


[*]         root@zabbix /etc/ansible/roles/zabbix]#tree
[*]         .
[*]         |-- files
[*]         |-- disk-health.sh   // 用于检测磁盘状态的脚本文件
[*]         |-- userparams_disk.conf    // 自定义参数文件
[*]         |-- zabbix-release-2.4-1.el6.noarch.rpm   // 安装 zabbix repo rpm 文件
[*]         |-- handlers
[*]         |-- main.yml   // 重启 zabbix-agent 服务
[*]         |-- tasks
[*]         |-- main.yml   // task 任务清单
[*]         |-- templates
[*]             -- zabbix_agentd.conf.j2// zabbix_agentd.conf 模板
[*]         4 directories, 6 files
                      task文件内容 :
                     


[*]
[*]         ---
[*]         #ZBX-agent play-book
[*]
[*]
[*]         - name: Copy zabbix repo file// 拷贝repo.rpm 文件
[*]             copy: >
[*]                   src=zabbix-release-2.4-1.el6.noarch.rpm
[*]                   dest=/etc/yum.repos.d/zabbix-release-2.4-1.el6.noarch.rpm
[*]                   mode=0644
[*]             tags:
[*]               - ZBX-install



[*]         - name: Install zabbix repo//安装repo.rpm 文件
[*]             command: rpm -ivh /etc/yum.repos.d/zabbix-release-2.4-1.el6.noarch.rpm
[*]             register: result
[*]             tags:
[*]               - ZBX-install



[*]         - name: Install zabbix-agent // 如果安装成功,yum 安装 zabbix-agent
[*]             yum: name=zabbix-agent-2.4.4-1.el6 state=present
[*]             when: result.rc == 0
[*]             tags:
[*]               - ZBX-install



[*]         - name: Configure zabbix-agentd conf// 拷贝 zabbix_agentd.conf 配置文件
[*]             template: >
[*]                   src=zabbix_agentd.conf.j2
[*]                   dest=/etc/zabbix/zabbix_agentd.conf
[*]                   mode=0644
[*]                   notify: restart zabbix-agent
[*]             tags:
[*]               - ZBX-reConfigure



[*]         - name: Create script directory   // 创建存放脚本的自定义文件夹
[*]             file: >
[*]                   path=/etc/zabbix/script
[*]                   state=directory
[*]                   owner=zabbix
[*]                   group=zabbix
[*]                   mode=0755
[*]             tags:
[*]               - ZBX-configure



[*]         - name: Copy userparams and script// 拷贝自定义参数配置文件以及对应脚本至相应目录
[*]             copy:
[*]                   src: "{{ item.src }}"
[*]                   dest: "{{ item.dest }}"
[*]                   mode: 0644
[*]             with_items:
[*]             - {
[*]                   src: "userparams_disk.conf",
[*]                   dest: "/etc/zabbix/zabbix_agentd.d/userparams_disk.conf"
[*]             }
[*]             - {
[*]                   src: "disk-health.sh",
[*]                   dest: "/etc/zabbix/script/disk-health.sh"
[*]             }
[*]             tags:
[*]               - ZBX-configure



[*]         - name: Start the zabbix-agentd service    // 开启 zabbix-agent 服务
[*]             service: name=zabbix-agent state=started enabled=yes
[*]             tags:
[*]               - ZBX-configure
[*]

                         handlers文件内容:
                        


[*]             ---
[*]             # Handlers for zabbix notifications
[*]
[*]             - name: restart zabbix-agent   // 模板发生变化,重启服务
[*]               service: name=zabbix-agent state=restarted
                        templates 文件内容:



[*]             root@zabbix /etc/ansible/roles/zabbix/templates]#cat zabbix_agentd.conf.j2
[*]
[*]             LogFile=/tmp/zabbix_agentd.log
[*]             Server={{ ZBX_server }}// 引用已定义好的的变量
[*]             ServerActive={{ ZBX_server_active }} // 引用已定义好的变量
[*]             Hostname={{ inventory_hostname }}// 引用清单中的主机名称
[*]             Timeout=30
[*]             Include=/etc/zabbix/zabbix_agentd.d/

                         files 文件夹下文件:



[*]             root@zabbix /etc/ansible/roles/zabbix/files]#ls
[*]             disk-health.shuserparams_disk.confzabbix-release-2.4-1.el6.noarch.rpm // 所需文件放入此文件夹
[*]
                        Variable 内容:



[*]            [#52#root@zabbix /etc/ansible/group_vars]#pwd
[*]            /etc/ansible/group_vars
[*]            [#53#root@zabbix /etc/ansible/group_vars]#cat all
[*]            ---
[*]            # Variables here are applicable to all host groups
[*]
[*]            ntpserver: 192.168.167.200
[*]
[*]            #ZBX-variable
[*]            ZBX_server: 192.168.167.173
[*]            ZBX_server_active: 192.168.167.173:10051
                        主 site.yml 内容:



[*]            # This playbook deploys the whole application stack in this site.
[*]
[*]            - hosts: hadoop
[*]            user: root
[*]            gather_facts: false
[*]            roles:
[*]               - zabbix

                        OK, ansible 端的设置就结束了,我们只需要执行一下 ansible-playbook site.yml -vv 就可以了, zabbix-sever 上已开启相应的自动注册条目。
                        最终达到了 zabbix-agent 自动批量化部署,zabbix server 自动注册,分组以及关联基础模板。

                        
                                                                                                                                                                                        geo_Cail

                         参考资料:

                                           http://docs.ansible.com/
                                           https://leanpub.com/ansible-for-devops
页: [1]
查看完整版本: Ansible deploy zabbix agent