Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chronicc opened this issue Oct 28, 2024 · 7 comments · May be fixed by #1425
Open

Comments

@chronicc
Copy link

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
  • Bug Report
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
Copy link
Collaborator

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
Copy link
Author

chronicc commented Nov 7, 2024

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
Copy link

dshuffin commented Nov 7, 2024

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: [zpc01] => 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: [zpc01 -> zwt01]: 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.

@dshuffin
Copy link

dshuffin commented Nov 22, 2024

The problem is in ansible itself or netcommon collection.
This playbook fails on task with delegation when ansible_connection is set to httpapi:

- name: test
  hosts: zpc01
  tasks:

    - name: Setting variable
      ansible.builtin.set_fact:
        test: 443

    - name: debug on host
      vars:
        ansible_connection: httpapi
        ansible_httpapi_port: "{{ test }}"
      debug:
        var: ansible_httpapi_port

    - name: debug with delegation without httpapi
      vars:
        ansible_httpapi_port: "{{ test }}"
      debug:
        var: ansible_httpapi_port
      delegate_to: localhost

    - name: debug with delegation
      vars:
        ansible_connection: httpapi
        ansible_httpapi_port: "{{ test }}"
      debug:
        var: ansible_httpapi_port
      delegate_to: localhost
TASK [Setting variable] ***********************************************************************************************************************************************************************************************
ok: [zpc01]

TASK [debug on host] **************************************************************************************************************************************************************************************************
ok: [zpc01] =>
  ansible_httpapi_port: '443'

TASK [debug with delegation without httpapi] **************************************************************************************************************************************************************************
ok: [zpc01 -> localhost] =>
  ansible_httpapi_port: '443'

TASK [debug with delegation] ******************************************************************************************************************************************************************************************
fatal: [zpc01 -> localhost]: FAILED! =>
  msg: '''test'' is undefined'

@BGmot
Copy link
Collaborator

BGmot commented Nov 22, 2024

Interesting observation.
What if you do

    - name: Setting variable
      ansible.builtin.set_fact:
        test: 443
      delegate_to: localhost

@dshuffin
Copy link

Tried the following, still fails at last task with same error message

    - name: Setting variable
      ansible.builtin.set_fact:
        test: 443
      delegate_to: localhost

    - name: Setting variable
      ansible.builtin.set_fact:
        test: 443

    - name: debug on host
      vars:
        ansible_connection: httpapi
        ansible_httpapi_port: "{{ test }}"
      debug:
        var: ansible_httpapi_port

    - name: debug with delegation without httpapi
      vars:
        ansible_httpapi_port: "{{ test }}"
      debug:
        var: ansible_httpapi_port
      delegate_to: localhost

    - name: debug with delegation
      vars:
        ansible_connection: httpapi
        ansible_httpapi_port: "{{ test }}"
      debug:
        var: ansible_httpapi_port
      delegate_to: localhost

@dshuffin
Copy link

Based on the discussion here: ansible/ansible#84374 created a pull request with a proposed fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants