ansible-collections / community.zabbix

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

Create new host group fails in check mode (`name 'Already_Exists' is not defined`) #856

Open Vanav opened 1 year ago

Vanav commented 1 year ago

zabbix_group fails in check mode, if host group is not existing yet.

Task:

  - name: "API | Create host groups"
    zabbix_group:
      host_group:
      - Other servers3
    vars:
      ansible_connection: httpapi
      ansible_network_os: community.zabbix.zabbix
      ansible_user: '{{ zabbix_api_login_user }}'
      ansible_httpapi_pass: '{{ zabbix_api_login_pass }}'
      ansible_httpapi_use_ssl: '{{ (zabbix_api_server_url | urlsplit("scheme") == "https") | ternary(true, false) }}'
    delegate_to: '{{ zabbix_api_server_url | urlsplit("hostname") }}'

Command: ansible-playbook site.yml -l prod_hosts --check -vvv

Result:

TASK [zabbix : API | Create host groups] [CHECK MODE] **** task path: /srv/Projects/example.com/infra/roles/zabbix/tasks/main.yml:157 redirecting (type: connection) ansible.builtin.httpapi to ansible.netcommon.httpapi redirecting (type: modules) ansible.builtin.zabbix_group to community.zabbix.zabbix_group redirecting (type: modules) ansible.builtin.zabbix_group to community.zabbix.zabbix_group Using module file /opt/pipx/venvs/ansible/lib/python3.10/site-packages/ansible_collections/community/zabbix/plugins/modules/zabbix_group.py Pipelining is enabled.

ESTABLISH LOCAL CONNECTION FOR USER: root EXEC /bin/sh -c '/opt/pipx/venvs/ansible/bin/python && sleep 0' fatal: [prod_host_node -> zabbix.example.com]: FAILED! => changed: false module_stderr: '' module_stdout: |4- {"changed": true, "invocation": {"module_args": {"host_group": ["Other servers3"], "host_groups": ["Other servers3"], "timeout": 10, "validate_certs": true, "state": "present", "server_url": null, "login_user": null, "login_password": null}}, "warnings": ["Option \"server_url\" is deprecated with the move to httpapi connection and will be removed in the next release", "Option \"login_user\" is deprecated with the move to httpapi connection and will be removed in the next release", "Option \"login_password\" is deprecated with the move to httpapi connection and will be removed in the next release", "Option \"timeout\" is deprecated with the move to httpapi connection and will be removed in the next release", "Option \"validate_certs\" is deprecated with the move to httpapi connection and will be removed in the next release"]} {"failed": true, "msg": "Failed to create host group(s): name 'Already_Exists' is not defined", "exception": " File \"/tmp/ansible_zabbix_group_payload_ea42hbg6/ansible_zabbix_group_payload.zip/ansible_collections/community/zabbix/plugins/modules/zabbix_group.py\", line 119, in create_host_group\n", "invocation": {"module_args": {"host_group": ["Other servers3"], "host_groups": ["Other servers3"], "timeout": 10, "validate_certs": true, "state": "present", "server_url": null, "login_user": null, "login_password": null}}, "warnings": ["Option \"server_url\" is deprecated with the move to httpapi connection and will be removed in the next release", "Option \"login_user\" is deprecated with the move to httpapi connection and will be removed in the next release", "Option \"login_password\" is deprecated with the move to httpapi connection and will be removed in the next release", "Option \"timeout\" is deprecated with the move to httpapi connection and will be removed in the next release", "Option \"validate_certs\" is deprecated with the move to httpapi connection and will be removed in the next release"]} msg: |- MODULE FAILURE See stdout/stderr for the exact error rc: 1 Environment: ``` ansible [core 2.14.1] config file = None configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /opt/pipx/venvs/ansible/lib/python3.10/site-packages/ansible ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections executable location = /opt/pipx/bin/ansible python version = 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] (/opt/pipx/venvs/ansible/bin/python) jinja version = 3.1.2 libyaml = True community.zabbix 1.9.0 ANSIBLE_PIPELINING(/srv/Projects/xxx/infra/ansible.cfg) = True CONFIG_FILE() = /srv/Projects/xxx/infra/ansible.cfg DEFAULT_FORCE_HANDLERS(/srv/Projects/xxx/infra/ansible.cfg) = True DEFAULT_JINJA2_NATIVE(/srv/Projects/xxx/infra/ansible.cfg) = True DIFF_ALWAYS(/srv/Projects/xxx/infra/ansible.cfg) = True INTERPRETER_PYTHON(/srv/Projects/xxx/infra/ansible.cfg) = auto Zabbix 6.2.6 ```
optica-phoffmann commented 3 months ago

I was able to reproduce this issue. This issue is related to special local Ansible configurations. This issue is triggered when working with Python virtualenvs. OP is working with virtualenv as can be seen by the python location /opt/pipx/venvs/ansible/bin/python

How to reproduce:

Solution: When working with delegate_to unexpected things happen to the ansible_python_interpreter. Please set the following environment variable to fix the issue.

export ANSIBLE_PYTHON_INTERPRETER="$(which python)"

To find out what is going on, you need to open the zabbix_group.py file in your installed zabbix collection and replace the very unhelpful code. Current code:

try:
    from zabbix_api import Already_Exists

    HAS_ZABBIX_API = True
    ZBX_IMP_ERR = Exception()
except ImportError:
    ZBX_IMP_ERR = traceback.format_exc()
    HAS_ZABBIX_API = False

Replace it with this:

from zabbix_api import Already_Exists

If you run the playbook again, you will get a new error which shows you that Ansible is using a different Python environment where the python package zabbix-api is not installed!

I can't tell which Ansible versions are affected. This fascinating behaviour cannot be fixed inside the the zabbix collection. However, it would be helpful to let the zabbix modules fail if python package zabbix-api cannot be found.