ansible批量管理zabbix
第1章 目錄結構
1.功能說明
1.批量安裝zabbix客戶端
2.批量更新客戶端配置文件
3.批量創建/更新/刪除主機
4.批量創建/更新/刪除組
5.批量創建監控項
6.自定義模版文件並導入
2.目錄結構
角色目錄:
[root@m01 ~]# tree /etc/ansible/roles/zabbix/
/etc/ansible/roles/zabbix/
├── create_group
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── create_host
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── del_host
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── get_groups
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── get_host
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── import_template
│ ├── files
│ │ └── zabbix_template.xml
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── init
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── zabbix.repo.j2
│ └── vars
├── update_conf
│ ├── files
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ │ └── zabbix_agentd.conf.j2
│ └── vars
└── update_item
├── files
│ ├── db
│ │ └── tcp_status.conf
│ └── web
│ └── tcp_status.conf
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
└── vars
執行腳本目錄:
[root@m01 ~]# tree /etc/ansible/zabbix/
/etc/ansible/zabbix/
├── 01_init.yaml
├── 02_update_conf.yaml
├── 03_get_host.yaml
├── 04_create_host.yaml
├── 05_del_host.yaml
├── 06_create_groups.yaml
├── 07_get_groups
├── 08_import_template
├── 09_update_item.yaml
└── auto_template
├── auto_template.sh
├── item_list.txt
└── trigger_list.txt
3.主機清單
[root@m01 ~]# cat /etc/ansible/hosts
[zabbix_web]
172.16.1.12
172.16.1.13
[zabbix_db]
172.16.1.51
172.16.1.52
[zabbix_all:children]
zabbix_web
zabbix_db
[zabbix_web:vars]
groups_name=“web”
template_1=“Template OS Linux”
template_2=“TCP”
[zabbix_db:vars]
groups_name=“db”
template_1=“Template OS Linux”
template_2=“TCP”
[zabbix_server]
172.16.1.11
第2章 角色內容
1.初始化清單
[root@m01 ~]# cat /etc/ansible/roles/zabbix/init/tasks/main.yaml
-
name: 01_copy_repo
template:
src: zabbix.repo.j2
dest: /etc/yum.repos.d/zabbix.repo -
name: 02_Install_Zabbix-agent
yum:
name: zabbix-agent
state: latest
update_cache: yes -
name: 03_Start_Zabbix-agent
systemd:
name: zabbix-agent
state: started
enabled: yes
2.創建組清單
[root@m01 ~]# cat /etc/ansible/roles/zabbix/create_group/tasks/main.yaml
- name: Create host groups
local_action:
module: zabbix_group
server_url: http://10.0.0.11/zabbix
login_user: Admin
login_password: zabbix
state: present
host_groups:
- “{{ groups_name }}”
3.創建主機清單
[root@m01 ~]# cat /etc/ansible/roles/zabbix/create_host/tasks/main.yaml
- name: 01_Create_host
local_action:
module: zabbix_host
server_url: http://10.0.0.11/zabbix
login_user: Admin
login_password: zabbix
host_name: “{{ ansible_nodename }}”
visible_name: “{{ ansible_nodename }}”
host_groups:
- “{{ groups_name }}”
link_templates:
- “{{ template_1 }}”
- “{{ template_2 }}”
status: enabled
state: present
inventory_mode: automatic
interfaces:
- type: 1
main: 1
useip: 1
ip: “{{ ansible_facts.eth1.ipv4.address }}”
dns: “”
port: 10050
4.刪除主機清單
[root@m01 ~]# cat /etc/ansible/roles/zabbix/del_host/tasks/main.yaml
- name: 01_del_host
local_action:
module: zabbix_host
server_url: http://10.0.0.11/zabbix
login_user: Admin
host_name: “{{ ansible_nodename }}”
login_password: zabbix
state: absent
5.獲取組列表清單
[root@m01 ~]# cat /etc/ansible/roles/zabbix/get_groups/tasks/main.yaml
-
name: get_groups
local_action:
module: zabbix_group_info
server_url: http://10.0.0.11/zabbix
login_user: Admin
login_password: zabbix
hostgroup_name:
- “{{ groups_name }}”
timeout: 10
register: group_status -
debug:
msg: “{{ group_status.host_groups }}”
6.獲取主機列表清單
[root@m01 ~]# cat /etc/ansible/roles/zabbix/get_host/tasks/main.yaml
- name: Get host info
local_action:
module: zabbix_host_info
server_url: http://10.0.0.11/zabbix
login_user: Admin
login_password: ‘zabbix’
#host_name: Zabbix server
host_ip: “{{ ansible_facts.eth1.ipv4.address }}”
timeout: 10
exact_match: no
remove_duplicate: yes
7.導入模版清單
[root@m01 ~]# cat /etc/ansible/roles/zabbix/import_template/tasks/main.yaml
- name: Import Zabbix templates from JSON
local_action:
module: zabbix_template
server_url: http://10.0.0.11/zabbix
login_user: Admin
login_password: zabbix
template_xml: “{{ lookup(‘file’, ‘zabbix_template.xml’)}}”
state: present
8.更新配置文件清單
[root@m01 ~]# cat /etc/ansible/roles/zabbix/update_conf/tasks/main.yaml
- name: 01_update_conf
template:
src: zabbix_agentd.conf.j2
dest: /etc/zabbix/zabbix_agentd.conf
notify:- restart zabbix-agent
9.更新監控項清單
[root@m01 ~]# cat /etc/ansible/roles/zabbix/update_item/tasks/main.yaml
- name: 01_update_item_conf
synchronize:
src: “/etc/ansible/roles/zabbix/update_item/files/{{ groups_name }}/”
dest: /etc/zabbix/zabbix_agentd.d
notify:- restart zabbix-agent
第3章 自動生成模版文件
1.自動生成腳本
[root@m01 /etc/ansible/zabbix/auto_template]# cat auto_template.sh
#!/bin/bash
#1.定義變量
TIME=$(date +%FT%H:%M:%SZ)
DIR=/etc/ansible/roles/zabbix/import_template/files
#2.清空文件內容
${DIR}/zabbix_template.xml
#3.生成第一段固定內容
cat >${DIR}/zabbix_template.xml<<EOF
<zabbix_export>
4.0
${TIME}
db
TCP
TCP
Linux servers
TCP
新的應用集
EOF
#4.循環生成監控項內容
for i in {DIR}/zabbix_template.xml<<EOF
$(echo ${i}|awk -F"," '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲')</name>
…(echo ${i}|awk -F"," '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')</key>
…(echo ${i}|awk -F"," ‘{print $3}’)
新的應用集
<jmx_endpoint/>
3s
<query_fields/>
<status_codes>200</status_codes>
<follow_redirects>1</follow_redirects>
<post_type>0</post_type>
<http_proxy/>
<retrieve_mode>0</retrieve_mode>
<request_method>0</request_method>
<output_format>0</output_format>
<allow_traps>0</allow_traps>
<ssl_cert_file/>
<ssl_key_file/>
<ssl_key_password/>
<verify_peer>0</verify_peer>
<verify_host>0</verify_host>
<master_item/>
EOF
done
#5.生成固定格式
cat >>${DIR}/zabbix_template.xml<<EOF
<discovery_rules/>
EOF
#06.循環生成觸發器
for i in {DIR}/zabbix_template.xml<<EOF
$(echo ${i}|awk -F"," '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 1}̲')</expression>…(echo ${i}|awk -F"," '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')</name>
…(echo ${i}|awk -F"," ‘{print $3}’)
0
<manual_close>0</manual_close>
EOF
done
#07.生成固定格式
cat >>${DIR}/zabbix_template.xml<<EOF
</zabbix_export>
EOF
2.監控項清單
[root@m01 ~]# cat /etc/ansible/zabbix/auto_template/item_list.txt
TCP_LISTEN,TCP_STATUS[LISTEN],TCP
TCP_ESTABLISHED,TCP_STATUS[ESTABLISHED],TCP
3.觸發器清單
[root@m01 ~]# cat /etc/ansible/zabbix/auto_template/trigger_list.txt
{TCP:TCP_STATUS[LISTEN].last()}=100,LISTEN連接數過多,2
{TCP:TCP_STATUS[ESTABLISHED].last()}=100,ESTABLISHED連接數過多,3