十三、使用Ansible批量安裝Zabbix Agent,并通過自動注冊添加Linux主機(jī):
成都創(chuàng)新互聯(lián)公司 - 成都服務(wù)器托管,四川服務(wù)器租用,成都服務(wù)器租用,四川網(wǎng)通托管,綿陽服務(wù)器托管,德陽服務(wù)器托管,遂寧服務(wù)器托管,綿陽服務(wù)器托管,四川云主機(jī),成都云主機(jī),西南云主機(jī),成都服務(wù)器托管,西南服務(wù)器托管,四川/成都大帶寬,機(jī)柜大帶寬租用·托管,四川老牌IDC服務(wù)商
1、Ansible簡介:
Ansible是一款基于Python研發(fā)的開源自動化工具,實(shí)現(xiàn)了批量運(yùn)行命令、批量部署程序、批量配置系統(tǒng)等功能。默認(rèn)通過SSH協(xié)議(也可使用其它協(xié)議)進(jìn)行遠(yuǎn)程命令執(zhí)行或下發(fā)配置,無需部署任何客戶端代理軟件(agentless)在被管控主機(jī)上,并可同時支持多臺主機(jī)并行管理。Ansible是基于模塊工作的,本身沒有批量部署的能力,真正具有批量部署的是Ansible所運(yùn)行的模塊,Ansible只是提供一種框架。Ansible幫助文檔:https://docs.ansible.com/ansible/latest/index.html
2、演示環(huán)境:
IP | 操作系統(tǒng) | 主機(jī)名 | 角色 |
192.168.0.120 | CentOS ? 7.7 x86_64 | zabbix-server | Zabbix ? Database、Zabbix ? Server、Zabbix ? Web、Zabbix ? Agent、Ansible主機(jī) |
192.168.0.121 | CentOS ? 7.7 x86_64 | web01 | Zabbix ? Agent、被管控主機(jī) |
192.168.0.122 | CentOS ? 7.7 x86_64 | db01 | Zabbix ? Agent、被管控主機(jī) |
目標(biāo):zabbix-server節(jié)點(diǎn)通過Ansible自動配置web01和db01節(jié)點(diǎn)的防火墻、SELinux、系統(tǒng)時間、主機(jī)名,自動安裝、配置、啟動Zabbix Agent,最后通過Zabbix Web自動注冊功能批量添加Linux主機(jī)
3、zabbix-server節(jié)點(diǎn)準(zhǔn)備工作:
(1)配置hosts文件:
# vim /etc/hosts
192.168.0.120 zabbix-server
192.168.0.121 web01
192.168.0.122 db01
(2)配置chrony服務(wù)端:
a、修改chrony.conf配置文件:
# yum -y install chrony
# mv /etc/chrony.conf{,.bak}
# vim /etc/chrony.conf,新增如下代碼:
#指定上層NTP服務(wù)器為阿里云提供的公網(wǎng)NTP服務(wù)器
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
#記錄系統(tǒng)時鐘獲得/丟失時間的速率至drift文件中
driftfile /var/lib/chrony/drift
#如果系統(tǒng)時鐘的偏移量大于10秒,則允許在前三次更新中步進(jìn)調(diào)整系統(tǒng)時鐘
makestep 10 3
#啟用RTC(實(shí)時時鐘)的內(nèi)核同步
rtcsync
#只允許192.168.0網(wǎng)段的客戶端進(jìn)行時間同步
allow 192.168.0.0/24
#如果未能從阿里云提供的公網(wǎng)NTP服務(wù)器同步到時間,也允許將本地時間作為標(biāo)準(zhǔn)時間授時給其它客戶端
local stratum 10
#指定包含NTP驗(yàn)證密鑰的文件
keyfile /etc/chrony.keys
#指定存放日志文件的目錄
logdir /var/log/chrony
#讓chronyd在選擇源時忽略源的層級
stratumweight 0
#禁用客戶端訪問的日志記錄
noclientlog
#如果時鐘調(diào)整大于0.5秒,則向系統(tǒng)日志發(fā)送消息
logchange 0.5
說明:詳細(xì)指令參數(shù)可以使用命令# man chrony.conf查看
b、啟動chronyd:
# systemctl start chronyd
# systemctl status chronyd
# ps aux | grep chronyd
# ss -tunlp | grep chronyd
備注:123端口為NTP服務(wù)監(jiān)聽端口,323端口為chrony服務(wù)監(jiān)聽端口
c、配置開機(jī)自啟:# 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、查看時間同步源狀態(tài):# chronyc sourcestats -v
(3)查看Python版本:# python -V
(4)還原至最初配置,刪除Zabbix Web中zabbix-server以外的所有節(jié)點(diǎn):
4、web01和db01節(jié)點(diǎn)為VMware Workstation最小化全新安裝的CentOS 7.7
5、zabbix-server節(jié)點(diǎn)安裝ansible:
# yum -y install epel-release
# yum -y install ansible
# ansible --version
6、zabbix-server節(jié)點(diǎn)配置被管控主機(jī)的主機(jī)清單文件:
# vim /etc/ansible/hosts,末尾新增如下代碼:
[websrvs]
web01 ansible_host=192.168.0.121
[dbsrvs]
db01 ansible_host=192.168.0.122
7、??zabbix-server節(jié)點(diǎn)配置SSH互信:
(1)生成密鑰對,基于密鑰認(rèn)證:# ssh-keygen -t rsa -P ""
(2)復(fù)制公鑰至所有被管控主機(jī):
# 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節(jié)點(diǎn)創(chuàng)建roles相關(guān)目錄結(jié)構(gòu):
# cd /etc/ansible/roles
# mkdir -pv {prepare,zabbix-agent}/{files,templates,tasks,handlers,vars,meta,defaults}
9、zabbix-server節(jié)點(diǎn)配置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/hostsline="{{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為內(nèi)網(wǎng)chrony服務(wù)端IP
(3)修改prepare/handlers/main.yml配置文件:
# vim prepare/handlers/main.yml
- name: Restart Chrony Service
service: name=chronyd state=restarted
10、zabbix-server節(jié)點(diǎn)配置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
對應(yīng)的/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)復(fù)制zabbix-server節(jié)點(diǎn)的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節(jié)點(diǎn)查看roles目錄結(jié)構(gòu):
# yum -y install tree
# cd /etc/ansible
# tree
12、zabbix-server節(jié)點(diǎn)編寫playbook并執(zhí)行:
# 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、查看已添加主機(jī):
Configuration --> Hosts
15、查看2個節(jié)點(diǎn)最新監(jiān)控?cái)?shù)據(jù):
新聞名稱:CentOS7.7yum方式安裝配置Zabbix4.0LTS詳解(十一)完結(jié)
當(dāng)前網(wǎng)址:http://aaarwkj.com/article40/pccdeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站內(nèi)鏈、定制網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、標(biāo)簽優(yōu)化、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)