How to add a new template to an existing host

Hi all,

I run the following playbook to add a new template to an existing host but without success.

The playbook:

- name: Set variables
set_fact:
zabbix_api_token: 60460362b83e4407f76863c40c60f082c375d0ddccdd220108c246f0efc26e2e
ansible_network_os: community.zabbix.zabbix
ansible_connection: httpapi
ansible_httpapi_port: 443
ansible_httpapi_use_ssl: true
ansible_httpapi_validate_certs: true
ansible_host: zabbix.server.ch
ansible_zabbix_url_path: monitoring

- name: Set API token
set_fact:
ansible_zabbix_auth_key: "{{ zabbix_api_token }}"
- name: Add template UNIFR RedHat to '{{ var_hosts }}'
community.zabbix.zabbix_host:
host_name: "{{ var_hosts }}"
link_templates:
- "UNIFR RedHat"
delegate_to: localhost`

My error :

"msg": "connection error occurred: REST API returned {'code': -32602, 'message': 'Invalid params.', 'data': 'Incorrect arguments passed to function.'} when sending {\"jsonrpc\": \"2.0\", \"method\": \"host.update\", \"id\": \"8a7590a9-dfd8-4eda-8ea9-14a7e3f51744\", \"params\": {\"hostid\": \"10667\", \"groups\": [{\"groupid\": \"2\"}], \"status\": 0, \"monitored_by\": 0, \"interfaces\": [{\"main\": 1, \"useip\": 1, \"ip\": \"134.21.83.154\", \"dns\": \"\", \"port\": \"10050\", \"interfaceid\": \"97\", \"hostid\": \"10667\", \"type\": 1, \"available\": \"1\", \"error\": \"\", \"errors_from\": \"0\", \"disable_until\": \"0\", \"details\": {\"version\": 2, \"bulk\": 1, \"community\": \"\", \"securityname\": \"\", \"contextname\": \"\", \"securitylevel\": 0, \"authprotocol\": 0, \"authpassphrase\": \"\", \"privprotocol\": 0, \"privpassphrase\": \"\"}}]}, \"auth\": \"60460362b83e4407f76863c40c60f082c375d0ddccdd220108c246f0efc26e2e\"}", 

What is wrong ?
I need help.
Regards
Olivier

Welcome to the forum, @Olivier_Studer!

The examples in the community.zabbix.zabbix_host collection are not super intuitive, but this should work:

- name: Add template UNIFR RedHat to '{{ var_hosts }}'
  community.zabbix.zabbix_host:
    host_name: "{{ var_hosts }}"
    link_templates:
      - "UNIFR RedHat"
  delegate_to: zabbix.server.ch
  vars:
    ansible_network_os: community.zabbix.zabbix
    ansible_connection: httpapi
    ansible_httpapi_port: 443
    ansible_httpapi_use_ssl: true
    ansible_httpapi_validate_certs: true
    ansible_host: zabbix.server.ch
    ansible_zabbix_url_path: monitoring
    ansible_zabbix_auth_key: 60460362b83e4407f76863c40c60f082c375d0ddccdd220108c246f0efc26e2e

However, you might want to add the zabbix host to your inventory

$ cat ansible-inventory/host_vars/zabbix-server.yml
---
ansible_network_os: community.zabbix.zabbix
ansible_connection: httpapi
ansible_httpapi_port: 443
ansible_httpapi_use_ssl: true
ansible_httpapi_validate_certs: true
ansible_host: zabbix.server.ch
ansible_zabbix_url_path: monitoring
ansible_zabbix_auth_key: 60460362b83e4407f76863c40c60f082c375d0ddccdd220108c246f0efc26e2e 

so you don’t have to specify all those variables in your playbook for every zabbix related task:

- name: Add template UNIFR RedHat to '{{ var_hosts }}'
  community.zabbix.zabbix_host:
    host_name: "{{ var_hosts }}"
    link_templates:
      - "UNIFR RedHat"
  delegate_to: zabbix-server

You should also consider encrypting the api token with ansible vault.

1 Like