密钥授权认证实现免密登陆
[root@ansible-centos7-shanghai-area6 ~]# ssh-keygen -t rsa
[root@ansible-centos7-shanghai-area6 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 10.0.2.21
在ansible/hosts中添加主机信息
[root@ansible-centos7-shanghai-area6 ~]# vim /etc/ansible/hosts
#10.0.2.21 ansible_port=22 ansible_user=hj ansible_ssh_pass=v2L8x;pV
#test21 ansible_host=10.0.2.21 ansible_port=22 ansible_user=hj ansible_ssh_pass=v2L8x;pV
#test21 ansible_host=10.0.2.21 ansible_port=22 ansible_user=root
10.0.2.21 ansible_port=22 ansible_user=root
定义agent模板
[root@ansible-centos7-shanghai-area6 ~]# vim /etc/ansible/zabbix_agentd.conf # 创建一个模板文件,里面包含agent中可变的变量,如:主机名和server地址
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server={{server}}
ServerActive={{server}}
Hostname={{hostname}}
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1
编写playbook文件
[root@ansible-centos7-shanghai-area6 ~]# vim /etc/ansible/linux-agent.yml
- hosts: zabbix-agent
remote_user: root
vars:
server: 10.0.2.2
hostname: "{{ ansible_hostname }}"
tasks:
- name: install rpm
command: rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-agent-4.2.1-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文件进行批量部署
[root@ansible-centos7-shanghai-area6 ~]# ansible-playbook -i /etc/ansible/hosts /etc/ansible/linux-agent.yml
PLAY [10.0.2.21] ************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************
ok: [10.0.2.21]
TASK [install agent] ********************************************************************************************************************************************************************************************************************
[WARNING]: Consider using the yum module rather than running 'yum'. If you need to use command because yum is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of
this message.
changed: [10.0.2.21]
TASK [cp templates zabbix_agentd.conf to zabbix agentd] *********************************************************************************************************************************************************************************
changed: [10.0.2.21]
TASK [restart zabbix-agent] *************************************************************************************************************************************************************************************************************
changed: [10.0.2.21]
PLAY RECAP ******************************************************************************************************************************************************************************************************************************
10.0.2.21 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
client 端验证服务运行情况
[hj@java10-centos7-shanghai-area6 ~]# sudo egrep -v "^$|^#" /etc/zabbix/zabbix_agentd.conf # agent配置文件中的变量也修改完成
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.0.2.2
ServerActive=10.0.2.2
Hostname=java10-centos7-shanghai-area6
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1
[hj@java10-centos7-shanghai-area6 ~]# sudo systemctl status zabbix-agent
● zabbix-agent.service - Zabbix Agent
Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2021-03-25 23:56:55 CST; 13s ago
Process: 9897 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
Main PID: 9899 (zabbix_agentd)
CGroup: /system.slice/zabbix-agent.service
├─9899 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
├─9900 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
├─9901 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
├─9902 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
├─9903 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
└─9904 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
Mar 25 23:56:55 java10-centos7-shanghai-area6 systemd[1]: Starting Zabbix Agent...
Mar 25 23:56:55 java10-centos7-shanghai-area6 systemd[1]: PID file /run/zabbix/zabbix_agentd.pid not readable (yet?) after start.
Mar 25 23:56:55 java10-centos7-shanghai-area6 systemd[1]: zabbix-agent.service: Supervising process 9899 which is not our child. We'll most likely not notice when it exits.
Mar 25 23:56:55 java10-centos7-shanghai-area6 systemd[1]: Started Zabbix Agent.