1. 程式人生 > >ansible-playbook基於role的配置一鍵安裝zabbix客戶端以及拉取自定義監控腳本

ansible-playbook基於role的配置一鍵安裝zabbix客戶端以及拉取自定義監控腳本

ansible基於role功能

在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 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客戶端以及拉取自定義監控腳本