ansible-collections / community.zabbix

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

Ansible task to create Zabbix host fails with "Invalid JSON response: 404 Not Found" error #1391

Closed amikha33 closed 1 day ago

amikha33 commented 1 week ago

While running an Ansible playbook to create or update a Zabbix host using the community.zabbix.zabbix_host module, the task fails with a 404 Not Found error. It seems that the module is receiving an invalid JSON response from the Zabbix API, potentially indicating an incorrect or missing URL endpoint.

Environment

Error Details

TASK [Create or update Zabbix host] **** An exception occurred during task execution. To see the full traceback, use -vvv. The error was: fatal: [localhost -> 127.0.0.1]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File "/home/master/.ansible/tmp/ansible-local-12047lmk8iodo/ansible-tmp-1726041988.1483855-12103-236711783000956/AnsiballZ_zabbix_host.py", line 107, in \n _ansiballz_main()\n File "/home/master/.ansible/tmp/ansible-local-12047lmk8iodo/ansible-tmp-1726041988.1483855-12103-236711783000956/AnsiballZ_zabbix_host.py", line 99, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File "/home/master/.ansible/tmp/ansible-local-12047lmk8iodo/ansible-tmp-1726041988.1483855-12103-236711783000956/AnsiballZ_zabbix_host.py", line 47, in invoke_module\n runpy.run_module(mod_name='ansible_collections.community.zabbix.plugins.modules.zabbix_host', init_globals=dict(_module_fqn='ansible_collections.community.zabbix.plugins.modules.zabbix_host', _modlib_path=modlib_path),\n File "/usr/lib/python3.10/runpy.py", line 224, in run_module\n return _run_module_code(code, init_globals, run_name, mod_spec)\n File "/usr/lib/python3.10/runpy.py", line 96, in _run_module_code\n _run_code(code, mod_globals, init_globals,\n File "/usr/lib/python3.10/runpy.py", line 86, in _run_code\n exec(code, run_globals)\n File "/tmp/ansible_zabbix_host_payload_xjzah4ys/ansible_zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/modules/zabbix_host.py", line 1356, in \n File "/tmp/ansible_zabbix_host_payload_xjzah4ys/ansible_zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/modules/zabbix_host.py", line 1143, in main\n File "/tmp/ansible_zabbix_host_payload_xjzah4ys/ansible_zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/module_utils/base.py", line 20, in init\n File "/tmp/ansible_zabbix_host_payload_xjzah4ys/ansible_zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/module_utils/api_request.py", line 53, in api_version\n File "/tmp/ansible_zabbix_host_payload_xjzah4ys/ansible_zabbix_host_payload.zip/ansible/module_utils/connection.py", line 200, in rpc\nansible.module_utils.connection.ConnectionError: Invalid JSON response: <!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n\n404 Not Found\n\n

Not Found

\n

The requested URL was not found on this server.

\n
\n
Apache/2.4.58 (Ubuntu) Server at 127.0.0.1 Port 80
\n\n\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

Yaml file

        ---
        - name: Manage Zabbix Hosts
          hosts: localhost
          gather_facts: no
          vars:
            server_url: http://127.0.0.1/
            login_user: Admin
            login_password: zabbix
          tasks:
            - name: Set connection specific variables
              set_fact:
                ansible_network_os: community.zabbix.zabbix
                ansible_connection: httpapi
                ansible_httpapi_port: 80
                ansible_httpapi_use_ssl: false
                ansible_httpapi_validate_certs: false
                ansible_httpapi_url: http://127.0.0.1/
            - name: Set credentials to access Zabbix Server API
              set_fact:
                http_login_user: Admin
                http_login_password: zabbix
            - name: Ensure Zabbix API is reachable
              uri:
                url: "{{ ansible_httpapi_url }}"
                method: GET
                return_content: yes
              register: zabbix_api_check
              failed_when: zabbix_api_check.status != 200
            - name: Create or update Zabbix host
              zabbix_host:
                host_name: ansible-MQB-AXD
                visible_name: ansible-1.137-MQB-UXD
                description: linux server which host ansible
                host_groups:
                  - Applications
                link_templates:
                  - AWS by HTTP
                status: enabled
                state: present
                inventory_mode: manual
                inventory_zabbix:
                  tag: ansible_add_host
                interfaces:
                  - type: 1
                    main: 1
                    useip: 1
                    ip: 192.168.1.137
                    port: "10050"
                state : present       
              delegate_to: 127.0.0.1
              vars:
                ansible_zabbix_url: "{{ zabbix_server_url }}"
                ansible_zabbix_api_user: "{{  login_user }}"
                ansible_zabbix_api_password: "{{ login_password }}"
                macros:
                  - macro: "{$SERVER}"
                    value: server
                    description: Example desc that work only with Zabbix 4.4 and higher
                tags:
                  - tag: linux
                  - tag: ansible
                    value: server
pyrodie18 commented 1 week ago

Are you running this playbook (ansible controller) from the Zabbix Server host? I'm pretty confident your problem is the 127.0.0.1 addresses you have in it.

amikha33 commented 1 week ago

Are you running this playbook (ansible controller) from the Zabbix Server host? I'm pretty confident your problem is the 127.0.0.1 addresses you have in it.

Why could be the localhost an issue ? I already tried another playbook to create a group proxy that worked fine.

BGmot commented 1 week ago

404 means wrong url. But more importantly what a mess in variables - you use both httpapi related and old vars. What version of this collection are you using?

pyrodie18 commented 1 day ago

This doesn't appear to be a problem with the collection and no reply so closing.