1. 程式人生 > >ansible自動部署 zabbix-agent 的模塊

ansible自動部署 zabbix-agent 的模塊

ansible自動化部署zabbix-agent客戶端

ansible自動部署 zabbix-agent 模塊 的準備階段

ansible所在的服務端可以免密鑰登錄所被部署的機器稱為客戶端。

免密鑰的做法

服務端 ssh-keygen 一路回車生成密鑰對

ssh-copy-id 指定IP 將公鑰發給指定的ip 即可

ssh-copy-id 192.168.1.18


下面紅色是代表文件或目錄 黑色字體代表是內容


使用了 roles 方法 整體的目錄結構是

/etc/ansible/zabbix-agent.yml

[[email protected] ansible]# pwd

/etc/ansible


/etc/ansibl/hosts

[[email protected]

/* */ ansible]# cat hosts


192.168.1.145 #centso 系統

192.168.1.147 # centos 系統 加入 ansible 的客戶端ip 本次示例中是這幾個 ip

192.168.1.148 #ubuntu 系統


[[email protected] ansible]# cat zabbix-agent.yml

---

- hosts: all

roles:

- zabbix-agent



/etc/ansible/roles


[[email protected] ansible]# ls

zabbix-agent


/etc/ansible/zabbix-agent



[[email protected] zabbix-agent]# ls

files handlers tasks templates


/etc/ansible/zabbix-agent/files


[[email protected] zabbix-agent]# ls files/

zabbix


/etc/ansible/zabbix-agent/handlers


[[email protected] zabbix-agent]# ls handlers/

main.yml


/etc/ansible/zabbix-agent/tasks



[[email protected] zabbix-agent]# ls tasks/

files.yml main.yml package.yml service.yml



/etc/ansible/zabbix-agnet/templates



[[email protected] zabbix-agent]# ls templates/

zabbix_agentd.conf



/etc/ansible/zabbix-agent/files/zabbix/conf.d


[[email protected] zabbix-agent]# ls files/zabbix/conf.d/ #zabbix-agent的配置文件 鍵值

impression.conf imsecret.conf mystar.conf resonance.conf tagme.conf userReg.conf


/etc/ansible/zabbix-agent/files/zabbix/scripts 要復制去客戶端的文件


[[email protected] zabbix-agent]# ls files/zabbix/scripts/ 下邊是zabbix 可執行的腳本用來監控

/etc/ansible/zabbix-agent/handlers/main.yml 這個是開啟zabbix-agent的


[[email protected] zabbix-agent]# cat handlers/main.yml

---

- name: start zabbix_agentd

service: name=zabbix-agentd state=started


/etc/ansible/zabbix-agent/tasks/files.yml 將 /etc/ansible/zabbix-agent/files/* 復制到客戶端的配置


[[email protected] zabbix-agent]# cat tasks/files.yml

---

- name: copy centos template conf file

template: src=zabbix_agentd.conf dest=/etc/

when: ansible_os_family == "RedHat" #判斷系統


- name: copy ubuntu template conf file

template: src=zabbix_agentd.conf dest=/etc/zabbix/

when: ansible_os_family == "Debian"


- name: crete conf.d and scripts

file: path={{ item }} state=directory owner=root group=root mode=0755

with_items:

- /etc/zabbix/scripts

- /etc/zabbix/conf.d

- /etc/zabbix/scripts1


- name: copy the scripts/file

copy: src=zabbix/scripts/ dest=/etc/zabbix/scripts/ mode=0755


- name: copy the scripts1/file

copy: src=zabbix/scripts1/ dest=/etc/zabbix/scripts1/ mode=0755


- name: copy the conf.d/file

copy: src=zabbix/conf.d/ dest=/etc/zabbix/conf.d/


/etc/ansible/zabbix-agent/tasks/main.yml 主的配置 在同級目錄下會先執行這個main


[[email protected] zabbix-agent]# cat tasks/main.yml

---

- include: ‘package.yml‘

- include: ‘files.yml‘

- include: ‘service.yml‘



/etc/ansible/zabbix-agent/tasks/package.yml 安裝zabbix-agent的配置


[[email protected] zabbix-agent]# cat tasks/package.yml

---

- name: useraddd the zabbix

user: name=zabbix


- name: install epel-release

yum: name=epel-release.noarch state=latest

when: ansible_os_family == "RedHat"


- name: install epel-release and zabbix_agent_package

yum: name={{ item }} state=present

with_items:

- zabbix22-agent-2.2.18-1.el6.x86_64

- zabbix22-2.2.18-1.el6.x86_64

when: ansible_os_family == "RedHat"


- name: apt-get install to ubuntu

apt: name={{ item }} state=present

with_items:

- zabbix-agent

when: ansible_os_family == "Debian"



/etc/ansible/zabbix-agent/tasks/service.yml 開啟腳本 萬無一失 2重開啟


[[email protected] zabbix-agent]# cat tasks/service.yml

---

- name: start zabbix-agentd

service: name=zabbix-agentd state=started

when: ansible_os_family == "RedHat"


- name: start ubuntu zabbix-agent

service: name=zabbix-agent state=started

when: ansible_os_family == "Debian"



/etc/ansible/zabbix-agent/templates/zabbix_agentd.conf 配置文件的模板放在這裏



[[email protected] zabbix-agent]# cat templates/zabbix_agentd.conf

PidFile=/var/run/zabbix/zabbix_agentd.pid

LogFile=/var/log/zabbix/zabbix_agentd.log

LogFileSize=100

Server=192.168.1.100 #zabbix 服務端所在的 ip

ServerActive=192.168.1.100 # zabbix-agent 主動模式 配置

Hostname={{ ansible_hostname }} #這個是系統變量

Include=/etc/zabbix/conf.d/

UnsafeUserParameters=1

# UserParameter 自定義的 監控項


具體執行

[[email protected] ansible]# pwd

/etc/ansible

[[email protected] ansible]# ansible-playbook zabbix-agent.yml

[[email protected] ansible]# ansible-playbook zabbix-agent.yml -vvvv

加 -vvv 可以看到更詳細的過程 其實是 沒必要的


本文出自 “手有余香” 博客,請務必保留此出處http://19941018.blog.51cto.com/11889001/1949589

ansible自動部署 zabbix-agent 的模塊