zabbix ansible
基于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
- 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
服务端安装:
编写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://www.bubuko.com/infodetail-2362194.html