Ansible是一种简单的自动化语言,可以完美地描述IT应用程序基础结构。它易于学习,自我记录,并且不需要毕业级的计算机科学学位来阅读。自动化不应该比它正在取代的任务更复杂。
今天给大家介绍一个ansible-playbook基于role的配置一键安装zabbix客户端以及拉取自定义监控脚本;
先看看role的目录结构:
[root@Dac_Auto playbooks]# tree roles/zabbix/
roles/zabbix/
├── files
│ ├── check_Mysql_custom.py
│ └── zabbix
├── handlers
│ └── main.yml
├── tasks
│ └── main.yml
└── templates
├── check_zombie.conf
├── FPM_parameter.conf
├── Mysql_Custom.conf
├── Mysql_repl.conf
├── Supervisor_parameter.conf
└── userparameter_mysql.conf
在files目录下存放需要拉取的文件,我们这里放了一个python文件与zabbix客户端的启动脚本(因为之前遇到一个版本问题所以写了一个基于centos7和6通用的脚本)
[root@Dac_Auto files]# ls
check_Mysql_custom.py zabbix
[root@Dac_Auto files]# cat check_Mysql_custom.py #!/usr/local/bin/python '''author = chenmingle''' '''Description:get mysql status''' import os import sys try: import MySQLdb as mysql except Exception, e: print e print "pip install MySQL-python" sys.exit(1) con = mysql.connect(user='root',passwd='',) def processlist(): cur = con.cursor() sql1 = 'show processlist' a = str(cur.execute(sql1)) print a def slave_status(): cur = con.cursor() sql2 = cur.execute('show status like "%Slave_running%";') status2 = str(cur.fetchall()) check2 = status2.split("'")[3] if check2 == 'ON': print 0 else: print 1 def show_status(type): cur = con.cursor() b = cur.execute('show status like "%s";' %(type)) for i in cur.fetchall(): cat = str(i) check = str(cat.split("'")[3]) print check def main(type): if type == 'processlist': processlist() elif type == 'slave_status': slave_status() else: show_status(type) if __name__ == '__main__': try: type = sys.argv[1] except Exception, e: print "Usage: python %s type" % sys.argv[0] sys.exit(1) main(type)
[root@Dac_Auto files]# cat zabbix #!/bin/sh #chkconfig: 2345 20 80 check_release=`cat /etc/redhat-release | awk '{print $NF}'` start(){ if [ $check_release == "(Core)" ]; then echo "start zabbix-agent" systemctl start zabbix-agent touch /tmp/zabbix-agent.pid else echo "start zabbix-agent" /etc/init.d/zabbix-agent start touch /tmp/zabbix-agent.pid fi } stop(){ if [ $check_release == "(Core)" ]; then echo "stopping zabbix-agent" systemctl stop zabbix-agent ps -ef | grep zabbix_agentd | grep -v grep | awk '{print $2}' | xargs kill -9 1 >/dev/null 2>&1 touch /tmp/zabbix-agent.pid else echo "stopping zabbix-agent" /etc/init.d/zabbix-agent stop touch /tmp/zabbix-agent.pid fi if [ $? == 0 ]; then rm -rf /tmp/zabbix-agent.pid echo "success stop zabbix-agent" else echo "check your zabbix-agent command" exit 10 fi } status(){ if [ $check_release == "(Core)" ]; then systemctl status zabbix-agent else /etc/init.d/zabbix-agent status fi } case $1 in start) start ;; stop) stop ;; reload|restart) start stop start ;; status) status ;; *) echo "Usage:$0{start|stop|reload/restart}" ;; esac
接下来介绍templates目录我们这里放的是自定义监控配置文件:
[root@Dac_Auto zabbix]# cd templates/ [root@Dac_Auto templates]# ls FPM_parameter.conf Mysql_Custom.conf [root@Dac_Auto templates]# cat Mysql_Custom.conf UserParameter=mysql.Custom[*],/usr/local/bin/python /home/python/check_Mysql_custom.py $1
然后在handlers目录下写一个触发的重启项:
[root@Dac_Auto zabbix]# cd handlers/ [root@Dac_Auto handlers]# ls main.yml [root@Dac_Auto handlers]# cat main.yml - name: restart zabbix-agent service: name: zabbix-agent state: restarted - name: restart zabbix service: name: zabbix state: restarted
最后在tasks目录下编写安装步骤:
[root@Dac_Auto zabbix]# cd tasks/ [root@Dac_Auto tasks]# cat main.yml - name: install pip for centos7 yum: name: "{{ item }}" state: present with_items: - python2-pip when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7" - name: install pip for centos6 yum: name: "{{ item }}" state: present with_items: - python-pip when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6" - name: Install mysql Packages yum: name: "{{ item }}" state: present with_items: - gcc - python-devel - pip: name: MySQL-python - name: Set Zabbix-agent /etc/init.d/zabbix copy: src: zabbix dest: /etc/init.d/zabbix mode: 0755 notify: restart zabbix - name: Set templates template: src: "{{ project_name }}.conf" dest: /etc/zabbix/zabbix_agentd.d/{{ project_name }}.conf notify: restart zabbix - name: Copy check_Mysql_custom.py to /home/python/check_Mysql_custom.py copy: src: check_Mysql_custom.py dest: /home/python/check_Mysql_custom.py mode: 0755
解析一下:
1、when的那句作用是检测主机的操作系统版本来决定执行步骤
2、template配置的{{ project_name }}的作用是基于不同的监控项目我就可以在执行是定义想要的监控项
最后在role同级目录下编写一个yml脚本
[root@Dac_Auto playbooks]# pwd /home/python/playbooks [root@Dac_Auto playbooks]# cat zabbix_templates.yml - hosts: testserver roles: - { role: zabbix, project_name: 'Mysql_Custom' }
ansible-playbook基于role的配置一键安装zabbix客户端以及拉取自定义监控脚本
原文地址:http://blog.51cto.com/legehappy/2113988