基於ansible的zabbix源代碼安裝
基於ansible的zabbix源代碼安裝
1、ansible簡介
ansible是新出現的自動化運維工具,基於Python開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。ansible是基於模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運行的模塊,ansible只是提供一種框架。我們現在通過ansible來實現zabbix的批量安裝。
zabbix我就不多說了,現在是一門比較熱的監控軟件。
[root@node1 ~]# clear [root@node1 ~]# cd /usr/local/src/ 先安裝lrzsz的軟件包,然後上傳zabbix-3.2.7.tar.gz,zabbix壓縮包可在官網下載 [root@node1 src]# rz [root@node1 src]# ls apr-1.6.2 apr-util-1.6.0.tar.gz httpd-2.4.27.tar.gz shuzu.sh while_ping.sh apr-1.6.2.tar.bz2 a.sh nginx-1.12.0 test_hangshu.sh zabbix-3.2.7.tar.gz apr-util-1.6.0 httpd-2.4.27 nginx-1.12.0.tar.gz while_login.sh [root@node1 src]#
接下來我們是基於ansible自動化源碼安裝zabbix,所以我們先安裝ansible
[root@node1 src]# yum install -y ansible
ansible的應用需要使用ssh-keygen生成私鑰和公鑰
生成公鑰
[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 | | | +-----------------+
生成客戶端的密鑰
[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 [email protected]‘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
服務端安裝:
編寫ansible文件
[root@node2 ~]# cat /etc/ansible/install_zabbix.yaml - hosts: webservers remote_user: root tasks: - name: install packages yum: name={{ item }} state=latest with_items: ###搭建lamp環境,安裝環境依賴的包 - make - mysql-server - httpd - php - mysql-devel - gcc - net-snmp-devel - curl-devel - perl-DBI - php-gd - php-mysql - php-bcmath - php-mbstring - php-xml - unixODBC-devel - OpenIPMI-devel - libxml2-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.sh dest=/tmp/install_zabbix.sh notify: install shell handlers: - name: install shell shell: /bin/bash /tmp/install_zabbix.sh
zabbix源代碼安裝腳本
[root@node1 php]# cat /usr/local/src/install_zabbix.sh #!/bin/bash useradd zabbix -s /sbin/nologin expect -c " spawn /usr/bin/mysql_secure_installation expect \"Enter current password for root (enter for none):\" send \"\r\" expect \"Set root password?\" send \"y\r\" expect \"New password:\" send \"zabbix\r\" expect \"Re-enter new password:\" send \"zabbix\r\" expect \"Remove anonymous users?\" send \"y\r\" expect \"Disallow root login remotely?\" send \"n\r\" expect \"Remove test database and access to it?\" send \"y\r\" expect \"Reload privilege tables now?\" send \"y\r\" expect eof " mysql -uroot -pzabbix -e "create database zabbix character set utf8 collate utf8_bin;" mysql -uroot -pzabbix -e "grant all on zabbix.* to zabbix@localhost identified by ‘zabbix‘;" mysql -uroot -pzabbix -e "flush privileges;" cd /usr/local/src/ tar zxvf zabbix-3.2.7.tar.gz cd /usr/local/src/zabbix-3.2.7 cd database/mysql/ mysql -uzabbix -pzabbix zabbix < schema.sql mysql -uzabbix -pzabbix zabbix < images.sql mysql -uzabbix -pzabbix zabbix < data.sql ./configure --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --with-openipmi --with-unixodbc --prefix=/usr/local/zabbix make && make install sed -i ‘s/DirectoryIndex index.html index.html.var/DirectoryIndex index.html index.php/g‘ /etc/httpd/conf/httpd.conf sed -i ‘s/# DBPassword=/DBPassword=zabbix/g‘ /usr/local/zabbix/etc/zabbix_server.conf cp misc/init.d/fedora/core5/zabbix_server /etc/init.d/ 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/ZABBIX_BIN="/usr/local/sbin/zabbix_server"/ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_server"/g‘ /etc/init.d/zabbix_server /etc/init.d/zabbix_server start /etc/init.d/zabbix_agentd start chkconfig zabbix_agentd on chkconfig zabbix_server on sed -i ‘s/max_execution_time = 30/max_execution_time = 300/g‘ /etc/php.ini sed -i ‘s/max_input_time = 60/max_input_time = 300/g‘ /etc/php.ini sed -i ‘s/;date.timezone =/date.timezone =Asia/Shanghai/g‘ /etc/php.ini sed -i ‘s/post_max_size = 8M/post_max_size = 32M/g‘ /etc/php.ini service httpd restart cd /usr/local/src/zabbix-3.2.7/frontends/ cp -rf php /var/www/html/zabbix /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini --daemonize
###寫完了腳本別忘
檢測ansible文件,沒問題
[root@node1 php]# ansible-playbook -C /etc/ansible/install_zabbix.yaml PLAY [webservers] ************************************************************************************** TASK [Gathering Facts] ********************************************************************************* ok: [172.25.0.32] TASK [install packages] ******************************************************************************** ok: [172.25.0.32] => (item=[u‘make‘, u‘mysql-server‘, u‘httpd‘, u‘php‘, u‘mysql-devel‘, u‘gcc‘, u‘net-snmp-devel‘, u‘curl-devel‘, u‘perl-DBI‘, u‘php-gd‘, u‘php-mysql‘, u‘php-bcmath‘, u‘php-mbstring‘, u‘php-xml‘, u‘unixODBC-devel‘, u‘OpenIPMI-devel‘, u‘libxml2-devel‘]) TASK [copy zabbix 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=4 changed=2 unreachable=0 failed=0
再執行一遍就可以安裝了
[root@node1 php]#ansible-playbook /etc/ansible/install_zabbix.yaml
2、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: 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,也可以寫成變量 chmod 700 /etc/init.d/zabbix_agentd sed -i ‘s/# UnsafeUserParameters=0/UnsafeUserParameters=1/g‘ /usr/local/zabbix/etc/zabbix_agentd.conf
在應用腳本文件時記得要改腳本的權限,不然就無法執行。
[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客戶端安裝了
本文出自 “我的運維” 博客,請務必保留此出處http://xiaozhagn.blog.51cto.com/13264135/1975084
基於ansible的zabbix源代碼安裝