只有你學會把自己已有的成績都歸零,才能騰出空間去接納更多的新東西,如此才能使自己不斷的超越自己。
簡介
Ansible 是新出現的自動化運維工具,基于 Python 開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,實現了批量系統配置、批量程序部署、批量運行命令等功能。Ansible 是基于模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是 Ansible 所運行的模塊,Ansible 只是提供一種框架。主要包括:
(1)、連接插件 connection plugins:負責和被監控端實現通信;
(2)、host inventory:指定操作的主機,是一個配置文件里面定義監控的主機;
(3)、各種模塊核心模塊、command 模塊、自定義模塊;
(4)、借助于插件完成記錄日志郵件等功能;
(5)、playbook:劇本執行多個任務時,非必需可以讓節點一次性運行多個任務.
因為 Ansible 是基于 ssh 協議的,所以在此之前,我們需要在 Zabbix_server 端進行對其他主機實現免密登錄。
使用命令生成密鑰
[root@ansible ~]# ssh-keygen
將公鑰發送到所有安裝zabbix客戶端的主機
[root@ansible ~]# ssh-copy-id 192.168.163.170
[root@ansible ~]# ssh-copy-id 192.168.163.171
安裝 Ansible 軟件
[root@ansible ~]# yum install -y ansible
修改配置文件,將 Zabbix 客戶機添加進組,(在文末添加即可)
[root@ansible ~]# vim /etc/ansible/hosts
[webserver]
192.168.163.170
192.168.163.171
創建一個安裝zabbix客戶端的劇本
[root@ansible ansible]# vim /etc/ansible/install_zabbix.yaml
- hosts: webserver
remote_user: root
tasks:
- name: copy
copy: src=/usr/local/src/zabbix-3.2.7.tar.gz dest=/usr/local/src/zabbix-3.2.7.tar.gz
- name: tar
shell: cd /usr/local/src;tar xf zabbix-3.2.7.tar.gz
- name: useradd
shell: useradd zabbix -s /sbin/nologin
- name: yum
yum: name={{ item }} state=latest
with_items:
- make
- gcc
- curl
- curl-devel
- name: configure
shell: cd /usr/local/src/zabbix-3.2.7;./configure --with-net-snmp --with-libcurl --enable-agent --prefix=/usr/local/zabbix;make && make install
- name: script
shell: cp /usr/local/src/zabbix-3.2.7/misc/init.d/fedora/core5/zabbix_agentd /etc/init.d/
- name: quanxian
shell: chmod 700 /etc/init.d/zabbix_agentd
- name: vim zabbix_agent
shell: sed -i 's/ZABBIX_BIN="\/usr\/local\/sbin\/zabbix_agentd"/ZABBIX_BIN="\/usr\/local\/zabbix\/sbin\/zabbix_agentd"/g' /etc/init.d/zabbix_agentd
- name: vim_conf
shell: sed -i 's/Server=127.0.0.1/Server={{ server_ip }}/g' /usr/local/zabbix/etc/zabbix_agentd.conf
- name: restart_server
shell: /etc/init.d/zabbix_agentd restart
執行該劇本
[root@ansible ansible]# ansible-playbook -e server_ip=192.168.163.169 install_zabbix.yaml
注意:-e 選項指定的 server_ip 是文件里的 server_ip,也就是客戶端所有指定的Zabbix_server 的 IP
輸出結果:
[root@localhost ansible]# ansible-playbook install_zabbix.yaml
PLAY [webserver] ********************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [192.168.163.171]
ok: [192.168.163.170]
TASK [copy] *************************************************************************************************************
changed: [192.168.163.170]
changed: [192.168.163.171]
TASK [tar] **************************************************************************************************************
changed: [192.168.163.170]
changed: [192.168.163.171]
TASK [useradd] **********************************************************************************************************
changed: [192.168.163.170]
changed: [192.168.163.171]
TASK [yum] **************************************************************************************************************
^CProcess WorkerProcess-10:
Traceback (most recent call last):
File "/usr/lib64/python2.7/multiprocessing/process.py", line 258, in _bootstrap
Process WorkerProcess-9:
Traceback (most recent call last):
File "/usr/lib64/python2.7/multiprocessing/process.py", line 258, in _bootstrap
[ERROR]: User interrupted execution
[root@localhost ansible]# ansible-playbook -e server_ip=192.168.163.169 install_zabbix.yaml
PLAY [webserver] ********************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [192.168.163.171]
ok: [192.168.163.170]
TASK [copy] *************************************************************************************************************
changed: [192.168.163.170]
changed: [192.168.163.171]
TASK [tar] **************************************************************************************************************
changed: [192.168.163.171]
changed: [192.168.163.170]
TASK [useradd] **********************************************************************************************************
changed: [192.168.163.170]
changed: [192.168.163.171]
TASK [yum] **************************************************************************************************************
ok: [192.168.163.171] => (item=[u'make', u'gcc', u'curl', u'curl-devel'])
ok: [192.168.163.170] => (item=[u'make', u'gcc', u'curl', u'curl-devel'])
TASK [configure] ********************************************************************************************************
changed: [192.168.163.171]
changed: [192.168.163.170]
TASK [script] ***********************************************************************************************************
changed: [192.168.163.170]
changed: [192.168.163.171]
TASK [quanxian] *********************************************************************************************************
[WARNING]: Consider using file module with mode rather than running chmod
changed: [192.168.163.170]
changed: [192.168.163.171]
TASK [vim zabbix_agent] *************************************************************************************************
[WARNING]: Consider using template or lineinfile module rather than running sed
changed: [192.168.163.170]
changed: [192.168.163.171]
TASK [vim_conf] *********************************************************************************************************
changed: [192.168.163.171]
changed: [192.168.163.170]
TASK [restart_server] ***************************************************************************************************
changed: [192.168.163.170]
changed: [192.168.163.171]
PLAY RECAP **************************************************************************************************************
192.168.163.170 : ok=11 changed=9 unreachable=0 failed=0
192.168.163.171 : ok=11 changed=9 unreachable=0 failed=0
測試:
在 Zabbix 管理界面添加 Zabbix 客戶機
添加成功
監控到數據
實驗成功!
本文出自 “xhk__運維” 博客
出處:http://xhk777.blog.51cto.com/13405744/1975082