ansible-collections / community.zabbix

Zabbix Ansible modules
http://galaxy.ansible.com/community/zabbix
Other
329 stars 285 forks source link

Host creation fails with "Template not found", template exists #1150

Closed richlv closed 10 months ago

richlv commented 10 months ago
SUMMARY

Host creation fails with "Template not found: {'name': 'MAS_Services'}" despite the mentioned template existing on the target server.

ISSUE TYPE
COMPONENT NAME

zabbix_host

ANSIBLE VERSION
ansible [core 2.15.6]
  config file = /opt/ansible/config/ansible-config-server.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/venv-ansible2023/lib64/python3.9/site-packages/ansible
  ansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/venv-ansible2023/bin/ansible
  python version = 3.9.16 (main, Sep 22 2023, 17:57:55) [GCC 8.5.0 20210514 (Red Hat 8.5.0-18)] (/opt/venv-ansible2023/bin/python3.9)
  jinja version = 3.1.2
  libyaml = True
CONFIGURATION
CONFIG_FILE() = /opt/ansible/config/ansible-config-server.cfg
DEFAULT_FORKS(/opt/ansible/config/ansible-config-server.cfg) = 100
DEFAULT_HOST_LIST(/opt/ansible/config/ansible-config-server.cfg) = ['/opt/ansible/inventory/cert/hosts']
DEFAULT_VAULT_PASSWORD_FILE(/opt/ansible/config/ansible-config-server.cfg) = /opt/ansible/keys/vault_token-nonprod
HOST_KEY_CHECKING(/opt/ansible/config/ansible-config-server.cfg) = False
RETRY_FILES_ENABLED(/opt/ansible/config/ansible-config-server.cfg) = False
OS / ENVIRONMENT / Zabbix Version

Zabbix 6.4.10.

STEPS TO REPRODUCE

A host definition as below.

hosts:
  - host_name: hostname.domain
    visible_name: hostname
    status: enabled
    state: present
    link_templates:
      - name: MAS_Services
      - name: HS Template OS Linux
    host_groups:
      - Discovered hosts
      - Another group
      - One more group
    interfaces:
      - type: agent
        main: 1
        useip: '0'
        ip: 10.11.12.13
        dns: hostname
        port: '10050'
    macros:
      - macro: '{$MACRO}'
        value: aaa
EXPECTED RESULTS

Host is created.

ACTUAL RESULTS
Template not found: {'name': 'MAS_Services'}

Adding -vvvv does not seem to provide any additional detail, the template linkage section looks like this:

        "link_templates": [
            {
                "name": "MAS_Services"
            },
            {
                "name": "HS Template OS Linux"
            }
        ],

image

I might be missing something obvious, but having verified everything several times cannot see anything:

As the error message is produced right away, it seems like the module is throwing this error instead of the Zabbix API.

BGmot commented 10 months ago

How do you actually pass this to zabbix_host module?

richlv commented 10 months ago
- name: Add the ad-hoc hosts
  zabbix_host:
    "{{item}}"
  loop: "{{hosts + generic_hosts}}"
  become: false
  vars:
    ansible_connection: httpapi
    ansible_user: username

Other connection parameters are specified in variables. This works for all other hosts, just this one complains about this one template.

BGmot commented 10 months ago

Templates should be defined this way

    link_templates:
      - MAS_Services
      - HS Template OS Linux
richlv commented 10 months ago

Ouch. Well, that explains why it worked for other hosts, as those did not have this extra "name: " part... Thank you so much for spotting this, I had stared at it for way too long...

Closing as user error.

richlv commented 10 months ago

Looks like it tried to use hash as a template name? Perhaps longer term some validation (template names to be strings only) is feasible to prevent silly users from harming themselves :)