Chinaunix首页 | 论坛 | 博客
  • 博客访问: 341693
  • 博文数量: 52
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 577
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-27 14:21
个人简介

知道自己该干嘛,知道自己能干嘛

文章分类

全部博文(52)

文章存档

2019年(1)

2018年(8)

2017年(2)

2016年(11)

2015年(3)

2014年(10)

2013年(17)

我的朋友

分类: 系统运维

2015-04-30 17:36:18

               
                       真的是好久好久没写了学习笔记了,最近一直都在看 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
           
  1.            root@zabbix /etc/ansible/roles/zabbix]#tree
  2.            .
  3.            |-- files
  4.            | -- disk-health.sh // 用于检测磁盘状态的脚本文件
  5.            | -- userparams_disk.conf // 自定义参数文件
  6.            | -- zabbix-release-2.4-1.el6.noarch.rpm // 安装 zabbix repo rpm 文件
  7.            |-- handlers
  8.            | -- main.yml // 重启 zabbix-agent 服务
  9.            |-- tasks
  10.            | -- main.yml // task 任务清单
  11.            |-- templates
  12.              -- zabbix_agentd.conf.j2 // zabbix_agentd.conf 模板
  13.            4 directories, 6 files
                      task文件内容 :
                      
  1.  
  2.            ---
  3.            #ZBX-agent play-book

  4.            - name: Copy zabbix repo file // 拷贝repo.rpm 文件
  5.              copy: >
  6.                    src=zabbix-release-2.4-1.el6.noarch.rpm
  7.                    dest=/etc/yum.repos.d/zabbix-release-2.4-1.el6.noarch.rpm
  8.                    mode=0644
  9.              tags:
  10.                  - ZBX-install

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

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

  1.            - name: Configure zabbix-agentd conf // 拷贝 zabbix_agentd.conf 配置文件
  2.              template: >
  3.                    src=zabbix_agentd.conf.j2
  4.                    dest=/etc/zabbix/zabbix_agentd.conf
  5.                    mode=0644
  6.                    notify: restart zabbix-agent
  7.              tags:
  8.                  - ZBX-reConfigure

  1.            - name: Create script directory // 创建存放脚本的自定义文件夹
  2.              file: >
  3.                    path=/etc/zabbix/script
  4.                    state=directory
  5.                    owner=zabbix
  6.                    group=zabbix
  7.                    mode=0755 
  8.              tags:
  9.                  - ZBX-configure

  1.            - name: Copy userparams and script // 拷贝自定义参数配置文件以及对应脚本至相应目录
  2.              copy:
  3.                    src: "{{ item.src }}"
  4.                    dest: "{{ item.dest }}"
  5.                    mode: 0644
  6.              with_items:
  7.              - {
  8.                    src: "userparams_disk.conf",
  9.                    dest: "/etc/zabbix/zabbix_agentd.d/userparams_disk.conf"
  10.              }
  11.              - {
  12.                    src: "disk-health.sh",
  13.                    dest: "/etc/zabbix/script/disk-health.sh"
  14.              }
  15.              tags:
  16.                  - ZBX-configure

  1.            - name: Start the zabbix-agentd service // 开启 zabbix-agent 服务
  2.              service: name=zabbix-agent state=started enabled=yes
  3.              tags:
  4.                  - ZBX-configure


                         handlers文件内容:
                        
  1.              ---
  2.              # Handlers for zabbix notifications
  3.              - name: restart zabbix-agent   // 模板发生变化,重启服务
  4.                service: name=zabbix-agent state=restarted
                          templates 文件内容:

  1.              root@zabbix /etc/ansible/roles/zabbix/templates]#cat zabbix_agentd.conf.j2

  2.              LogFile=/tmp/zabbix_agentd.log
  3.              Server={{ ZBX_server }}  // 引用已定义好的的变量
  4.              ServerActive={{ ZBX_server_active }} // 引用已定义好的变量
  5.              Hostname={{ inventory_hostname }}  // 引用清单中的主机名称
  6.              Timeout=30
  7.              Include=/etc/zabbix/zabbix_agentd.d/

                         files 文件夹下文件:

  1.              root@zabbix /etc/ansible/roles/zabbix/files]#ls
  2.              disk-health.sh userparams_disk.conf zabbix-release-2.4-1.el6.noarch.rpm // 所需文件放入此文件夹

                        Variable 内容:

  1.             [#52#root@zabbix /etc/ansible/group_vars]#pwd
  2.             /etc/ansible/group_vars
  3.             [#53#root@zabbix /etc/ansible/group_vars]#cat all
  4.             ---
  5.             # Variables here are applicable to all host groups
  6.             ntpserver: 192.168.167.200
  7.             #ZBX-variable
  8.             ZBX_server: 192.168.167.173
  9.             ZBX_server_active: 192.168.167.173:10051
                        主 site.yml 内容:

  1.             # This playbook deploys the whole application stack in this site.
  2.             - hosts: hadoop
  3.               user: root
  4.               gather_facts: false
  5.               roles:
  6.                - zabbix

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

                        
                                                                                                                                                                                        geo_Cail

                         参考资料:

                                           
                                           https://leanpub.com/ansible-for-devops





阅读(2391) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~