ansible-collections / community.zabbix

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

Zabbix Agent: zabbix_api_server_port is undefined when hostgroup or host api creation is enabled #1410

Open chronicc opened 2 weeks ago

chronicc commented 2 weeks ago
SUMMARY

When running the zabbix-agent role against a linux node and the zabbix_api_create_hostgroup or the zabbix_api_create_host is set to true, the playbook fails with the error FAILED! => {"msg": "'zabbix_api_server_port' is undefined"} on the task API | Create host groups or API | Create a new host or update an existing host's info respectively.

ISSUE TYPE
COMPONENT NAME

Role: zabbix_agent

Part of requirements.yml ``` collections: - name: ansible.netcommon version: 7.1.0 - name: ansible.posix version: 1.5.4 - name: ansible.utils version: 4.1.0 - name: community.general version: 7.4.0 - name: community.zabbix version: 3.1.2 ```
requirements.lock ``` ansible==10.5.0 ansible-core==2.17.5 certifi==2024.8.30 cffi==1.17.1 charset-normalizer==3.4.0 cryptography==43.0.3 docker==7.1.0 idna==3.10 Jinja2==3.1.4 MarkupSafe==3.0.2 netaddr==1.3.0 packaging==24.1 pycparser==2.22 pyspnego==0.11.1 pywinrm==0.5.0 PyYAML==6.0.2 requests==2.32.3 requests_ntlm==1.3.0 resolvelib==1.0.1 urllib3==2.2.3 xmltodict==0.14.2 ```
requirements-dev.lock ``` ansible==10.5.0 ansible-compat==24.9.1 ansible-core==2.17.5 ansible-lint==24.9.2 attrs==24.2.0 black==24.10.0 bracex==2.5.post1 certifi==2024.8.30 cffi==1.17.1 cfgv==3.4.0 charset-normalizer==3.4.0 click==8.1.7 click-help-colors==0.9.4 cryptography==43.0.3 distlib==0.3.9 distro==1.9.0 docker==7.1.0 enrich==1.2.7 filelock==3.16.1 identify==2.6.1 idna==3.10 importlib_metadata==8.5.0 Jinja2==3.1.4 jsonschema==4.23.0 jsonschema-specifications==2024.10.1 markdown-it-py==3.0.0 MarkupSafe==3.0.2 mdurl==0.1.2 molecule==24.9.0 molecule-docker==2.1.0 netaddr==1.3.0 nodeenv==1.9.1 packaging==24.1 pathspec==0.12.1 platformdirs==4.3.6 pluggy==1.5.0 pre_commit==4.0.1 pycparser==2.22 Pygments==2.18.0 pyspnego==0.11.1 pywinrm==0.5.0 PyYAML==6.0.2 referencing==0.35.1 requests==2.32.3 requests_ntlm==1.3.0 resolvelib==1.0.1 rich==13.9.3 rpds-py==0.20.0 ruamel.yaml==0.18.6 ruamel.yaml.clib==0.2.12 selinux==0.3.0 subprocess-tee==0.4.2 urllib3==2.2.3 virtualenv==20.27.0 wcmatch==10.0 xmltodict==0.14.2 yamllint==1.35.1 zipp==3.20.2 ```
STEPS TO REPRODUCE

I've created two virtual machines with vagrant. The OS is Ubuntu 22.04. One machine is the zabbix server/agent (zabbix-1.vagrant) and the other machine is just an agent (ubuntu-2204.vagrant).

---
- name: Setup a zabbix agent.
  hosts: zabbix_agent
  become: true
  serial: 10
  roles:
    - role: community.zabbix.zabbix_agent
      zabbix_agent_server: zabbix-1.vagrant
      zabbix_agent_serveractive: zabbix-1.vagrant
      zabbix_api_server_host: zabbix-1.vagrant
      zabbix_api_login_user: Admin
      zabbix_api_login_pass: zabbix
      zabbix_api_create_hostgroup: true
      zabbix_api_create_hosts: true
      zabbix_host_groups:
        - Linux Servers
      zabbix_agent_tags:
        - tag: environment
          value: development
EXPECTED RESULTS

Hostgroup and host api calls are successful.

ACTUAL RESULTS

The zabbix agent is installed successfully but the api calls for hostgroup and host generation/update fail.

TASK [community.zabbix.zabbix_agent : API | Create a new host or update an existing host's info] **********************************************************************************************
task path: /home/chronicc/.ansible/collections/ansible_collections/community/zabbix/roles/zabbix_agent/tasks/api.yml:15
Monday 28 October 2024  12:38:14 +0100 (0:00:00.024)       0:00:08.903 ******** 
Monday 28 October 2024  12:38:14 +0100 (0:00:00.024)       0:00:08.903 ******** 
redirecting (type: connection) ansible.builtin.httpapi to ansible.netcommon.httpapi
Loading collection ansible.netcommon from /home/chronicc/.ansible/collections/ansible_collections/ansible/netcommon
Loading collection ansible.utils from /home/chronicc/.ansible/collections/ansible_collections/ansible/utils
fatal: [ubuntu-2204 -> zabbix-1.vagrant]: FAILED! => {
    "msg": "'zabbix_api_server_port' is undefined"
}

PLAY RECAP ************************************************************************************************************************************************************************************
ubuntu-2204                : ok=24   changed=0    unreachable=0    failed=1    skipped=24   rescued=0    ignored=0
masa-orca commented 1 week ago

Will you show whole log to me? The zabbix_api_server_port variable will be defined in this task. I think the task was skipped.

chronicc commented 1 week ago

Did the same run with the zabbix_proxy.

# Zabbix Proxy Setup
#
---
- name: Setup a zabbix proxy.
  hosts: zabbix_proxy
  become: true
  roles:
    - role: geerlingguy.mysql
      mysql_users:
        - name: zabbix_proxy
          password: zabbix_proxy
    - role: community.zabbix.zabbix_proxy
      zabbix_proxy_server: zabbix-1.vagrant;zabbix-2.vagrant
      zabbix_api_server_host: zabbix-1.vagrant
      zabbix_api_login_user: Admin
      zabbix_api_login_pass: zabbix
      zabbix_api_create_proxy: true
      zabbix_proxy_name: '{{ inventory_hostname }}'
    - role: community.zabbix.zabbix_agent
      zabbix_agent_package_remove: true
      zabbix_agent_server: '{{ zabbix_agent_server_overwrite | default("zabbix-1.vagrant,zabbix-2.vagrant") }}'
      zabbix_agent_serveractive: '{{ zabbix_agent_serveractive_overwrite | default("zabbix-1.vagrant;zabbix-2.vagrant") }}'
      zabbix_agent2: true
      zabbix_api_create_hostgroup: false
      zabbix_api_create_hosts: false
      zabbix_api_login_pass: zabbix
      zabbix_api_login_user: Admin
      zabbix_api_server_host: zabbix-1.vagrant
      zabbix_agent_tags:
        - tag: environment
          value: development

ansible_zabbix_proxy.log

The same error message is thrown.

dshuffin commented 1 week ago

Same issue with zabbix_proxy role when zabbix_api_create_proxy is set. The task setting the variable succeeds:

TASK [community.zabbix.zabbix_proxy : Setting Zabbix API Server Port] **********************
ok: [admitrzpc01.rmd.remedy.fi] => changed=false
  ansible_facts:
    zabbix_api_server_port: '80'

but the proxy registration task still fails:

TASK [community.zabbix.zabbix_proxy : Ensure proxy definition is up-to-date (added/updated/removed)] **************************************************************************************************************************************
fatal: [admitrzpc01.rmd.remedy.fi -> admitrzwt01.rmd.remedy.fi]: FAILED! => 
  msg: '''zabbix_api_server_port'' is undefined'

However setting zabbix_api_server_port variable in host_vars of host set in zabbix_api_server_host fixes the issue. I think it's because task is delegated to another host: delegate_to: "{{ zabbix_api_server_host }}" , so variables set for the current host in play no longer apply.