ansible-collections / community.zabbix

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

macros are not set #925

Closed markuman closed 1 year ago

markuman commented 1 year ago
SUMMARY

I am using community.zabbix 1.9.2 with zabbix 6.0 LTS server and try to add an host and set some macros.

- name: configure zabbix localhost hosts
  community.zabbix.zabbix_host:
    host_name: zabbix.mydomain.tld
    interfaces:
      - type: 1
        main: 1
        useip: 0
        dns: localhost
        port: "10050"
    host_groups:
      - letsencrypt
    link_templates:
      - Website certificate by Zabbix agent 2
    status: enabled
    state: present
    tls_connect: 1
    tls_accept: 1
    tags:
      - tag: env
        value: prod
    macros:
      - macro: '{$CERT.EXPIRY.WARN}'
        value: '14'
      - macro: '{$CERT.WEBSITE.HOSTNAME}'
        value: 'zabbix.mydomain.tld'
      - macro: '{$CERT.WEBSITE.IP}'
        value: "{{ lookup('dig', 'zabbix.mydomain.tld') }}"
      - macro: '{$CERT.WEBSITE.PORT}'
        value: '443'

The host is set correctly, but the macros are ignored and are empty.

ISSUE TYPE
COMPONENT NAME

zabbix_host

ANSIBLE VERSION
ansible [core 2.13.5]
  config file = None
  configured module search path = ['/home/m/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/m/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/m/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/m/.local/bin/ansible
  python version = 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]
  jinja version = 3.1.2
  libyaml = False
CONFIGURATION
OS / ENVIRONMENT / Zabbix Version
STEPS TO REPRODUCE

see example task above.

EXPECTED RESULTS
TASK [zabbix : debug] *********************************************************************************************************************************************
ok: [localhost] => {
    "out": {
        "changed": false,
        "failed": false,
        "hosts": [
            {
                "auto_compress": "1",
                "custom_interfaces": "0",
                "description": "",
                "flags": "0",
                "groups": [
                    {
                        "flags": "0",
                        "groupid": "23",
                        "internal": "0",
                        "name": "letsencrypt",
                        "uuid": "2426d455c22c4c1f8be2a4c14d1aa785"
                    }
                ],
                "host": "zabbix.mydomain.tld",
                "hostid": "10627",
                "hostinterfaces": [
                    {
                        "available": "0",
                        "details": [],
                        "disable_until": "0",
                        "dns": "localhost",
                        "error": "",
                        "errors_from": "0",
                        "hostid": "10627",
                        "interfaceid": "78",
                        "ip": "",
                        "main": "1",
                        "port": "10050",
                        "type": "1",
                        "useip": "0"
                    }
                ],
                "inventory": [],
                "inventory_mode": "-1",
                "ipmi_authtype": "-1",
                "ipmi_password": "",
                "ipmi_privilege": "2",
                "ipmi_username": "",
                "lastaccess": "0",
                "macros": [
                    {
                        "description": "",
                        "hostid": "10627",
                        "hostmacroid": "5862",
                        "macro": "{$CERT.EXPIRY.WARN}",
                        "type": "0",
                        "value": "14"
                    },
                    {
                        "description": "",
                        "hostid": "10627",
                        "hostmacroid": "5863",
                        "macro": "{$CERT.WEBSITE.HOSTNAME}",
                        "type": "0",
                        "value": "zabbix.mydomain.tld"
                    },
                    {
                        "description": "",
                        "hostid": "10627",
                        "hostmacroid": "5864",
                        "macro": "{$CERT.WEBSITE.IP}",
                        "type": "0",
                        "value": "10.112.60.61"
                    },
                    {
                        "description": "",
                        "hostid": "10627",
                        "hostmacroid": "5865",
                        "macro": "{$CERT.WEBSITE.PORT}",
                        "type": "0",
                        "value": "443"
                    }
                ],
                "maintenance_from": "0",
                "maintenance_status": "0",
                "maintenance_type": "0",
                "maintenanceid": "0",
                "name": "zabbix.mydomain.tld",
                "parentTemplates": [
                    {
                        "name": "Website certificate by Zabbix agent 2",
                        "templateid": "10413"
                    }
                ],
                "proxy_address": "",
                "proxy_hostid": "0",
                "status": "0",
                "tags": [
                    {
                        "tag": "env",
                        "value": "prod"
                    },
                    {
                        "tag": "target",
                        "value": "www"
                    }
                ],
                "templateid": "0",
                "tls_accept": "1",
                "tls_connect": "1",
                "tls_issuer": "",
                "tls_subject": "",
                "uuid": ""
            }
        ],
        "ok": true
    }
}
ACTUAL RESULTS

the host_info module returns below after it was set.

TASK [zabbix : debug] *********************************************************************************************************************************************
ok: [localhost] => {
    "out": {
        "changed": false,
        "failed": false,
        "hosts": [
            {
                "auto_compress": "1",
                "custom_interfaces": "0",
                "description": "",
                "flags": "0",
                "groups": [
                    {
                        "flags": "0",
                        "groupid": "23",
                        "internal": "0",
                        "name": "letsencrypt",
                        "uuid": "2426d455c22c4c1f8be2a4c14d1aa785"
                    }
                ],
                "host": "zabbix.mydomain.tld",
                "hostid": "10628",
                "hostinterfaces": [
                    {
                        "available": "0",
                        "details": [],
                        "disable_until": "0",
                        "dns": "localhost",
                        "error": "",
                        "errors_from": "0",
                        "hostid": "10628",
                        "interfaceid": "79",
                        "ip": "",
                        "main": "1",
                        "port": "10050",
                        "type": "1",
                        "useip": "0"
                    }
                ],
                "inventory": [],
                "inventory_mode": "-1",
                "ipmi_authtype": "-1",
                "ipmi_password": "",
                "ipmi_privilege": "2",
                "ipmi_username": "",
                "lastaccess": "0",
                "macros": [],
                "maintenance_from": "0",
                "maintenance_status": "0",
                "maintenance_type": "0",
                "maintenanceid": "0",
                "name": "zabbix.mydomain.tld",
                "parentTemplates": [
                    {
                        "name": "Website certificate by Zabbix agent 2",
                        "templateid": "10413"
                    }
                ],
                "proxy_address": "",
                "proxy_hostid": "0",
                "status": "0",
                "tags": [
                    {
                        "tag": "env",
                        "value": "prod"
                    },
                    {
                        "tag": "target",
                        "value": "www"
                    }
                ],
                "templateid": "0",
                "tls_accept": "1",
                "tls_connect": "1",
                "tls_issuer": "",
                "tls_subject": "",
                "uuid": ""
            }
        ],
        "ok": true
    }
}
BGmot commented 1 year ago

I can't reproduce.

BGmot commented 1 year ago

How did you get the output with expected results?

markuman commented 1 year ago

with the zabbix_hostmacro module.

- name: host macros for zabbix.lekker.de
  with_items:
    - macro: '{$CERT.EXPIRY.WARN}'
      value: '14'
    - macro: '{$CERT.WEBSITE.HOSTNAME}'
      value: 'zabbix.mydomain.tld'
    - macro: '{$CERT.WEBSITE.IP}'
      value: "{{ lookup('dig', 'zabbix.mydomain.tld') }}"
    - macro: '{$CERT.WEBSITE.PORT}'
      value: '443'
  community.zabbix.zabbix_hostmacro:
    host_name: zabbix.mydomain.tld
    macro_name: "{{ item.macro }}"
    macro_value: "{{ item.value }}"
    state: present
BGmot commented 1 year ago

How I am trying to re-produce:

(env_py39_ans213) ey@ls1:~/ansible$ cat module_test1.yml 
(env_py39_ans213) ey@ls1:~/ansible$ cat module_test1.yml 
- hosts: all
  gather_facts: false
  tasks:
  - name: Create host group
    zabbix_group:
      host_groups:
         - letsencrypt

  - name: Create host
    zabbix_host:
      host_name: Example Host
      interfaces:
        - type: 1
          main: 1
          useip: 0
          dns: localhost
          port: "10050"
      host_groups:
        - letsencrypt
      link_templates:
        - Website certificate by Zabbix agent 2
      status: enabled
      state: present
      tls_connect: 1
      tls_accept: 1
      tags:
        - tag: env
          value: prod
      macros:
        - macro: '{$CERT.EXPIRY.WARN}'
          value: '14'
        - macro: '{$CERT.WEBSITE.HOSTNAME}'
          value: 'zabbix.mydomain.tld'
        - macro: '{$CERT.WEBSITE.IP}'
          value: '1.1.1.1'
        - macro: '{$CERT.WEBSITE.PORT}'
          value: '443'

Then running:

(env_py39_ans213) ey@ls1:~/ansible$ ansible-playbook -i inventories/modules_test.yml module_test1.yml -vv
<snip>
changed: [ls1] => {
    "changed": true,
    "invocation": {
        "module_args": {
            "ca_cert": null,
            "description": null,
            "force": true,
            "host_groups": [
                "letsencrypt"
            ],
            "host_name": "Example Host",
            "http_login_password": null,
            "http_login_user": null,
            "interfaces": [
                {
                    "details": {},
                    "dns": "localhost",
                    "ip": "",
                    "main": 1,
                    "port": "10050",
                    "type": 1,
                    "useip": 0
                }
            ],
            "inventory_mode": null,
            "inventory_zabbix": null,
            "ipmi_authtype": null,
            "ipmi_password": null,
            "ipmi_privilege": null,
            "ipmi_username": null,
            "link_templates": [
                "Website certificate by Zabbix agent 2"
            ],
            "login_password": null,
            "login_user": null,
            "macros": [
                {
                    "description": "",
                    "macro": "{$CERT.EXPIRY.WARN}",
                    "type": "0",
                    "value": "14"
                },
                {
                    "description": "",
                    "macro": "{$CERT.WEBSITE.HOSTNAME}",
                    "type": "0",
                    "value": "zabbix.mydomain.tld"
                },
                {
                    "description": "",
                    "macro": "{$CERT.WEBSITE.IP}",
                    "type": "0",
                    "value": "1.1.1.1"
                },
                {
                    "description": "",
                    "macro": "{$CERT.WEBSITE.PORT}",
                    "type": "0",
                    "value": "443"
                }
            ],
            "proxy": null,
            "server_url": null,
            "state": "present",
            "status": "enabled",
            "tags": [
                {
                    "tag": "env",
                    "value": "prod"
                }
            ],
            "timeout": null,
            "tls_accept": 1,
            "tls_connect": 1,
            "tls_psk": null,
            "tls_psk_identity": null,
            "tls_subject": null,
            "validate_certs": null,
            "visible_name": null
        }
    },
    "result": "Successfully added host Example Host (None) and linked with template '['Website certificate by Zabbix agent 2']'"
}

and I see all the macros in the host.

(env_py39_ans213) ey@ls1:~/ansible$ ansible --version
ansible [core 2.13.0]
  config file = /home/ey/ansible/ansible.cfg
  configured module search path = ['/home/ey/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/ey/env_py39_ans213/lib/python3.9/site-packages/ansible
  ansible collection location = /home/ey/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/ey/env_py39_ans213/bin/ansible
  python version = 3.9.5 (default, Nov 23 2021, 15:27:38) [GCC 9.3.0]
  jinja version = 3.1.2
  libyaml = True

(env_py39_ans213) ey@ls1:~/ansible$ ansible-galaxy collection list | grep zabb community.zabbix 1.9.2



Please provide your `ansible --version` and `ansible-galaxy collection list | grep zabbix`, also provide full output of ansible-playbook run with `-vvv`
markuman commented 1 year ago

hmmmm I cannot reproduce it myself anymore...
sorry for your wasted time!