基於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來安裝,來管理。