原创作者:运维工程师 谢晋
Ansible批量部署zabbix-agnet
环境配置表
hostname | ip |
---|---|
Ansible | 192.168.10.100 |
zabbix-server | 192.168.20.102 |
prometheus | 192.168.99.105 |
Promethues_106 | 192.168.20.106 |
Ansible批量部署zabbix-agnet
- 定义linux主机组和主机
# vi /etc/ansible/hosts
[agnet]
192.168.20.105 hostname=prometheus
192.168.20.106 hostname=Promethues_106
# egrep -v "*#|^$" /etc/ansible/hosts
[agnet]
192.168.20.105 hostname=prometheus
192.168.20.106 hostname=Promethues_106
- 免密登录
管理节点(ansible)中创建密钥对
# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:muxDC7kPCBYJEVoRnKZy3Uj40/Q0mF+FIHvMWEd0WsY root@Ansible
The key's randomart image is:
+---[DSA 1024]----+
|+++= .oo+++= |
|o.* . +Bo.o=E |
|.= + =o++o. |
|o o = o.o |
|oo o S |
|.. .o..o |
| . .++. |
| .oo |
| .o. |
+----[SHA256]-----+
生成密钥对并将公钥推送给被管理端
# cd ~
# cd .ssh/
#将公钥推送至被管理端
# ssh-copy-id root@192.168.20.105
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.20.105 (192.168.20.105)' can't be established.
ECDSA key fingerprint is SHA256:bE/sXXK2vmQ40RJ8HvAZQ9sMl8z8L1C65MXfnX7e7uM.
ECDSA key fingerprint is MD5:e4:61:d0:40:e8:cb:03:21:90:78:0a:b6:8f:96:82:5c.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.20.105's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.20.105'"
and check to make sure that only the key(s) you wanted were added.
# ssh-copy-id root@192.168.20.106
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.20.106 (192.168.20.106)' can't be established.
ECDSA key fingerprint is SHA256:F8YebtWCZA2PyKqnIw5mZLCzvwwmHaujEw2SQ8ChDLI.
ECDSA key fingerprint is MD5:a9:37:d7:61:5f:82:bd:03:57:d4:f6:0e:73:a6:c5:de.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.20.106's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.20.106'"
and check to make sure that only the key(s) you wanted were added.
- 查看linux被管理端是否已经连接
# ansible -i /etc/ansible/hosts agnet -m ping
192.168.20.106 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.20.105 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
连接成功
- ansible 批量下发文件
在这里插入代码片
- 定义agent模板
# cat /etc/ansible/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=50
EnableRemoteCommands=1
LogRemoteCommands=1
Server={{server}}
ServerActive={{server}}
Hostname={{hostname}}
HostMetadata=Linux
Timeout=10
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1
- 添加linux 的 playbook文件
# cat /etc/ansible/linux-agent.yml
- hosts: agnet #修改为hosts里面创建的主
remote_user: root
vars:
server: 192.168.20.102
hostname: "{{ ansible_hostname }}"
tasks:
- name: install rpm
command: rpm -ivh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.18-1.el7.x86_64.rpm
- name: install agent
command: yum install zabbix-agent -y
- name: cp templates zabbix_agentd.conf to zabbix agentd
template: src=/etc/ansible/zabbix_agentd.conf dest=/etc/zabbix/zabbix_agentd.conf
- name: restart zabbix-agent
command: systemctl restart zabbix-agent
- 执行playbook文件进行批量部署
# ansible-playbook -i /etc/ansible/hosts /etc/ansible/linux-agent.yml
PLAY [agnet] ****************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************
ok: [192.168.20.106]
ok: [192.168.20.105]
PLAY RECAP ******************************************************************************************************************
PLAY RECAP ******************************************************************************************************************
192.168.20.105 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.20.106 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 确认安装
登录到192.168.20.105主机
# systemctl status zabbix-agent.service
● zabbix-agent.service - Zabbix Agent
Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2021-12-21 18:45:12 CST; 1min 10s ago
Process: 1546 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
Main PID: 1549 (zabbix_agentd)
Tasks: 6
CGroup: /system.slice/zabbix-agent.service
├─1549 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
├─1550 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
├─1551 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
├─1552 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
├─1553 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
└─1554 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
Dec 21 18:45:12 mysql-slave01 systemd[1]: Starting Zabbix Agent...
Dec 21 18:45:12 mysql-slave01 systemd[1]: Started Zabbix Agent.
登录到192.168.20.106主机
# systemctl status zabbix-agent.service
● zabbix-agent.service - Zabbix Agent
Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2021-12-21 18:45:12 CST; 2min 29s ago
Process: 21944 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
Main PID: 21947 (zabbix_agentd)
Tasks: 6
CGroup: /system.slice/zabbix-agent.service
├─21947 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
├─21948 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
├─21949 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
├─21950 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
├─21951 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
└─21952 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
Dec 21 18:45:12 mysql-slave02 systemd[1]: Starting Zabbix Agent...
Dec 21 18:45:12 mysql-slave02 zabbix_agentd[21944]: zabbix_agentd [21944]: Warning: EnableRemoteCommands parameter is...stead
Dec 21 18:45:12 mysql-slave02 systemd[1]: Started Zabbix Agent.
Hint: Some lines were ellipsized, use -l to show in full.
至此完成安装!