环境配置
主机 | ip |
ansible | 192.168.58.150 |
agent 客户端 | 192.168.58.45 |
修改主机映射
[root@ansible ansible]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.58.45 zabbix-agent01
192.168.58.46 zabbix-agent02
192.168.58.150 ansible
密钥的发放
[root@ansible ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:8yuso7yB8426UAQE4AXnrDlq4vTpmnI7nI432uf0ab0 root@ansible
The key's randomart image is:
+---[RSA 3072]----+
|Bo.o |
|..= |
| ..o |
| .o |
| +. S |
|.... o |
|++o.o o . |
|*+X=o=o.+ . |
|+OO%B+=oEo. |
+----[SHA256]-----+
[root@ansible ~]# vim /etc/hosts
[root@ansible ~]# ssh-copy-id zabbix-agent01
ansible剧本的编写
首先先测试一下192.168.58.45主机是否可以通
[root@ansible ansible]# ansible -m ping 192.168.58.45
192.168.58.45 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
显示绿色 表示通过
剧本的编写
---
- name: zabbix6.0 install
hosts: 192.168.58.45
become: yes
tasks:
- name: rpm
ansible.builtin.get_url:
url: https://repo.zabbix.com/zabbix/6.0/rhel/9/x86_64/zabbix-release-6.0-4.el9.noarch.rpm
dest: ~/zabbix-release-6.0-4.el9.noarch.rpm
- name: 安装 agent2
ansible.builtin.yum:
name:
- zabbix-release-6.0-4.el9.noarch.rpm
- zabbix-agent2
- zabbix-agent2-plugin-*
- name: 服务
service:
name: zabbix-agent2
state: started
enabled: yes
执行结果
[root@ansible ansible]# vim zabbix6.0.yaml
[root@ansible ansible]# ansible-playbook zabbix6.0.yaml
PLAY [zabbix6.0 install] ********************************************************************************
TASK [Gathering Facts] **********************************************************************************
ok: [192.168.58.45]
TASK [rpm] **********************************************************************************************
changed: [192.168.58.45]
TASK [安装 agent2] **************************************************************************************
changed: [192.168.58.45]
TASK [服务] *********************************************************************************************
changed: [192.168.58.45]
PLAY RECAP **********************************************************************************************
192.168.58.45 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
步骤大概 拉取rpm 包 然后下载安装 并开启服务 becom允许 root
修改客户端的配置文件剧本
---
- name: modify file
hosts: 192.168.58.45
become: yes #表示此任务需要root权限运行
tasks:
- name: replace line in file
ansible.builtin.replace: #用于文件编辑模块
path: /etc/zabbix/zabbix_agent2.conf
regexp: '^Server=127.0.0.1' #指定查找和替换的模式
replace: 'Server=192.168.58.20' #定义新的行内容
backup: yes
结束啦