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

#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<

    4.0
    ${TIME}
    
        
            db
        
    
    
        
    
    
EOF

#06.循环生成触发器
for i in $(cat trigger_list.txt)
do
cat >>${DIR}/zabbix_template.xml<
            $(echo ${i}|awk -F"," '{print $1}')
            0
            
            $(echo ${i}|awk -F"," '{print $2}')
            0
            
            
            0
            $(echo ${i}|awk -F"," '{print $3}')
            
            0
            0
            
            
        
EOF
done

#07.生成固定格式
cat >>${DIR}/zabbix_template.xml<

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

你可能感兴趣的:(ansible批量管理zabbix)