ansible实现zabbix agent批量部署

发布时间 2023-10-08 09:35:43作者: 小糊涂90

1.安装ansible在zabbix-server上

#1.安装ansible在zabbix-server上
root@zabbix-server ~]#yum install -y ansible
#选择yum安装,安装好源,直接安装会出现下面的问题原因是yum源里的版本低于2.12,需要配置源
root@zabbix-server ~]#mv CentOS-AppStream.repo CentOS-AppStream.repo.back
root@zabbix-server ~]#cat /etc/yum.repos.d/CentOS-Stream.repo 
[base]
name=CentOS-8-stream - Base - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos/8-stream/BaseOS/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

#additional packages that may be useful
[extras]
name=CentOS-8-stream - Extras - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos/8-stream/extras/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-8-stream - Plus - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos/8-stream/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

[PowerTools]
name=CentOS-8-stream - PowerTools - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos/8-stream/PowerTools/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

[AppStream]
name=CentOS-8-stream - AppStream - mirrors.aliyun.com
baseurl=https://mirrors.aliyun.com/centos/8-stream/AppStream/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official

#安装epel源
[root@zabbix-server ~]#yum install -y epel-release

#清除缓存
[root@zabbix-server ~]#yum clean all

#安装ansible
[root@zabbix-server ~]#yum install -y ansible

2.配置主机清单及变量hostname,后期agent配置文件模板需要用到变量

[root@zabbix-server ~]#tail -n4 /etc/ansible/hosts
[zabbix-agent]
10.0.0.160 hostname=10.0.0.160
10.0.0.170 hostname=10.0.0.170
10.0.0.180 hostname=10.0.0.180

3.创建角色目录

[root@zabbix-server ~]#mkdir -p /etc/ansible/roles/zabbix-agent/{files,templates,tasks}
[root@zabbix-server ~]#tree /etc/ansible/roles/zabbix-agent
/etc/ansible/roles/zabbix-agent
├── files
├── tasks
└── templates

4.在files目录下创建yum的源

[root@zabbix-server ~]#cd /etc/ansible/roles/zabbix-agent/files/
[root@zabbix-server files]#cat zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/6.0/rhel/8/$basearch/
enabled=1
gpgcheck=0

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/8/$basearch/
enabled=1
gpgcheck=0

5.在templates目录下,cp配置文件,设置变量。

[root@zabbix-server files]#cp /etc/zabbix/zabbix_agentd.conf  /etc/ansible/roles/zabbix-agent/templates/zabbix_agentd.conf.j2

[root@zabbix-server templates]#grep "^[a-Z]" zabbix_agentd.conf.j2
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.0.150
ServerActive=10.0.0.150
Hostname={{ hostname }}
Include=/etc/zabbix/zabbix_agentd.d/*.conf

6.在tasks目录下,创建main.yml文件

[root@zabbix-server templates]#cd ../tasks/

[root@zabbix-server tasks]#vim main.yml

[root@zabbix-server tasks]#cat main.yml
- name: copy the zabbix-agent
  copy:
    src: zabbix.repo
    dest: /etc/yum.repos.d/
- name: install the zabbix-agent
  yum:
    name: zabbix-agent
    state: present
- name: copy the zabbix_agentd.conf
  template:
    src: zabbix_agentd.conf.j2
    dest: /etc/zabbix/zabbix_agentd.conf
- name: start zabbix-agent
  service:
    name: zabbix-agent
    state: started
    enabled: true

7.配置ansible服务器到其他服务器的免密登录

[root@zabbix-server ~]#ssh-keygen #三次回车
[root@zabbix-server ~]#ssh-copy-id 10.0.0.160
[root@zabbix-server ~]#ssh-copy-id 10.0.0.170
[root@zabbix-server ~]#ssh-copy-id 10.0.0.180

8.创建playbook

[root@zabbix-server ~]#vim /etc/ansible/roles/zabbix-agent/zabbix-agent.yml
[root@zabbix-server ~]#
[root@zabbix-server ~]#cat /etc/ansible/roles/zabbix-agent/zabbix-agent.yml
---
- name: zabbix_install
  hosts: zabbix-agent
  roles:
  - zabbix-agent

9.执行playbook

[root@zabbix-server ~]#ansible-playbook  /etc/ansible/roles/zabbix-agent/zabbix-agent.yml

10.到其他主机上验证agent运行状态及配置文件

[root@centos8 ~]#hostname -I
10.0.0.160 

[root@centos8 ~]#systemctl status zabbix-agent.service
● zabbix-agent.service - Zabbix Agent
   Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2022-08-31 22:35:24 CST; 12s ago
  Process: 32951 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
 Main PID: 32953 (zabbix_agentd)
    Tasks: 6 (limit: 12106)
   Memory: 4.1M
   CGroup: /system.slice/zabbix-agent.service
           ├─32953 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
           ├─32954 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
           ├─32955 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
           ├─32956 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
           ├─32957 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
           └─32958 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]

Aug 31 22:35:24 centos8.magedu.org systemd[1]: Starting Zabbix Agent...
Aug 31 22:35:24 centos8.magedu.org systemd[1]: Started Zabbix Agent.
[root@centos8 ~]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.0.150
ServerActive=10.0.0.150
Hostname=10.0.0.160
Include=/etc/zabbix/zabbix_agentd.d/*.conf