码迷,mamicode.com
首页 > 其他好文 > 详细

ansible部署zabbix客户端

时间:2017-10-23 01:01:53      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:zabbix   ansible   自动化   

Ansible部署zabbix客户端


1、简介

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:

(1)、连接插件connection plugins:负责和被监控端实现通信;

(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

(3)、各种模块核心模块、command模块、自定义模块;

(4)、借助于插件完成记录日志邮件等功能;

(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务.


因为 ansible 是基于ssh协议的,所以在此之前,我们需要在 zabbix_server 端进行对其他主机实现免密登录。

使用命令生成密钥

[root@ansible ~]# ssh-keygen 


将公钥发送到所有安装zabbix客户端的主机

[root@ansible ~]# ssh-copy-id 192.168.163.170

[root@ansible ~]# ssh-copy-id 192.168.163.171


安装 ansible 软件

[root@ansible ~]# yum install -y ansible


修改配置文件,将zabbix 客户机添加进组,(在文末添加即可)

[root@ansible ~]# vim /etc/ansible/hosts 

[webserver]
192.168.163.170
192.168.163.171


创建一个安装zabbix客户端的剧本

[root@ansible ansible]# vim /etc/ansible/install_zabbix.yaml 

- hosts: webserver
  remote_user: root
  tasks:
  - name: copy
    copy: src=/usr/local/src/zabbix-3.2.7.tar.gz dest=/usr/local/src/zabbix-3.2.7.tar.gz
  - name: tar
    shell: cd /usr/local/src;tar xf zabbix-3.2.7.tar.gz
  - name: useradd
    shell: useradd zabbix -s /sbin/nologin
  - name: yum
    yum: name={{ item }} state=latest
    with_items:
    - make
    - gcc
    - curl
    - curl-devel
  - name: configure
    shell: cd /usr/local/src/zabbix-3.2.7;./configure --with-net-snmp --with-libcurl --enable-agent --prefix=/usr/local/zabbix;make && make install
  - name: script
    shell: cp /usr/local/src/zabbix-3.2.7/misc/init.d/fedora/core5/zabbix_agentd /etc/init.d/
  - name: quanxian
    shell: chmod 700 /etc/init.d/zabbix_agentd 
  - name: vim zabbix_agent
    shell: sed -i ‘s/ZABBIX_BIN="\/usr\/local\/sbin\/zabbix_agentd"/ZABBIX_BIN="\/usr\/local\/zabbix\/sbin\/zabbix_agentd"/g‘ /etc/init.d/zabbix_agentd
  - name: vim_conf
    shell: sed -i ‘s/Server=127.0.0.1/Server={{ server_ip }}/g‘ /usr/local/zabbix/etc/zabbix_agentd.conf
  - name: restart_server
    shell: /etc/init.d/zabbix_agentd restart


执行该剧本

[root@ansible ansible]# ansible-playbook -e server_ip=192.168.163.169 install_zabbix.yaml 

注意:-e 选项指定的server_ip 是文件里的server_ip,也就是客户端所有指定的zabbix_server的IP


输出结果:

[root@localhost ansible]# ansible-playbook install_zabbix.yaml 


PLAY [webserver] ********************************************************************************************************


TASK [Gathering Facts] **************************************************************************************************

ok: [192.168.163.171]

ok: [192.168.163.170]


TASK [copy] *************************************************************************************************************

changed: [192.168.163.170]

changed: [192.168.163.171]


TASK [tar] **************************************************************************************************************

changed: [192.168.163.170]

changed: [192.168.163.171]


TASK [useradd] **********************************************************************************************************

changed: [192.168.163.170]

changed: [192.168.163.171]


TASK [yum] **************************************************************************************************************

^CProcess WorkerProcess-10:

Traceback (most recent call last):

  File "/usr/lib64/python2.7/multiprocessing/process.py", line 258, in _bootstrap

Process WorkerProcess-9:

Traceback (most recent call last):

  File "/usr/lib64/python2.7/multiprocessing/process.py", line 258, in _bootstrap

 [ERROR]: User interrupted execution


[root@localhost ansible]# ansible-playbook -e server_ip=192.168.163.169 install_zabbix.yaml 


PLAY [webserver] ********************************************************************************************************


TASK [Gathering Facts] **************************************************************************************************

ok: [192.168.163.171]

ok: [192.168.163.170]


TASK [copy] *************************************************************************************************************

changed: [192.168.163.170]

changed: [192.168.163.171]


TASK [tar] **************************************************************************************************************

changed: [192.168.163.171]

changed: [192.168.163.170]


TASK [useradd] **********************************************************************************************************

changed: [192.168.163.170]

changed: [192.168.163.171]


TASK [yum] **************************************************************************************************************

ok: [192.168.163.171] => (item=[u‘make‘, u‘gcc‘, u‘curl‘, u‘curl-devel‘])

ok: [192.168.163.170] => (item=[u‘make‘, u‘gcc‘, u‘curl‘, u‘curl-devel‘])


TASK [configure] ********************************************************************************************************

changed: [192.168.163.171]

changed: [192.168.163.170]


TASK [script] ***********************************************************************************************************

changed: [192.168.163.170]

changed: [192.168.163.171]


TASK [quanxian] *********************************************************************************************************

 [WARNING]: Consider using file module with mode rather than running chmod


changed: [192.168.163.170]

changed: [192.168.163.171]


TASK [vim zabbix_agent] *************************************************************************************************

 [WARNING]: Consider using template or lineinfile module rather than running sed


changed: [192.168.163.170]

changed: [192.168.163.171]


TASK [vim_conf] *********************************************************************************************************

changed: [192.168.163.171]

changed: [192.168.163.170]


TASK [restart_server] ***************************************************************************************************

changed: [192.168.163.170]

changed: [192.168.163.171]


PLAY RECAP **************************************************************************************************************

192.168.163.170            : ok=11   changed=9    unreachable=0    failed=0   

192.168.163.171            : ok=11   changed=9    unreachable=0    failed=0   


测试:

在 zabbix 管理界面添加 zabbix 客户机

技术分享

技术分享

添加成功

技术分享


监控到数据

技术分享


实验成功!!!!!!!!!!!!!!!!!!!!!!!

本文出自 “xhk777” 博客,请务必保留此出处http://xhk777.blog.51cto.com/13405744/1975082

ansible部署zabbix客户端

标签:zabbix   ansible   自动化   

原文地址:http://xhk777.blog.51cto.com/13405744/1975082

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!