Automate Bulk Zabbix‑Agent Deployment on Linux and Windows with Ansible
This guide shows how to use Ansible to automate the mass installation and configuration of Zabbix‑agent on both Linux and Windows hosts, covering key‑based SSH authentication, playbook creation, service management, and Zabbix auto‑discovery setup.
Automate Bulk Deployment of Zabbix‑Agent on Linux
We use Ansible to deploy Zabbix‑agent to multiple Linux hosts; the same approach can be applied with scripts.
Environment
ansible: 10.127.0.133
agent1: 172.168.0.4
agent2: 172.168.0.5Key‑based SSH authentication (password‑less login)
Generate a key pair on the Ansible control node and copy the public key to each agent.
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub 172.168.0.4
ssh-copy-id -i /root/.ssh/id_rsa.pub 172.168.0.5Add hosts to /etc/ansible/hosts
[Linux-agent]
172.168.0.4
172.168.0.5Linux‑agent playbook
Steps
Install the Zabbix‑agent 4.2 RPM package.
Install the agent via yum .
Replace configuration variables with a template file.
Restart the Zabbix‑agent service.
Agent configuration template
Create /etc/ansible/zabbix_agentd.conf containing variables such as hostname and server address.
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=1Playbook file
vim /etc/ansible/linux-agent.yml
- hosts: zabbix-agent
remote_user: root
vars:
server: 10.127.0.133
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-agentExecute the playbook
Run:
ansible-playbook -i /etc/ansible/hosts /etc/ansible/linux-agent.ymlSuccessful execution is shown in the following screenshots.
The agent configuration file reflects the updated variables.
Create auto‑discovery rule for the deployed hosts
Define a discovery rule, add discovery actions, and configure post‑discovery operations. The screenshots illustrate the rule linking to a Linux‑OS template.
Automate Bulk Deployment of Zabbix‑Agent on Windows
Windows deployment can be performed with Ansible using WinRM; the steps below show the required preparation and playbook.
Environment
ansible: 10.127.0.133
Windows server2012: 172.168.0.6Dependencies
Ansible
pywinrm>=0.3.0Install with:
pip install pywinrm>=0.3.0Windows
PowerShell 3.0
.NET Framework 4.0+For older Windows versions, upgrade as described in the Ansible documentation.
Install WinRM memory hot‑fix
Run the following PowerShell script to apply the hot‑fix.
$url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Install-WMF3Hotfix.ps1"
$file = "$env:temp\Install-WMF3Hotfix.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file -VerboseOpen firewall ports for WinRM and Zabbix‑agent
WinRM listens on 5985 by default; Zabbix‑agent uses 10050. Ensure both ports are allowed.
winrm enumerate winrm/config/ListenerDownload Windows Zabbix‑agent package
Obtain the Windows agent zip from the official Zabbix download page and extract it on the Ansible control node.
Add Windows host to /etc/ansible/hosts
[windows]
172.168.0.6 ansible_python_interpreter=/usr/bin/python ansible_user="administrator" ansible_password="asd.123" ansible_port=5985 ansible_connection="winrm" ansible_winrm_transport=ntlm ansible_winrm_server_cert_validation=ignoreWindows‑agent playbook
Steps
Copy the downloaded agent files to the Windows host.
Replace configuration variables using a template.
Install the Zabbix‑agent.
Start the Zabbix‑agent service.
Playbook file
vim /etc/ansible/windows-agent.yml
- hosts: windows
remote_user: administrator
vars:
server: 10.127.0.133
hostname: "{{ ansible_host }}"
tasks:
- name: cp zabbix-agent
win_copy:
src: /etc/ansible/windows_agent/
dest: C:\windows_agent\
- name: cp templates zabbix_agentd.conf to zabbix agentd
win_template:
src: /etc/ansible/zabbix_agentd.conf
dest: C:\windows_agent\conf\
- name: install zabbix-agent
win_command: zabbix_agentd.exe -i -c C:\windows_agent\conf\zabbix_agentd.conf
args:
chdir: C:\windows_agent\bin\
- name: start zabbix-agent
win_command: zabbix_agentd.exe -s -c C:\windows_agent\conf\zabbix_agentd.conf
args:
chdir: C:\windows_agent\bin\Execute the playbook
ansible-playbook -i /etc/ansible/hosts /etc/ansible/windows-agent.ymlSuccessful execution and running service are shown below.
Configure discovery actions for Windows hosts
Define discovery actions and post‑discovery operations; the screenshots demonstrate the rule linking to a Windows‑OS template.
Both Linux and Windows agents are now automatically discovered and monitored by Zabbix.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
