基于ansible的zabbix源代码安装php
ansible简介node
ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优势,实现了批量系统配置、批量程序部署、批量运行命令等功能。ansible是基于模块工做的,自己没有批量部署的能力。真正具备批量部署的是ansible所运行的模块,ansible只是提供一种框架。咱们如今经过ansible来实现zabbix的批量安装。web
zabbix我就很少说了,如今是一门比较热的监控软件。shell
[root@node1 ~]# clear [root@node1 ~]# cd /usr/local/src/
先安装lrzsz的软件包,或者直接wget下载zabbix压缩包,我这里使用的是zabbix-3.2.7压缩包,zabbix压缩包可在官网下载,下载地址为vim
#wget https://ncu.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.2.7/zabbix-3.2.7.tar.gzbash
[root@node1 src]# ls zabbix-3.2.7.tar.gz
接下来咱们是基于ansible自动化源码安装zabbix,因此咱们先安装ansible框架
[root@node1 src]# yum install -y ansible
ansible的应用须要使用ssh-keygen生成私钥和公钥运维
生成公钥dom
[root@node1 src]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa already exists. Overwrite (y/n)? y Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: c6:4d:7a:b3:dc:96:12:81:a2:44:44:29:24:2f:3a:de root@node1 The key's randomart image is: +--[ RSA 2048]----+ |...o+. | | o... . | |. ... . . o | |.. . . o + . | |o . S = | |... . o = . | | . E + + | | o | | | +-----------------+
生成客户端的密钥ssh
[root@node1 src]# ssh-copy-id 172.25.0.32 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@172.25.0.32's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '172.25.0.32'" and check to make sure that only the key(s) you wanted were added.
修改ansible的hosts文件,针对指定客户端进行推送
进入hosts文件找到[webservers]把注释去掉,再添加客户端的ip
[root@node1 src]# cat /etc/ansible/hosts [webservers] ###这个能够随便定义 ##alpha.example.org ##beta.example.org ## 192.168.1.100 ## 192.168.1.110 172.25.0.32
接下来咱们开始配置ansible基于jinjia的模板,写个zabbix的基于ansible源代码安装文件
ansible的变量支持类型主要是如下几个
字符串:使用单引号或双引号;
数字:整数、浮点数;
列表:[item1, item2, ...]
元组:(item1, item2, ...)
字典:{key1:value1, key2:value2, ...}
布尔型:true/false
zabbix客户端安装:
方法一:
ansibleZ在zabbix的应用的比较重要的部分是推送客户端
编写ansible文件
[root@node2 ~]# cat /etc/ansible/install_zabbix.yaml - hosts: webservers ##若是你是大批量安装,把webservers 改为all,不过先添加密钥ssh-keygen+key-copy-id + 客户端ip remote_user: root tasks: - name: install packages yum: name={{ item }} state=latest with_items: - make - gcc -openssl-devel - pcre-devel - name: copy zabbix.tar to clien copy: src=/usr/local/src/zabbix-3.2.7.tar.gz dest=/usr/local/src/ - name: copy install_shell to clien copy: src=install_zabbix_client.sh dest=/tmp/install_zabbix_client.sh notify: install shell handlers: - name: install shell shell: /bin/bash /tmp/install_zabbix_client.sh
推送给客户端的脚本文件:
[root@node1 ansible]# cat install_zabbix_client.sh #!/bin/bash useradd zabbix -s /sbin/nologin cd /usr/local/src tar zxvf zabbix-3.2.7.tar.gz cd /usr/local/src/zabbix-3.2.7 ./configure --with-net-snmp --with-libcurl --enable-agent --prefix=/usr/local/zabbix make && make install cp misc/init.d/fedora/core5/zabbix_agentd /etc/init.d/ sed -i 's/ZABBIX_BIN="/usr/local/sbin/zabbix_agentd"/ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd"/g' /etc/init.d/zabbix_agentd sed -i 's/Server=127.0.0.1/Server=172.25.0.29/g' /usr/local/zabbix/etc/zabbix_agentd.conf ##这个ip我写死了172.25.0.29;是你的服务端ip,也能够写成变量 sed -i 's/ServerActive=127.0.0.1/ServerActive={{ server_ip }}/g' /usr/local/zabbix/etc/zabbix_agentd.conf sed -i 's/# UnsafeUserParameters=0/UnsafeUserParameters=1/g' /usr/local/zabbix/etc/zabbix_agentd.conf sed -i 's/# EnableRemoteCommands=0/EnableRemoteCommands=1/g' /usr/local/zabbix/etc/zabbix_agentd.conf sed -i 's/# ListenPort=10050/ListenPort=10050/g'/usr/local/zabbix/etc/zabbix_agentd.conf chmod 700 /etc/init.d/zabbix_agentd
在应用脚本文件时记得要改脚本的权限,否则就没法执行。
[root@node1 ansible]# chmod a+x install_zabbix_client.sh [root@node1 php]# ansible-playbook -C /etc/ansible/install_zabbix_client.yaml PLAY [webservers] ************************************************************************************** TASK [Gathering Facts] ********************************************************************************* ok: [172.25.0.32] TASK [copy zabbix.tar to clien] ************************************************************************ changed: [172.25.0.32] TASK [copy install_shell to clien] ********************************************************************* changed: [172.25.0.32] RUNNING HANDLER [install shell] ************************************************************************ skipping: [172.25.0.32] PLAY RECAP ********************************************************************************************* 172.25.0.32 : ok=3 changed=2 unreachable=0 failed=0
测试没问题后,之后这样就能够实现批量对zabbix客户端安装了
方法二:
如今咱们能够发现基于脚本安装不够很好的检测安装时错误状况,接下来咱们直接在ansible里面写安装命令,这样以便咱们检错,这个方法我的比较推荐:
[root@node1 ansible]# cat zabbix_install.yaml -hosts: webservers 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: yum yum: name={{ item }} state=latest with_items: - make - gcc - curl - curl-devel - pcre-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: shuoquan 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;sed -i 's/ServerActive=127.0.0.1/ServerActive={{ server_ip }}/g' /usr/local/zabbix/etc/zabbix_agentd.conf;sed -i 's/# UnsafeUserParameters=0/UnsafeUserParameters=1/g' /usr/local/zabbix/etc/zabbix_agentd.conf;sed -i 's/# EnableRemoteCommands=0/EnableRemoteCommands=1/g' /usr/local/zabbix/etc/zabbix_agentd.conf;sed -i 's/# ListenPort=10050/ListenPort=10050/g'/usr/local/zabbix/etc/zabbix_agentd.conf ###推送的时候咱们能够随时定义咱们的服务端ip - name: restart_server shell: /etc/init.d/zabbix_agentd restart
二、而后执行命令推送到客户端:推送到客户端而且建立:
[root@node1 ansible]#ansible-playbook -e server_ip=172.25.0.29 /etc/ansible/zabbix_install.yaml
我的感悟:以为经过ansible部署很好的解决一人管理上百台机器安装zabbix客户端的问题,并且是不只是zabbix安装,其它软件咱们也能够基于ansible来安装,来管理。