天天看點

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

        在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

└── 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' }      

繼續閱讀