在IT工作中,您可能会一遍又一遍地执行相同的任务;没有人喜欢重复的任务。通过Ansible,IT管理员可以开始自动化日常任务中的苦差事。自动化解放了管理人员,专注于通过加快应用交付时间和建立在成功文化基础之上,为业务提供更多价值的努力。最终,Ansible为团队提供了他们永远无法获得足够的一件事:时间。让聪明的人专注于聪明的事情。
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 osimport systry: import MySQLdb as mysqlexcept 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 adef 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 1def 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 checkdef 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 80check_release=`cat /etc/redhat-release | awk '{print $NF}'`start(){if [ $check_release == "(Core)" ]; thenecho "start zabbix-agent"systemctl start zabbix-agenttouch /tmp/zabbix-agent.pidelseecho "start zabbix-agent"/etc/init.d/zabbix-agent starttouch /tmp/zabbix-agent.pidfi}stop(){ if [ $check_release == "(Core)" ]; then echo "stopping zabbix-agent" systemctl stop zabbix-agentps -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 fiif [ $? == 0 ]; thenrm -rf /tmp/zabbix-agent.pidecho "success stop zabbix-agent"elseecho "check your zabbix-agent command"exit 10fi}status(){ if [ $check_release == "(Core)" ]; then systemctl status zabbix-agent else /etc/init.d/zabbix-agent status fi}case $1 instart)start;;stop)stop;;reload|restart)startstopstart;;status)status;;*)echo "Usage:$0{start|stop|reload/restart}";;esac
接下来介绍templates目录我们这里放的是自定义监控配置文件:
[root@Dac_Auto zabbix]# cd templates/[root@Dac_Auto templates]# lsFPM_parameter.conf Mysql_Custom.conf [root@Dac_Auto templates]# cat Mysql_Custom.confUserParameter=mysql.Custom[*],/usr/local/bin/python /home/python/check_Mysql_custom.py $1
然后在handlers目录下写一个触发的重启项:
[root@Dac_Auto zabbix]# cd handlers/[root@Dac_Auto handlers]# lsmain.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' }