MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Ansible Playbook for Zabbix Agent Deployment

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 later state: latest task cannot upgrade beyond 6.0.0 because no repository offers newer builds.
  • validate_certs: no turns 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-devel is 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.posix collection 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_repository creates /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_version selects the EL7, EL8 or EL9 path, which is why gather_facts is now true.
  • ansible.builtin.package calls yum or dnf, whichever the host uses. state: present keeps runs idempotent. Upgrades are handled on purpose, through your patching process.
  • template instead of copy lets each host get its own Hostname and the right server address.
  • notify restarts 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

  1. Check the syntax: ansible-playbook -i inventory.ini zabbix-agent.yaml --syntax-check
  2. 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.
  3. Apply it: ansible-playbook -i inventory.ini zabbix-agent.yaml --private-key node.pem
  4. 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"
  5. Test a key locally on a host: zabbix_agentd -t agent.ping should print agent.ping [u|1].
  6. From the Zabbix server, test a passive check: zabbix_get -s 10.0.0.21 -k agent.ping should return 1.
  7. 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

SymptomLikely causeFix
Agent log shows connection from "x.x.x.x" rejected, allowed hosts: ...The server's IP address is not in ServerSet zabbix_server to the address the server or proxy actually connects from, then rerun the playbook
zabbix_get times out or reports connection refusedPort 10050/tcp blocked by a host firewall or a cloud security groupCheck 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 dataHostname does not match the frontend host name, or 10051/tcp to the server is blockedMake the names match exactly and allow outbound traffic to the server on 10051
GPG check failed or signature error during installWrong key for the OS releaseSet zabbix_gpg_key to the key published for that EL version
Zabbix packages come from EPEL instead of repo.zabbix.comEPEL also carries Zabbix packagesAdd excludepkgs=zabbix* to the EPEL repo definition, as Zabbix's install instructions recommend
Repository zabbix is listed more than onceA zabbix-release RPM was installed earlierRemove the zabbix-release package or skip the yum_repository task on those hosts

Best practices for production

  • Keep SELinux enforcing. If a custom UserParameter script is blocked, check ausearch -m avc and 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 UserParameter definitions in /etc/zabbix/zabbix_agentd.d/ as separate templated files instead of editing the main config.
  • Pin zabbix_version to 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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals

Related Posts

How to add Hosts & Devices in Bulk in Zabbix Automatically?

Method 1 – Active Agent Registration Method 2- Network Discovery Method 3 – API How to setup Active Agent in Zabbix? Step 1 – Configure Zabbix Agent…

Read More

Zabbix Tutorials: Example of zabbix_proxy.conf

The example above is the full, commented sample zabbix_proxy.conf shipped with Zabbix 7.0. It includes 7.0-only options such as ProxyBufferMode, ProxyMemoryBufferSize and the asynchronous agent, HTTP and…

Read More

Zabbix Tutorials: Example of zabbix_agentd.conf

zabbix_agentd.conf is the configuration file for the classic Zabbix agent on Linux and Unix. It sets which Zabbix servers may poll the agent, where it sends active…

Read More

Zabbix Tutorials: Example of zabbix_agentd.win.conf

zabbix_agentd.win.conf is the sample configuration file that comes with the classic Zabbix agent for Windows. The file above is the stock sample from a recent release, but…

Read More

Zabbix Tutorials: Example of zabbix_agent2.win.conf

The example above is the stock zabbix_agent2.win.conf that ships with Zabbix agent 2 for Windows. Nearly every line is a comment that documents a default value. Only…

Read More

Ansible Training Flow

This page walks through the Ansible training flow step by step. It starts with ad hoc commands, moves on to playbooks and finishes with roles. At each…

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
reiska
reiska
4 years ago

Never disable SELInux. Only clueless beginners do that.

1
0
Would love your thoughts, please comment.x
()
x