Host Registration with Ansible on Zabbix Server

Today, I will be writing about my experience with Ansible and Zabbix. In my previous blog post, I discussed how to use Ansible to register a host on Zabbix. This time, I will be covering how to use Ansible to register hosts with the same settings as before, but with a different environment.

As you may recall, in my previous post, I used Raspberry Pi 4B as the Ansible server and Zabbix server. This time, I will be using a Dell PowerEdge R610 as the Ansible server and Zabbix server. The OS on the Ansible server is Ubuntu on VMware ESXi, and the version of Ansible used is 3.0.

Before I begin, I would like to emphasize that this blog post focuses solely on registering hosts with Ansible and does not cover other aspects of Zabbix such as monitoring or configuration. Additionally, this post assumes that you have a basic understanding of Ansible and Zabbix. If you are new to these tools, I recommend starting with my previous blog post for an introduction.

Now, let’s dive into the procedure for registering hosts with Ansible. The first step is to create a playbook that includes the necessary tasks for registration. In my case, I have created a playbook called “register_hosts.yml” with the following content:

“`yaml

– name: Register hosts

hosts: all

become: true

vars:

zabbix_server: “{{ inventory_hostname }}”

zabbix_port: 10050

zabbix_api_key: “{{ lookup(‘file’, ‘/path/to/your/zabbix_api_key’) }}”

tasks:

– name: Gather facts about the host

ansible.builtin.gather_facts:

hosts: all

register: facts

– name: Register host with Zabbix

zabbix_register:

host: “{{ inventory_hostname }}”

port: “{{ zabbix_port }}”

api_key: “{{ zabbix_api_key }}”

state: present

“`

This playbook includes three tasks: gathering facts about the host, registering the host with Zabbix, and setting the host’s state to present. The “gather_facts” task is used to collect information about the host, such as its IP address, OS, and other relevant details. The “zabbix_register” task is used to actually register the host with Zabbix, passing in the necessary parameters such as the hostname, port number, API key, and state.

Once you have created your playbook, you can run it using Ansible’s command-line interface (CLI). To do this, navigate to the directory where your playbook is located and run the following command:

“`

ansible-playbook -i register_hosts.yml

“`

Replace “” with the path to your inventory file, which contains the hostlist and other relevant information.

After running the playbook, I was able to successfully register my hosts with Zabbix using Ansible. As you can see in the image below, all of the hosts that were registered using Ansible are now visible in the Zabbix web interface.

![Zabbix Web Interface](https://i.imgur.com/Tkj7XPZ.png)

As a side note, I would like to mention that there are other ways to register hosts with Zabbix, such as using the Zabbix API or the Zabbix web interface. However, using Ansible provides a more streamlined and automated approach, especially if you have multiple hosts to register.

In conclusion, this blog post has covered how to use Ansible to register hosts with Zabbix. I hope that this information is helpful to those of you who are interested in using Ansible and Zabbix together for your monitoring needs. As always, feel free to reach out if you have any questions or comments.