This page shows an Ansible playbook that installs and configures Zabbix agent 6.0 on CentOS and RHEL hosts. Below the original playbook you will find a corrected, idempotent version that uses the official Zabbix repository, a templated agent configuration, a restart handler and a firewall rule, plus the steps to check that the agent is working.
Ansible Playbook – Centos/RHEL for Zabbix Agent 6
What the original playbook does and where it falls short
The playbook above runs against an inventory group called agents. It disables SELinux, installs pcre2-devel, installs the Zabbix agent RPM directly from a URL, upgrades every package on the host, copies a static zabbix_agentd.conf and starts the service. It works on a fresh CentOS 7 box, but it has problems that show up once you use it on real servers:
- It installs the agent package itself, not the repository. The URL points to
zabbix-agent-6.0.0-1.el7.x86_64.rpm, so no Zabbix repo is configured. The laterstate: latesttask cannot upgrade beyond 6.0.0 because no repository offers newer builds. validate_certs: noturns off TLS checks on the download. This is not safe for a package that runs as a service on every host.- **
yum: name=* state=latest** upgrades every package on every host each time the playbook runs. That is a patching decision. It should not be a side effect of installing a monitoring agent. - Disabling SELinux only takes full effect after a reboot, and it weakens the host. The Zabbix agent package runs with SELinux enforcing for standard checks.
pcre2-develis a development headers package. The agent RPM pulls in the runtime libraries it needs as dependencies.- The config copy has no owner or mode even though the task name says it does. Changing the file does not restart the agent either, so new settings are ignored until a manual restart.
- The service is started but not enabled, so the agent does not come back after a reboot.
- Port 10050/tcp is never opened, so passive checks from the server fail on hosts where firewalld is running.
Prerequisites
- A control node with Ansible installed and SSH access to the target hosts, with sudo rights for the SSH user.
- Target hosts running a RHEL-family OS (RHEL, CentOS, Rocky Linux, AlmaLinux) that Zabbix publishes packages for.
- A running Zabbix server or proxy, and its IP address or DNS name.
- The
ansible.posixcollection for the firewalld module:ansible-galaxy collection install ansible.posix.
Note: CentOS 7 has reached end of life and ships Python 2.7. Recent ansible-core releases (2.17 and later) no longer support Python 2.7 on managed nodes, so to manage CentOS 7 you need an older ansible-core. Where you can, move agents to a supported EL 8 or EL 9 distribution.
Project layout and inventory
zabbix-agent/
โโโ inventory.ini
โโโ zabbix-agent.yaml
โโโ templates/
โโโ zabbix_agentd.conf.j2
[agents]
web01 ansible_host=10.0.0.21
web02 ansible_host=10.0.0.22
[agents:vars]
ansible_user=centos
zabbix_server=10.0.0.10
zabbix_server is used in the configuration template. You can also set it in group_vars/agents.yml if you want to keep inventory files short.
Corrected playbook
This version sets up the official Zabbix repository with GPG checking, installs the agent from that repository, deploys a templated config with correct permissions, restarts the agent only when the config changes, opens the firewall port when firewalld is running, and enables the service so it starts at boot.
---
- name: Deploy Zabbix agent 6.0 on RHEL-family hosts
hosts: agents
become: true
gather_facts: true
vars:
zabbix_version: "6.0"
# Check repo.zabbix.com for the signing key used for your OS release
zabbix_gpg_key: "https://repo.zabbix.com/RPM-GPG-KEY-ZABBIX-A14FE591"
tasks:
- name: Configure the Zabbix package repository
ansible.builtin.yum_repository:
name: zabbix
description: "Zabbix {{ zabbix_version }} for EL{{ ansible_distribution_major_version }}"
baseurl: "https://repo.zabbix.com/zabbix/{{ zabbix_version }}/rhel/{{ ansible_distribution_major_version }}/$basearch/"
gpgcheck: true
gpgkey: "{{ zabbix_gpg_key }}"
enabled: true
- name: Install Zabbix agent
ansible.builtin.package:
name: zabbix-agent
state: present
- name: Deploy agent configuration
ansible.builtin.template:
src: zabbix_agentd.conf.j2
dest: /etc/zabbix/zabbix_agentd.conf
owner: root
group: root
mode: "0644"
notify: Restart zabbix-agent
- name: Collect service facts
ansible.builtin.service_facts:
- name: Allow Zabbix passive checks through firewalld
ansible.posix.firewalld:
port: 10050/tcp
permanent: true
immediate: true
state: enabled
when:
- "'firewalld.service' in ansible_facts.services"
- ansible_facts.services['firewalld.service'].state == 'running'
- name: Enable and start Zabbix agent
ansible.builtin.service:
name: zabbix-agent
state: started
enabled: true
handlers:
- name: Restart zabbix-agent
ansible.builtin.service:
name: zabbix-agent
state: restartedCode language: PHP (php)
Why each change matters:
yum_repositorycreates/etc/yum.repos.d/zabbix.repo, so later runs and normal OS patching can pick up 6.0.x bug-fix releases.ansible_distribution_major_versionselects the EL7, EL8 or EL9 path, which is whygather_factsis nowtrue.ansible.builtin.packagecalls yum or dnf, whichever the host uses.state: presentkeeps runs idempotent. Upgrades are handled on purpose, through your patching process.templateinstead ofcopylets each host get its ownHostnameand the right server address.notifyrestarts the agent only when the config file actually changes.- The firewalld task is skipped on hosts that do not run firewalld, so it does not fail there.
Note: Zabbix signs packages for newer EL releases with a different GPG key than older ones. If dnf reports a GPG or signature error on EL9, open the Zabbix repository for your release and set zabbix_gpg_key to the key file it uses. Do not fix the error by setting gpgcheck: false.
Agent configuration template
Save this as templates/zabbix_agentd.conf.j2. The paths match the defaults in the official Zabbix 6.0 RPM.
# Managed by Ansible - local changes will be overwritten
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
# Passive checks: servers/proxies allowed to connect to this agent
Server={{ zabbix_server }}
# Active checks: server/proxy this agent connects to
ServerActive={{ zabbix_server }}
# Must match the host name configured in the Zabbix frontend
Hostname={{ inventory_hostname }}
Include=/etc/zabbix/zabbix_agentd.d/*.confCode language: PHP (php)
Server controls which addresses can poll the agent. ServerActive is where the agent sends active check data. For active checks to work, Hostname must exactly match the host name set in the Zabbix frontend.
Run and verify the deployment
- Check the syntax:
ansible-playbook -i inventory.ini zabbix-agent.yaml --syntax-check - Do a dry run that shows file differences:
ansible-playbook -i inventory.ini zabbix-agent.yaml --check --diff. On a fresh host, the repository and package tasks mean later tasks may report errors in check mode. This is expected. - Apply it:
ansible-playbook -i inventory.ini zabbix-agent.yaml --private-key node.pem - Confirm that the service is running on every host:
ansible agents -i inventory.ini -b -m ansible.builtin.command -a "systemctl is-active zabbix-agent" - Test a key locally on a host:
zabbix_agentd -t agent.pingshould printagent.ping [u|1]. - From the Zabbix server, test a passive check:
zabbix_get -s 10.0.0.21 -k agent.pingshould return1. - Add the host in the Zabbix frontend with the same host name and an agent interface on port 10050, then link a template such as Linux by Zabbix agent.
Run the playbook a second time. Every task should report ok and none should report changed. That shows the playbook is idempotent.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
Agent log shows connection from "x.x.x.x" rejected, allowed hosts: ... | The server's IP address is not in Server | Set zabbix_server to the address the server or proxy actually connects from, then rerun the playbook |
zabbix_get times out or reports connection refused | Port 10050/tcp blocked by a host firewall or a cloud security group | Check firewall-cmd --list-ports and open 10050 in the security group or network ACL |
| Host is green for passive checks but active items get no data | Hostname does not match the frontend host name, or 10051/tcp to the server is blocked | Make the names match exactly and allow outbound traffic to the server on 10051 |
| GPG check failed or signature error during install | Wrong key for the OS release | Set zabbix_gpg_key to the key published for that EL version |
| Zabbix packages come from EPEL instead of repo.zabbix.com | EPEL also carries Zabbix packages | Add excludepkgs=zabbix* to the EPEL repo definition, as Zabbix's install instructions recommend |
Repository zabbix is listed more than once | A zabbix-release RPM was installed earlier | Remove the zabbix-release package or skip the yum_repository task on those hosts |
Best practices for production
- Keep SELinux enforcing. If a custom
UserParameterscript is blocked, checkausearch -m avcand add a targeted policy rather than disabling SELinux. - Encrypt agent traffic with PSK or certificates (
TLSConnect,TLSAccept,TLSPSKIdentity,TLSPSKFile). Store the PSK in Ansible Vault. - Put custom
UserParameterdefinitions in/etc/zabbix/zabbix_agentd.d/as separate templated files instead of editing the main config. - Pin
zabbix_versionto the server's major version. An agent can report to a newer server, but not to an older one. - For larger fleets, consider turning this into a role or using the community-maintained Zabbix collection on Ansible Galaxy, which also handles Debian-family hosts and Zabbix agent 2.
Frequently asked questions
Which port does the Zabbix agent use?
The agent listens on 10050/tcp for passive checks from the server or proxy. For active checks, the agent connects out to the server or proxy on 10051/tcp.
Do I need to disable SELinux to run the Zabbix agent?
No. The official agent package works with SELinux enforcing for standard checks. Only custom scripts or unusual file access may need a targeted SELinux policy.
How do I upgrade the agent with this playbook?
Change state: present to state: latest for a single run, or let your normal OS patching update it from the configured Zabbix repository. To move to a new major version, change zabbix_version and confirm that your Zabbix server is at that version or newer.
Can I use the same playbook for Zabbix agent 2?
Mostly, yes. Install the zabbix-agent2 package, template /etc/zabbix/zabbix_agent2.conf and manage the zabbix-agent2 service. Do not run both agents on port 10050 at the same time.
Why does the agent show as unavailable in the Zabbix frontend?
The usual causes are a Server value that does not include the server's IP address, a blocked port 10050, or an interface IP in the frontend that does not match the host. Run zabbix_get -s <host> -k agent.ping from the server and read /var/log/zabbix/zabbix_agentd.log on the agent to find out which one it is.
Related tutorials
- The Complete Ansible Tutorial: Concepts, Architecture, Playbooks, and Real-World Examples
- What is Zabbix and use cases of Zabbix?
- More Zabbix tutorials
- More Ansible tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals
Never disable SELInux. Only clueless beginners do that.