dj-wasabi / ansible-zabbix-agent

Installing and maintaining zabbix-agent for RedHat/Debian/Ubuntu/Windows/Suse.
https://galaxy.ansible.com/dj-wasabi/zabbix-agent
MIT License
327 stars 249 forks source link

Table of Contents

Introduction

This role is migrated to: https://github.com/ansible-collections/community.zabbix/ In this repository, a read only version is/will be available for those who can not make use of collections (yet). Changes/updates will only be applied to the collection and not in this repository.

Requirements

Operating systems

This role will work on the following operating systems:

So, you'll need one of those operating systems.. :-) Please sent Pull Requests or suggestions when you want to use this role for other Operating systems.

Local system access

To successfully complete the install the role requires python-netaddr on the controller to be able to manage IP addresses. This requires that the library is available on your local machine (or that pip is installed to be able to run). This will likely mean that running the role will require sudo access to your local machine and therefore you may need the -K flag to be able to enter your local machine password if you are not running under root.

Zabbix Versions

See the following list of supported Operating systems with the Zabbix releases:

Zabbix 4.4

Zabbix 4.2

Zabbix 4.0

Zabbix 3.4

Zabbix 3.2

Zabbix 3.0

Zabbix 2.4

Zabbix 2.2

Getting started

Installation

Installing this role is very simple: ansible-galaxy install dj-wasabi.zabbix-agent

This will install the zabbix-agent role into your roles directory.

Minimal Configuration

In order to get the Zabbix Agent running, you'll have to define the following properties before executing the role:

The zabbix_agent_version is optional. The latest available major.minor version of Zabbix will be installed on the host(s). If you want to use an older version, please specify this in the major.minor format. Example: zabbix_agent_version: 4.0, zabbix_agent_version: 3.4 or zabbix_agent_version: 2.2.

The zabbix_agent_server (and zabbix_agent_serveractive) should contain the ip or fqdn of the host running the Zabbix Server.

Issues

Due to issue discussed on #291, the Ansible Version 2.9.{0,1,2} isn't working correctly on Windows related targets.

Role Variables

Main variables

There are some variables in default/main.yml which can (or need to) be overridden:

TLS Specific configuration

These variables are specific for Zabbix 3.0 and higher:

Zabbix API variables

These variables need to be overridden when you want to make use of the zabbix-api for automatically creating and or updating hosts.

Host encryption configuration will be set to match agent configuration.

When zabbix_api_create_hostgroup or zabbix_api_create_hosts is set to True, it will install on the host executing the Ansible playbook the zabbix-api python module.

Windows Variables

NOTE

Supporting Windows is a best effort (I don't have the possibility to either test/verify changes on the various amount of available Windows instances). PRs specific to Windows will almost immediately be merged, unless someone is able to provide a Windows test mechanism via Travis for Pull Requests.

macOS Variables

Docker Variables

When you don't want to install the Zabbix Agent on the host, but would like to run it in a container then these properties are useful. When zabbix_agent_docker is set to True, then a Docker image will be downloaded and a Container will be started. No other installations will be done on the host, with the exception of the PSK file and the "Zabbix Include Directory".

The following directories are mounted in the Container:

  - /etc/zabbix/zabbix_agentd.d:/etc/zabbix/zabbix_agentd.d
  - /:/hostfs:ro
  - /etc:/hostfs/etc:ro
  - /proc:/hostfs/proc:ro
  - /sys:/hostfs/sys:ro
  - /var/run:/var/run

Keep in mind that using the Zabbix Agent in a Container requires changes to the Zabbix Template for Linux as /proc, /sys and /etc are mounted in a directory /hostfs.

Other variables

IPMI variables

proxy

When the target host does not have access to the internet, but you do have a proxy available then the following properties needs to be set to download the packages via the proxy:

Dependencies

There are no dependencies on other roles.

Example Playbook

agent_interfaces

This will configure the Zabbix Agent interface on the host.

zabbix_agent_interfaces:
  - type: 1
    main: 1
    useip: "{{ zabbix_useuip }}"
    ip: "{{ zabbix_agent_ip }}"
    dns: "{{ ansible_fqdn }}"
    port: "{{ zabbix_agent_listenport }}"

Other interfaces

You can also configure the zabbix_agent_interfaces to add/configure snmp, jmx and ipmi interfaces.

You'll have to use one of the following type numbers when configuring it:

Type Interface Nr
Zabbix Agent 1
snmp 2
ipmi 3
jmx 4

Configuring a snmp interface will look like this:

zabbix_agent_interfaces:
  - type: 2
    main: 1
    useip: "{{ zabbix_useuip }}"
    ip: "{{ agent_ip }}"
    dns: "{{ ansible_fqdn }}"
    port: "{{ agent_listenport }}"

Vars in role configuration

Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:

- hosts: all
  roles:
     - role: dj-wasabi.zabbix-agent
       zabbix_agent_server: 192.168.33.30
       zabbix_agent_serveractive: 192.168.33.30
       zabbix_url: http://zabbix.example.com
       zabbix_api_use: true # use zabbix_api_create_hosts and/or zabbix_api_create_hostgroup from 0.8.0
       zabbix_api_user: Admin
       zabbix_api_pass: zabbix
       zabbix_create_host: present
       zabbix_host_groups:
         - Linux Servers
       zabbix_link_templates:
         - Template OS Linux
         - Apache APP Template
       zabbix_macros:
         - macro_key: apache_type
           macro_value: reverse_proxy

Combination of group_vars and playbook

You can also use the group_vars or the host_vars files for setting the variables needed for this role. File you should change: group_vars/all or host_vars/<zabbix_server> (Where is the hostname of the machine running Zabbix Server)

    zabbix_agent_server: 192.168.33.30
    zabbix_agent_serveractive: 192.168.33.30
    zabbix_url: http://zabbix.example.com
    zabbix_api_use: true # use zabbix_api_create_hosts and/or zabbix_api_create_hostgroup from 0.8.0
    zabbix_api_user: Admin
    zabbix_api_pass: zabbix
    zabbix_create_host: present
    zabbix_host_groups:
      - Linux Servers
    zabbix_link_templates:
      - Template OS Linux
      - Apache APP Template
    zabbix_macros:
      - macro_key: apache_type
        macro_value: reverse_proxy

and in the playbook only specifying:

- hosts: all
  roles:
     - role: dj-wasabi.zabbix-agent

Example for TLS PSK encrypted agent communication

Variables e.g. in the playbook or in host_vars/myhost:

zabbix_agent_tlsaccept: psk
zabbix_agent_tlsconnect: psk
zabbix_agent_tlspskidentity: "myhost PSK"
zabbix_agent_tlspsk_secret: b7e3d380b9d400676d47198ecf3592ccd4795a59668aa2ade29f0003abbbd40d
zabbix_agent_tlspskfile: /etc/zabbix/zabbix_agent_pskfile.psk

Molecule

This role is configured to be tested with Molecule. You can find on this page some more information regarding Molecule: https://werner-dijkerman.nl/2016/07/10/testing-ansible-roles-with-molecule-testinfra-and-docker/

With each Pull Request, Molecule will be executed via travis.ci. Pull Requests will only be merged once these tests run successfully.

There are 2 scenarios that are executed with Travis.

default

With the first scenario, Molecule will boot 5 Docker containers with the following OS'es:

This scenario will be doing a basic installation/configuration, without registering the host via the Zabbix API to the server.

with-server

The 2nd scenario will boot 4 Docker containers with the following OS'es:

First, a Zabbix Server will be installed on a container. This installation make uses of other dj-wasabi roles to install/configure a Zabbix Server. Once this instance is running, the 3 other agents are installed.

Each host will register itself on the Zabbix Server and the status should be 0 (This means the Zabbix Server and Zabbix Agent are connected).

The Ubuntu agent will register itself via a PSK, so that communication between the Zabbix Server and Zabbix Agent is encrypted with e Pre-Shared Key.

before-last-version

The 3rd and last scenario is the before-last-version. This is the same scenario like the default, but uses the previous Zabbix version.

Deploying Userparameters

The following steps are required to install custom userparameters and/or scripts:

Example:

- hosts: mysql_servers
  tasks:
    - include_role:
        name: dj-wasabi.zabbix-agent
      vars:
        zabbix_agent_server: zabbix.mydomain.com
        zabbix_agent_userparameters:
          - name: mysql
            scripts_dir: mysql
          - name: galera

Example of the "templates/userparameters/mysql.j2" file:

UserParameter=mysql.ping_to,mysqladmin -uroot ping | grep -c alive

License

MIT

Author Information

Please send suggestion or pull requests to make this role better. Also let me know if you encounter any issues installing or using this role.

Github: https://github.com/dj-wasabi/ansible-zabbix-agent

mail: ikben [ at ] werner-dijkerman . nl