Install zabbix agent with ansible

I have already an installation guide, on how to install the zabbix agent on linux servers here. Here I show how to create an ansible playbook, which automates the installation.

Functionality of the playbook

  • Installs zabbix agent version 5.2 (handled by a variable)
  • works on Ubuntu 20.04 and Debian 10

Preparation

First of all, we create the folder zabbix-agent, with the following structure.

templates\
    zabbix_agentd.conf.j2
vars\
    Debian.yaml
    Ubuntu.yaml
ansible.cfg
hosts.yaml
install-zabbix-agent.yaml

Implementation

ansible.cfg

Lets begin with the ansible.cfg file. This is a specific configuration file for ansible and the current playbook.

[defaults]
inventory = hosts.yaml
host_key_checking = False
ansible_python_interpreter = /usr/bin/python3

Here we tell ansible, to look by default for the hosts.yaml file in the current directory.

hosts.yaml

This file specifies on which targets the playbook will be executed.

all:
    hosts:
        ubu-test.codesie.local:
        deb-test.codesie.local:

Debian.yaml & Ubuntu.yaml

These files contain different links for the zabbix repository package. Depending on the os, the according variable file will be loaded. Which var file is used, is determined in the install-zabbix-agent.yaml.

Debian.yaml
zabbix_version: 5.2
zabbix_repo_url: https://repo.zabbix.com/zabbix/{{ zabbix_version }}/debian/pool/main/z/zabbix-release/zabbix-release_{{ zabbix_version }}-1%2Bdebian10_all.deb
Ubuntu.yaml
zabbix_version: 5.2
zabbix_repo_url: https://repo.zabbix.com/zabbix/{{ zabbix_version }}/ubuntu/pool/main/z/zabbix-release/zabbix-release_{{ zabbix_version }}-1%2Bubuntu20.04_all.deb

zabbix_agentd.conf.j2

Is the zabbix_agentd.conf file, which will be edited for each host accordingly and copied on the target.

PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=10.200.1.80
ServerActive=127.0.0.1
Hostname={{ ansible_hostname }}
Include=/etc/zabbix/zabbix_agentd.d/*.conf

install-zabbix-agent.yaml

And here we are at the main part of the ansible playbook. All the files from above are used here.

---
- hosts: all
  gather_facts: yes
  tasks:
      - name: gather os specific variables
        include_vars: "{{ item }}"
        with_first_found:
            - "vars/{{ ansible_distribution }}.yaml"

      - name: Download zabbix repo package
        get_url:
            url: "{{ zabbix_repo_url}}"
            dest: /tmp/zabbix.deb

      - name: Install zabbix repo
        become: yes
        apt:
            deb: /tmp/zabbix.deb
            state: present

      - name: Install zabbix agent
        become: yes
        apt:
            name: zabbix-agent
            state: present
            update_cache: yes

      - name: Stop service zabbix-agent
        become: yes
        service:
            name: zabbix-agent
            state: stopped

      - name: Remove zabbix config file
        become: yes
        file:
            path: /etc/zabbix/zabbix_agentd.conf
            state: absent

      - name: Create new zabbix config file from template
        become: yes
        template:
            src: "templates/zabbix_agentd.conf.j2"
            dest: "/etc/zabbix/zabbix_agentd.conf"

      - name: Start service zabbix-agent
        become: yes
        service:
            name: zabbix-agent
            state: started

Here we determine which variable file to use, and install the zabbix agent.