第1章 目录结构
1.功能说明
1.批量安装zabbix客户端
2.批量更新客户端配置文件
3.批量创建/更新/删除主机
4.批量创建/更新/删除组
5.批量创建监控项
6.自定义模版文件并导入
2.目录结构
角色目录:
[root@m01 ~]
执行脚本目录:
[root@m01 ~]
3.主机清单
[root@m01 ~]
第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'
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
#########################################################################
# File Name: auto_template.sh
# Author: zhangya
# mail: 526195417@qq.com
# Created Time: 2020-04-04 15:35:24
# Description: 自动移生成zabbix监控模版xml文件
#########################################################################
#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
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>4.0</version>
<date>${TIME}</date>
<groups>
<group>
<name>db</name>
</group>
</groups>
<templates>
<template>
<template>TCP</template>
<name>TCP</name>
<description/>
<groups>
<group>
<name>Linux servers</name>
</group>
</groups>
<applications>
<application>
<name>TCP</name>
</application>
<application>
<name>新的应用集</name>
</application>
</applications>
<items>
EOF
#4.循环生成监控项内容
for i in $(cat item_list.txt)
do
cat >>${DIR}/zabbix_template.xml<<EOF
<item>
<name>$(echo ${i}|awk -F"," '{print $1}')</name>
<type>0</type>
<snmp_community/>
<snmp_oid/>
<key>$(echo ${i}|awk -F"," '{print $2}')</key>
<delay>30s</delay>
<history>90d</history>
<trends>365d</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<params/>
<ipmi_sensor/>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>$(echo ${i}|awk -F"," '{print $3}')</name>
</application>
<application>
<name>新的应用集</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<preprocessing/>
<jmx_endpoint/>
<timeout>3s</timeout>
<url/>
<query_fields/>
<posts/>
<status_codes>200</status_codes>
<follow_redirects>1</follow_redirects>
<post_type>0</post_type>
<http_proxy/>
<headers/>
<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</