用ansible自动化部署zabbix监控系统
3.zabbix网页端网址为http://server端ip:10050,默认用户Admin,密码zabbix。2.该剧本部署的为被动式监控系统,如要更换为主动式,仍需进行少许配置修改。5. 安装zabbix-agent和zabbix-sender。3. 安装zabbix-release-5.0 repo文件。4.在网页端添加主机和监控项,完成zabbix报警监控系统。2. 下载zabbix-re
1.制作本地域名解析
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.89.140 zabbix-node0
192.168.89.141 zabbix-node1
192.168.89.142 zabbix-node2
192.168.89.143 zabbix-node3
192.168.89.144 zabbix-node4
192.168.89.145 zabbix-node5
192.168.89.146 zabbix-node6
192.168.89.147 zabbix-node7
192.168.89.148 zabbix-node8
192.168.89.149 zabbix-node9
192.168.89.150 zabbix-node10
192.168.89.151 zabbix-node11
192.168.89.152 zabbix-node12
2.创建变量文件
将受控节点的ip和节点名写入新的变量文件,目的是为了能在ansible里正确引用ip和hostname,参考命令:
cat /etc/hosts|grep 192|awk '{print " - hostname: " $2 "\n ip: "$1}' |sed -r '1i\- computers:' > /root/ansible/ip_name.yaml
3.编写远程发送公钥的expect脚本
ssh-keygen生成一对不对成的公钥和私钥
vim expect1 #制作自动化发送公钥的脚本
#!/bin/expect
set pass 123123
set timeout -1 #一定要写,防止连接超时
set ip [ lindex $argv 0 ] #接收变量
spawn ssh-copy-id $ip
expect {
"yes/no" { send "yes\r"; exp_continue }
"password: " { send "$pass\r" };
}
interact
4.用shell脚本实现免密登录
制作简易脚本,不进行验证输入参数的正确与否,仅供参考
vim /root/ansible/ssh-copy-id.sh
#!/bin/bash
read -p "请输入你要远程发送公钥的ip范围(例如:192.168.89.140-192.168.89.147):" ADDR
IP41=`echo $ADDR|awk -F'.' '{print $4}'|awk -F'-' '{print $1}'`
IP42=`echo $ADDR|awk -F'.' '{print $7}'`
IP11=`echo $ADDR|awk -F'.' '{print $1}'`
IP12=`echo $ADDR|awk -F'.' '{print $4}'|awk -F'-' '{print $2}'`
IP21=`echo $ADDR|awk -F'.' '{print $2}'`
IP22=`echo $ADDR|awk -F'.' '{print $5}'`
IP31=`echo $ADDR|awk -F'.' '{print $3}'`
IP32=`echo $ADDR|awk -F'.' '{print $6}'`
for (( i=$IP41;i<=$IP42;i++ ));do
ping -c1 $IP11.$IP21.$IP31.$i >/dev/null
hostname$i=`cat /etc/hosts |grep $IP11.$IP21.$IP31.$i|awk '{print $2}'` #实现ip向hostname的转换
if [ $? -eq 0 ];then
./expect01 $IP11.$IP21.$IP31.$i #对ip做信任登录
./expect01 $((hostname$i)) #对hostname做信任登录
else
echo "$IP11.$IP21.$IP31.$i 不可达"
fi
done
5.编写修改hostname的shell脚本
该脚本用于ansible自动化修改节点主机名
1 #!/bin/bash
2 hostnamectl set-hostname $1
6.编写zabbix-agent自动化部署yml剧本
剧本流程:
1. 修改节点hostname
2. 下载zabbix-release-5.0 rpm包
3. 安装zabbix-release-5.0 repo文件
4. 禁用epel源
5. 安装zabbix-agent和zabbix-sender
6. 修改zabbix-agent配置文件
7. 启动zabbix-agent并设置开机自启
---
- name: install zabbix-agent
hosts: test
vars_files:
- /root/ansible/ip_name.yaml
tasks:
- name: Run a script for each computer
script: /root/ansible/hostnamectl.sh "{{ item.name }}"
loop: "{{ computers }}"
when: item.ip in ansible_all_ipv4_addresses
- name: Download zabbix-agent
get_url:
url: https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
dest: /etc/zabbix-release-5.0-1.el7.noarch.rpm
- name: install zabbix rpm from a local file
yum:
name: /etc/zabbix-release-5.0-1.el7.noarch.rpm
state: present
- name: This command will change the working directory to somedir/.
shell:
cmd: mv epel.repo epel.repo.bak
chdir: /etc/yum.repos.d/
creates: epel.repo.bak
- name: ensure a list of packages installed
yum:
name: "{{ packages }}"
vars:
packages:
- zabbix-agent
- zabbix-sender
- name: exchange Server ip
lineinfile:
path: /etc/zabbix/zabbix_agentd.conf
regexp: '^Server=127.0.0.1'
line: Server=192.168.89.111 #修改为zabbix-server端ip
- name: exchange ServerActive ip
lineinfile:
path: /etc/zabbix/zabbix_agentd.conf
regexp: '^ServerActive=127.0.0.1'
line: ServerActive=192.168.89.111 #修改为zabbix-server端ip
- name: exchange Hostname
lineinfile:
path: /etc/zabbix/zabbix_agentd.conf
regexp: '^Hostname='
line: Hostname={{ ansible_hostname }}
- name: exchange UnsafeUserParameters to 1
lineinfile:
path: /etc/zabbix/zabbix_agentd.conf
regexp: '^# UnsafeUserParameters='
line: UnsafeUserParameters=1 #启用特殊字符
- name: Make sure a service is running
systemd:
name: zabbix-agent
state: started
enabled: yes
- name: restart zabbix-agent
systemd:
name: zabbix-agent
state: restarted
7.其他
zabbix-agent端部署完毕,仍需要关注的几个问题:
1.部署zabbix-server端
2.该剧本部署的为被动式监控系统,如要更换为主动式,仍需进行少许配置修改
3.zabbix网页端网址为http://server端ip:10050,默认用户Admin,密码zabbix
4.在网页端添加主机和监控项,完成zabbix报警监控系统
更多推荐




所有评论(0)