Ansible

Ansible Zabbix

Ansible is a powerful open-source automation tool that empowers the DevOps engineers to manage and automate the tasks for server configuration and application deployment.

As Ansible’s ecosystem has grown, so has its collection of community-contributed modules that are designed to interact with various services and tools. One such collection is the “community.zabbix” module which focuses on automating the tasks related to Zabbix.

In this tutorial, we will learn how to use the “community.zabbix” module to interact with and automate the tasks in a Zabbix environment.

Prerequisites:

Ensure that you have the following tools installed:

  • A working installation of Ansible
  • A running Zabbix server and agent

Install the Zabbix Module

Before using this module, we must ensure that the Zabbix collection is installed on the controller node.

You can run the following command to accomplish this:

$ ansible-galaxy collection install community.zabbix

Authenticating with Zabbix

Nearly all the modules in the “community.zabbix” requires authentication to the Zabbix server. By default, you need the following credentials:

  • server_url – URL of your Zabbix server
  • login_user – Zabbix login username
  • login_password – Zabbix login password

Supported Modules

The “community.zabbix” collection supports the following collections and their description:

  • zabbix_action – The module to manage the Zabbix actions (Create/Delete/Update).
  • zabbix_authentication – The module to handle the Zabbix authentication updates.
  • zabbix_autoregister – The module to update the Zabbix autoregistration.
  • zabbix_discovery_rule – The module to manage the Zabbix discovery rules (Create/Delete/Update).
  • zabbix_globalmacro – The module to create, update, and delete the Zabbix Global macros.
  • zabbix_group – The module to create and delete the Zabbix host groups.
  • zabbix_group_info – The module to gather information about the Zabbix host groups.
  • zabbix_host – The module to create, update, and delete the Zabbix hosts.
  • zabbix_host_events_info – The module to get all related triggers to a Zabbix host.
  • zabbix_host_info – The module to gather information about the Zabbix hosts.
  • zabbix_hostmacro – The module to create, update, and delete the Zabbix host macros.
  • zabbix_housekeeping – The module to update the Zabbix housekeeping configurations.
  • zabbix_maintenance – The module to create the Zabbix maintenance windows.
  • zabbix_map – The module to create, update, and delete the Zabbix maps.
  • zabbix_mediatype – The module to manage the Zabbix media types (Create/Update/Delete).
  • zabbix_proxy – The module to create, delete, get, and update the Zabbix proxies.
  • zabbix_proxy_info – The module to gather information about the Zabbix proxies.
  • zabbix_regexp – The module to create, update, and delete the Zabbix regular expressions.
  • zabbix_script – The module to create, update, and delete the Zabbix scripts.
  • zabbix_service – The module to create, update, and delete the Zabbix services.
  • zabbix_settings – The module to update the Zabbix global settings.
  • zabbix_template – The module to create, update, and delete the Zabbix templates.
  • zabbix_template_info – The module to gather information about the Zabbix templates.
  • zabbix_token – The module to manage the Zabbix tokens (Create/Update/Generate/Delete).
  • zabbix_user – The module to create, update, and delete the Zabbix users.
  • zabbix_user_directory – The module to create, update, and delete the Zabbix user directories.
  • zabbix_user_info – The module to gather information about Zabbix users.
  • zabbix_user_role – The module to add or remove the Zabbix roles for users.
  • zabbix_usergroup – The module to create, delete, and update the Zabbix user groups.
  • zabbix_valuemap – The module to create, update, and delete the Zabbix value maps.

Examples:

Let us explore some basic examples on how to use some common modules provided in the Zabbix collection.

Example 1: Registering a Zabbix Host

The zabbix_host module allows us to manage the Zabbix hosts. An example playbook is as follows:

- name: Add a new Zabbix host
  community.zabbix.zabbix_host:
    server_url: http://localhost: 10051/zabbix
    login_user: zabbix
    login_password: password
    host_name: Server1
    host_groups:
      - Linux servers
    interfaces:
      - type: 1
        main: 1
        useip: 1
        ip: 10.10.10.10
        dns: ""
        port: "10050"
    state: present

Example 2: Managing the Zabbix Templates

Next, we have the zabbix_template module which allows us to manage the Zabbix templates. An example playbook is as follows:

- name: Link a template to the host
  community.zabbix.zabbix_template:
    server_url: http://localhost:10051/zabbix
    login_user: zabbix
    login_password: password
    host_name: Server1
    template_name: Template OS Linux
    state: linked

Example 3: Triggering the Zabbix Maintenance

With the zabbix_maintenance module, we can set up the maintenance periods as demonstrated in the following example:

- name: Create a maintenance period
  community.zabbix.zabbix_maintenance:
    server_url: http://localhost:10051/zabbix
    login_user: zabbix
    login_password: password
    name: SundayMaintenance
    start_time: "2023-08-06 22:00:00"
    stop_time: "2023-08-07 02:00:00"
    host_names:
      - Server1
    state: present

Conclusion

We explored the most fundamental functionality of the “community.zabbix” collection. We also covered some common usage of the various modules in the collection. This tutorial only scratches the surface of the capabilities of the collection. Feel free to explore the docs for expansive functionality.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list