Note: This article relates to ansible after version 2.5.0 and zabbix version 3.4, but presumably works with version 4.0

Prerequisites for this tutorial

The prerequisites for this tutorial are a working zabbix server, version 3.4 or greater, and you should have to hand the zabbix web gui URL, and login details for that instance. If you haven’t yet setup a zabbix server, we provide an ansible role for this task, and this is available from galaxy here.

Your fresh instance of zabbix should look something like this:

XXXX

You should also have a working ansible installation, and be familiar with the basic ansible cli commands.

So here we dive into actually configuring the hosts, templates, users, and alerts that we need for a useful monitoring setup with ansible and zabbix.

The configuration is split into 2 parts, and so we use 2 plays. One for general configuration that is applied to the server, and the second for host specific configuration. The general configuration only needs to be done once, and not for every host.

So initially we will create a file like so, with a section for configuring the server, and section which is run for each host to be monitored.

---

- hosts: zabbix
  become: true
  tasks:
    - import_role:
        name: limepepper.zabbix-server

- hosts: zabbix_hosts
  tasks:
    - import_role:
        name: limepepper.zabbix-agent

If you run this with ansible-playbook it will import the zabbix-server modules and install zabbix agent on each of the hosts.

You can check each host has zabbix-agent running, but as we have not configured auto-discovery, we do not expect the hosts to exist in the server console yet.

Hosts

So when run, this playbook will run first against the zabbix server, and then once for each Host in the ansible hostgroup zabbix_hosts. What we want the result of that to be, is that each hosts registers in zabbix, and for that we can use the zabbix_host module like so;

---

- hosts: zabbix
  become: true
  tasks:
    - import_role:
        name: limepepper.zabbix-server

- hosts: zabbix_hosts
  tasks:
    - import_role:
        name: limepepper.zabbix-agent

    - name: Create a new host for this agent
      zabbix_host:
        login_user: Admin
        login_password: "{{ zabbix_web_admin_pass }}"
        server_url: http://localhost/zabbix
        host_name: "{{ ansible_fqdn }}"
        # requires that the host be in at least 1 group
        host_groups: "{{ zabbix_groups }}"
        link_templates:
          - Template OS Linux
        inventory_mode: automatic
        interfaces:
          - type: agent
            main: 1
            useip: "{{ zabbix_interface_use_ip | ternary( 1, 0 ) }}"
            ip: "{{ zabbix_interface_use_ipv6 | ternary( ansible_default_ipv6.address, ansible_default_ipv4.address ) }}"
            dns: "{{ ansible_fqdn }}"
            port: 10050

once you run this playbook, you should expect to see each of the hosts in the zabbix_hosts ansible group appear in the zabbix gui under the configuration tab: