十3、使用Ansible批量安裝Zabbix Agent,並經過自動註冊添加Linux主機:html
一、Ansible簡介:python
Ansible是一款基於Python研發的開源自動化工具,實現了批量運行命令、批量部署程序、批量配置系統等功能。默認經過SSH協議(也可以使用其它協議)進行遠程命令執行或下發配置,無需部署任何客戶端代理軟件(agentless)在被管控主機上,並可同時支持多臺主機並行管理。Ansible是基於模塊工做的,自己沒有批量部署的能力,真正具備批量部署的是Ansible所運行的模塊,Ansible只是提供一種框架。Ansible幫助文檔:https://docs.ansible.com/ansible/latest/index.htmllinux
二、演示環境:web
IPvim |
操做系統服務器 |
主機名框架 |
角色less |
192.168.0.120ssh |
CentOS 7.7 x86_64ide |
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主機
三、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之外的全部節點:
四、web01和db01節點爲VMware Workstation最小化全新安裝的CentOS 7.7
五、zabbix-server節點安裝ansible:
# yum -y install epel-release
# yum -y install ansible
# ansible --version
六、zabbix-server節點配置被管控主機的主機清單文件:
# vim /etc/ansible/hosts,末尾新增以下代碼:
[websrvs]
web01 ansible_host=192.168.0.121
[dbsrvs]
db01 ansible_host=192.168.0.122
七、 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
八、zabbix-server節點建立roles相關目錄結構:
# cd /etc/ansible/roles
# mkdir -pv {prepare,zabbix-agent}/{files,templates,tasks,handlers,vars,meta,defaults}
九、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
十、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
備註:不能有中橫槓,下劃線能夠
十一、zabbix-server節點查看roles目錄結構:
# yum -y install tree
# cd /etc/ansible
# tree
十二、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
1三、Zabbix Web中定義動做:
Configuration --> Actions --> Auto registration --> Create action --> Add
1四、查看已添加主機:
Configuration --> Hosts
1五、查看2個節點最新監控數據: