[Ansible实战]-免交互批量管理Zabbix

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
#########################################################################
# 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</verify_peer>
                    <verify_host>0</verify_host>
                    <master_item/>
                </item>
EOF
done

#5.生成固定格式
cat >>${DIR}/zabbix_template.xml<<EOF
            </items>
            <discovery_rules/>
            <httptests/>
            <macros/>
            <templates/>
            <screens/>
        </template>
    </templates>
    <triggers>
EOF

#06.循环生成触发器
for i in $(cat trigger_list.txt)
do
cat >>${DIR}/zabbix_template.xml<<EOF
        <trigger>
            <expression>$(echo ${i}|awk -F"," '{print $1}')</expression>
            <recovery_mode>0</recovery_mode>
            <recovery_expression/>
            <name>$(echo ${i}|awk -F"," '{print $2}')</name>
            <correlation_mode>0</correlation_mode>
            <correlation_tag/>
            <url/>
            <status>0</status>
            <priority>$(echo ${i}|awk -F"," '{print $3}')</priority>
            <description/>
            <type>0</type>
            <manual_close>0</manual_close>
            <dependencies/>
            <tags/>
        </trigger>
EOF
done

#07.生成固定格式
cat >>${DIR}/zabbix_template.xml<<EOF
    </triggers>
</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
老男孩Linux教育欢迎咨询免费试听 联系方式: QQ:526195417
系统化运维博客
原文地址:https://www.cnblogs.com/nsthink-666/p/12744728.html