您好,登錄后才能下訂單哦!
十三、使用Ansible批量安裝Zabbix Agent,并通過自動注冊添加Linux主機:
1、Ansible簡介:
Ansible是一款基于Python研發的開源自動化工具,實現了批量運行命令、批量部署程序、批量配置系統等功能。默認通過SSH協議(也可使用其它協議)進行遠程命令執行或下發配置,無需部署任何客戶端代理軟件(agentless)在被管控主機上,并可同時支持多臺主機并行管理。Ansible是基于模塊工作的,本身沒有批量部署的能力,真正具有批量部署的是Ansible所運行的模塊,Ansible只是提供一種框架。Ansible幫助文檔:https://docs.ansible.com/ansible/latest/index.html
2、演示環境:
IP | 操作系統 | 主機名 | 角色 |
192.168.0.120 | CentOS ? 7.7 x86_64 | zabbix-server | Zabbix ? Database、Zabbix ? Server、Zabbix ? Web、Zabbix ? Agent、Ansible主機 |
192.168.0.121 | CentOS ? 7.7 x86_64 | web01 | Zabbix ? Agent、被管控主機 |
192.168.0.122 | CentOS ? 7.7 x86_64 | db01 | Zabbix ? Agent、被管控主機 |
目標:zabbix-server節點通過Ansible自動配置web01和db01節點的防火墻、SELinux、系統時間、主機名,自動安裝、配置、啟動Zabbix Agent,最后通過Zabbix Web自動注冊功能批量添加Linux主機
3、zabbix-server節點準備工作:
(1)配置hosts文件:
# vim /etc/hosts
192.168.0.120 zabbix-server
192.168.0.121 web01
192.168.0.122 db01
(2)配置chrony服務端:
a、修改chrony.conf配置文件:
# yum -y install chrony
# mv /etc/chrony.conf{,.bak}
# vim /etc/chrony.conf,新增如下代碼:
# 指定上層NTP服務器為阿里云提供的公網NTP服務器
server ntp1.aliyun.com iburst minpoll 4 maxpoll 10
server ntp2.aliyun.com iburst minpoll 4 maxpoll 10
server ntp3.aliyun.com iburst minpoll 4 maxpoll 10
server ntp4.aliyun.com iburst minpoll 4 maxpoll 10
server ntp5.aliyun.com iburst minpoll 4 maxpoll 10
server ntp6.aliyun.com iburst minpoll 4 maxpoll 10
server ntp7.aliyun.com iburst minpoll 4 maxpoll 10
# 記錄系統時鐘獲得/丟失時間的速率至drift文件中
driftfile /var/lib/chrony/drift
# 如果系統時鐘的偏移量大于10秒,則允許在前三次更新中步進調整系統時鐘
makestep 10 3
# 啟用RTC(實時時鐘)的內核同步
rtcsync
# 只允許192.168.0網段的客戶端進行時間同步
allow 192.168.0.0/24
# 如果未能從阿里云提供的公網NTP服務器同步到時間,也允許將本地時間作為標準時間授時給其它客戶端
local stratum 10
# 指定包含NTP驗證密鑰的文件
keyfile /etc/chrony.keys
# 指定存放日志文件的目錄
logdir /var/log/chrony
# 讓chronyd在選擇源時忽略源的層級
stratumweight 0
# 禁用客戶端訪問的日志記錄
noclientlog
# 如果時鐘調整大于0.5秒,則向系統日志發送消息
logchange 0.5
說明:詳細指令參數可以使用命令# man chrony.conf查看
b、啟動chronyd:
# systemctl start chronyd
# systemctl status chronyd
# ps aux | grep chronyd
# ss -tunlp | grep chronyd
備注:123端口為NTP服務監聽端口,323端口為chrony服務監聽端口
c、配置開機自啟:# systemctl enable chronyd
d、查看時間同步源:# chronyc sources -v
說明:
120.25.115.20:ntp1.aliyun.com域名解析后的地址
203.107.6.88:ntp2.aliyun.com~ntp7.aliyun.com域名解析后的地址
e、查看時間同步源狀態:# chronyc sourcestats -v
(3)查看Python版本:# python -V
(4)還原至最初配置,刪除Zabbix Web中zabbix-server以外的所有節點:
4、web01和db01節點為VMware Workstation最小化全新安裝的CentOS 7.7
5、zabbix-server節點安裝ansible:
# yum -y install epel-release
# yum -y install ansible
# ansible --version
6、zabbix-server節點配置被管控主機的主機清單文件:
# vim /etc/ansible/hosts,末尾新增如下代碼:
[websrvs]
web01 ansible_host=192.168.0.121
[dbsrvs]
db01 ansible_host=192.168.0.122
7、?? zabbix-server節點配置SSH互信:
(1)生成密鑰對,基于密鑰認證:# ssh-keygen -t rsa -P ""
(2)復制公鑰至所有被管控主機:
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.121
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.122
(3)測試連通性:# ansible all -m ping
8、zabbix-server節點創建roles相關目錄結構:
# cd /etc/ansible/roles
# mkdir -pv {prepare,zabbix-agent}/{files,templates,tasks,handlers,vars,meta,defaults}
9、zabbix-server節點配置prepare role:
(1)修改prepare/tasks/main.yml配置文件:
# vim prepare/tasks/main.yml
- name: Stop Iptables On CentOS 6
service: name=iptables state=stopped enabled=no
when: ansible_distribution=="CentOS" and ansible_distribution_major_version=="6"
- name: Stop Firewalld On CentOS 7
systemd: name=firewalld.service state=stopped enabled=no
when: ansible_distribution=="CentOS" and ansible_distribution_major_version=="7"
- name: Install libselinux-python
yum: name=libselinux-python state=latest
- name: Stop SELinux
selinux: state=disabled
- name: Set Hostname
hostname: name={{inventory_hostname}}
- name: Edit Hosts File
lineinfile: path=/etc/hosts line="{{ansible_host}} {{inventory_hostname}}" state=present backup=yes
- name: Install {{item}}
yum: name={{item}} state=latest
loop:
- epel-release
- chrony
- name: Install Configuration File
copy: src=chrony.conf dest=/etc/ owner=root group=root mode=0644 backup=yes
notify: Restart Chrony Service
tags: Chrony Configuration File
- name: Start Chrony Service
service: name=chronyd state=started enabled=yes
(2)修改prepare/files/chrony.conf配置文件:
# vim prepare/files/chrony.conf
server 192.168.0.120 iburst
driftfile /var/lib/chrony/drift
makestep 10 3
rtcsync
local stratum 10
keyfile /etc/chrony.keys
logdir /var/log/chrony
stratumweight 0
noclientlog
logchange 0.5
備注:192.168.0.120為內網chrony服務端IP
(3)修改prepare/handlers/main.yml配置文件:
# vim prepare/handlers/main.yml
- name: Restart Chrony Service
service: name=chronyd state=restarted
10、zabbix-server節點配置zabbix-agent role:
(1)修改zabbix-agent/tasks/main.yml配置文件:
# vim zabbix-agent/tasks/main.yml
- name: Create Zabbix Repository
yum_repository: file=zabbix name=aliyun-zabbix description="Aliyun Zabbix Repository" baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/$basearch/ gpgcheck=no enabled=yes owner=root group=root mode=0644 state=present
- name: Install zabbix-agent
yum: name=zabbix-agent state=latest
- name: Install Configuration File
template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf owner=root group=root mode=0644 backup=yes
notify: Restart zabbix-agent Service
tags: zabbix-agent Configuration File
- name: Start zabbix-agent Service
service: name=zabbix-agent state=started enabled=yes
說明:
yum_repository: file=zabbix name=aliyun-zabbix description="Aliyun Zabbix Repository" baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/$basearch/ gpgcheck=no enabled=yes owner=root group=root mode=0644 state=present
對應的/etc/yum.repos.d/zabbix.repo
[aliyun-zabbix]
name=Aliyun Zabbix Repository
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/$basearch/
enabled=1
gpgcheck=0
(2)修改zabbix-agent/handlers/main.yml配置文件:
# vim zabbix-agent/handlers/main.yml
- name: Restart zabbix-agent Service
service: name=zabbix-agent state=restarted
(3)復制zabbix-server節點的zabbix_agentd.conf配置文件,并修改成zabbix_agentd.conf.j2通用模板文件:
# cp /etc/zabbix/zabbix_agentd.conf /etc/ansible/roles/zabbix-agent/templates/zabbix_agentd.conf.j2
# vim /etc/ansible/roles/zabbix-agent/templates/zabbix_agentd.conf.j2
修改前 | 修改后 |
Server=192.168.0.120 | Server={{zabbix_server}} |
ListenPort=10050 | ListenPort={{listen_port}} |
ListenIP=192.168.0.120 | ListenIP={{ansible_host}} |
ServerActive=192.168.0.120 | ServerActive={{zabbix_server}} |
Hostname=zabbix-server | Hostname={{inventory_hostname}} |
# HostMetadata= | HostMetadata={{inventory_hostname}} |
?(4)修改/etc/ansible/roles/zabbix-agent/vars/main.yml配置文件:
# vim /etc/ansible/roles/zabbix-agent/vars/main.yml
zabbix_server: 192.168.0.120
listen_port: 10050
備注:不能有中橫杠,下劃線可以
11、zabbix-server節點查看roles目錄結構:
# yum -y install tree
# cd /etc/ansible
# tree
12、zabbix-server節點編寫playbook并執行:
# mkdir -pv /playbooks
# vim /playbooks/zabbix-agent.yml
- hosts: all
remote_user: root
roles:
- prepare
- zabbix-agent
# ansible-playbook --syntax-check /playbooks/zabbix-agent.yml
# ansible-playbook -C /playbooks/zabbix-agent.yml
# ansible-playbook /playbooks/zabbix-agent.yml
13、Zabbix Web中定義動作:
Configuration --> Actions --> Auto registration --> Create action --> Add
14、查看已添加主機:
Configuration --> Hosts
15、查看2個節點最新監控數據:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。