Ansible批量安装Zabbix-agnet客户端

简介

本文详细使用ansible批量安装Zabbix-agent,在客户端比较多,结合ansible进行批量安装,将非常重要,省时高效,并且能够加稳定

安装步骤

1、安装关联组件

[root@centos78 \~]# yum install gcc* pcre* -y (缺少必要组件脚本推送会报错)

[root@centos78 \~]# yum install epel-release -y

2、安装ansible组件

[root@centos78 \~]# yum install ansible –y

3、配置ssh免密通信

[root@centos78 \~]# ssh-keygen (连续敲回车即可)

[root@centos78 \~]# ssh-copy-id 192.168.101.78

[root@centos78 \~]# ssh-copy-id 192.168.101.75

4、配置ansible,编辑hosts,定义组

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

[zabbix]

192.168.101.75

192.168.101.78

ping客户端是否通讯

[root@centos78 \~]# ansible zabbix -m ping

192.168.101.78 | SUCCESS => {

"changed": false,

"ping": "pong"}

192.168.101.75 | SUCCESS => {

"changed": false,

"ping": "pong"}

5、配置批量推送脚本

[root@centos78 \~]# cd /etc/ansible/

[root@centos78 ansible]# cat zabbix\_agent.yml (使用ansible的jinjia模板来部署ansible客户端)

  • hosts: zabbix

    remote\_user: root

    tasks:

    • name: install zabbix\_agent packages

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

      with\_items:

      • curl-devel
    • name: copy install\_shell to clien

      copy: src=/etc/ansible/zabbix\_agent.sh dest=/usr/local/src/zabbix\_agent.sh

    • name: sent tar

      copy: src=/usr/local/src/zabbix-3.4.12.tar.gz dest=/usr/local/src/zabbix-3.4.12.tar.gz (没有安装包的可以按照操作下载)

      notify: install shell

    handlers:

    • name: install shell

      shell: /bin/bash /usr/local/src/zabbix\_agent.sh

6、下载用于批量安装的客户端zabbix-3.4.12.tar.gz

[root@centos78 ansible]# cd /usr/local/src/ (下载路径和脚本路径一致)

[root@centos78 src]# wget https:`//nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.4.12/zabbix-3.4.12.tar`.gz

[root@centos78 ansible]# cd /usr/local/src/ (下载路径和脚本路径一致)

7、编辑脚本内容

[root@centos78 ansible]# cat zabbix\_agent.sh

#!/bin/bashuseradd zabbix -s /sbin/nologincd /usr/local/srctar -xf zabbix-3.4.12.tar.gzcd zabbix-3.4.12
./configure --with-net-snmp --with-libcurl --enable-agent --prefix=/usr/local/zabbixmake && make installcp misc/init.d/fedora/core5/zabbix_agentd /etc/init.d/chmod 700 /etc/init.d/zabbix_agentdsed -i  's\ZABBIX_BIN="/usr/local/sbin/zabbix_agentd\ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd\g' /etc/init.d/zabbix_agentdsed -i  's\Server=127.0.0.1\Server=192.168.101.78\g' /usr/local/zabbix/etc/zabbix_agentd.confsed  -i  's\ServerActive=127.0.0.1\ServerActive=192.168.101.78\g' /usr/local/zabbix/etc/zabbix_agentd.conf 
/etc/init.d/zabbix_agentd startchkconfig zabbix_agentd on

8.执行脚本

[root@centos78 ansible]# ansible-playbook zabbix\_agent.yml

PLAY [zabbix] ******************************************************************

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

ok: [192.168.101.78]

ok: [192.168.101.75]

TASK [install zabbix\_agent packages] *******************************************

ok: [192.168.101.75] => (item=[u'curl-devel'])

ok: [192.168.101.78] => (item=[u'curl-devel'])

TASK [copy install\_shell to clien] *********************************************

ok: [192.168.101.78]

ok: [192.168.101.75]

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

ok: [192.168.101.78]

ok: [192.168.101.75]

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

192.168.101.75 : ok=4 changed=0 unreachable=0 failed=0

192.168.101.78 : ok=4 changed=0 unreachable=0 failed=0

9.查看zabbix客户端状态

[root@centos78 ansible]# ansible zabbix -m shell -a "ps -ef |grep zabbix\_agentd"

192.168.101.78 | SUCCESS | rc=0 >>

zabbix 11785 1 0 22:33 ? 00:00:00 /usr/local/zabbix/sbin/zabbix\_agentd

zabbix 11787 11785 0 22:33 ? 00:00:00 /usr/local/zabbix/sbin/zabbix\_agentd: collector [idle 1 sec]

zabbix 11788 11785 0 22:33 ? 00:00:00 /usr/local/zabbix/sbin/zabbix\_agentd: listener #1 [waiting for connection]

zabbix 11789 11785 0 22:33 ? 00:00:00 /usr/local/zabbix/sbin/zabbix\_agentd: listener #2 [waiting for connection]

zabbix 11790 11785 0 22:33 ? 00:00:00 /usr/local/zabbix/sbin/zabbix\_agentd: listener #3 [waiting for connection]

zabbix 11791 11785 0 22:33 ? 00:00:00 /usr/local/zabbix/sbin/zabbix\_agentd: active checks #1 [idle 1 sec]

root 17136 17131 0 23:52 pts/1 00:00:00 /bin/sh -c ps -ef |grep zabbix\_agentd

root 17138 17136 0 23:52 pts/1 00:00:00 grep zabbix\_agentd

192.168.101.75 | SUCCESS | rc=0 >>

zabbix 21448 1 0 22:20 ? 00:00:00 /usr/local/zabbix/sbin/zabbix\_agentd

zabbix 21450 21448 0 22:20 ? 00:00:01 /usr/local/zabbix/sbin/zabbix\_agentd: collector [idle 1 sec]

zabbix 21451 21448 0 22:20 ? 00:00:01 /usr/local/zabbix/sbin/zabbix\_agentd: listener #1 [waiting for connection]

zabbix 21452 21448 0 22:20 ? 00:00:01 /usr/local/zabbix/sbin/zabbix\_agentd: listener #2 [waiting for connection]

zabbix 21453 21448 0 22:20 ? 00:00:01 /usr/local/zabbix/sbin/zabbix\_agentd: listener #3 [waiting for connection]

zabbix 21454 21448 0 22:20 ? 00:00:00 /usr/local/zabbix/sbin/zabbix\_agentd: active checks #1 [idle 1 sec]

root 23645 23640 0 23:52 pts/1 00:00:00 /bin/sh -c ps -ef |grep zabbix\_agentd

root 23647 23645 0 23:52 pts/1 00:00:00 grep zabbix\_agentd


总结

使用ansible批量安装zabbix客户端,非常快就能完成大批量的安装。安装时候一定要注意,批量安装时,客户端基本组件也要安装好,比如make等等。

你可能感兴趣的:(IT文库专栏,ansible,zabbix)