community.zabbix icon indicating copy to clipboard operation
community.zabbix copied to clipboard

variable ansible_zabbix_url_path does not work

Open drMARTINS13 opened this issue 2 years ago • 24 comments

SUMMARY

We have zabbix sitting on simple subdomain zabbix.example.com and when we want add something to zabbix via ansible we get this error The error was: ansible.module_utils.connection.ConnectionError: Invalid JSON response: File not found So I know that ansible call zabbix.example.com/zabbix/api_jsonrpc.php. I add to inventory variable ansible_zabbix_url_path (i tried "/" and "") and still same problem. When i try curl zabbix.example.com/api_jsonrpc.php i get valid response.

We think problem is here https://github.com/ansible-collections/community.zabbix/blob/56f94d151da34b71a1a09af0b4eca6c7ddee319b/plugins/httpapi/zabbix.py#L132

ISSUE TYPE
  • Bug Report
COMPONENT NAME

TASK [community.zabbix.zabbix_agent : API | Create host groups] but it is also in another tasks that call api_jsonrpc.php

ANSIBLE VERSION

ansible [core 2.14.4] community.zabbix 1.9.3 (tried also 1.9.2)

OS / ENVIRONMENT / Zabbix Version

Zabbix server 6.4

STEPS TO REPRODUCE

Install zabbix on subdomain for example zabbix.example.com Run ansible with invetory file ... server001: install_zabbix_agent: True zabbix_api_create_hosts: True zabbix_api_create_hostgroup: True zabbix_agent2: True zabbix_agent_package_remove: True zabbix_agent2_server: zabbix-proxy.example.com zabbix_agent2_serveractive: zabbix-proxy.example.com zabbix_agent_proxy: zabbix-proxy.example.com zabbix_api_server_host: zabbix.example.com ansible_zabbix_url_path: "" zabbix_host_groups: - MAINGROUP - MAINGROUP/SUBGROUP zabbix_agent_link_template: - Template OS Linux by Zabbix agent - Template Check security updates via APT - Template Module ICMP Ping

role/task/main.yml

  • name: Installing Zabbix Agent include_role: name: community.zabbix.zabbix_agent .....
EXPECTED RESULTS

Server001 is registered to zabbix server, is in groups and has templates

ACTUAL RESULTS

TASK [community.zabbix.zabbix_agent : API | Create host groups] ********************************************************************************************** FAILED - RETRYING: [server001 -> zabbix.example.com]: API | Create host groups (3 retries left). FAILED - RETRYING: [server001 -> zabbix.example.com]: API | Create host groups (2 retries left). FAILED - RETRYING: [server001-> zabbix.example.com]: API | Create host groups (1 retries left). An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.module_utils.connection.ConnectionError: Invalid JSON response: File not found.

drMARTINS13 avatar Apr 24 '23 10:04 drMARTINS13

What version of ansible.netcommon do you have installed? (ansible-galaxy collection list should show it.)

flowerysong avatar Apr 24 '23 19:04 flowerysong

sorry for late answer ansible.netcommon 4.1.0

drMARTINS13 avatar Apr 27 '23 06:04 drMARTINS13

Please update to collection version 2.0.0 and I don't see the error you are getting, please provide output when you run it with -vvv

BGmot avatar May 14 '23 13:05 BGmot

I am seeing the exact same issue. We also run zabbix on a subdomain. I am using Zabbix collection 2.0.0 and netcommon 5.1.1.

Running with -vvv I get the following output:

TASK [community.zabbix.zabbix_agent : API | Create a new host using agent2 or update an existing host's info] ********************************************************************************************
task path: /home/user/.ansible/collections/ansible_collections/community/zabbix/roles/zabbix_agent/tasks/api.yml:50
The full traceback is:
Traceback (most recent call last):
  File "<stdin>", line 107, in <module>
  File "<stdin>", line 99, in _ansiballz_main
  File "<stdin>", line 47, in invoke_module
  File "/usr/lib64/python3.9/runpy.py", line 225, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib64/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib64/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_community.zabbix.zabbix_host_payload_9erm4e1t/ansible_community.zabbix.zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/modules/zabbix_host.py", line 1237, in <module>
  File "/tmp/ansible_community.zabbix.zabbix_host_payload_9erm4e1t/ansible_community.zabbix.zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/modules/zabbix_host.py", line 1042, in main
  File "/tmp/ansible_community.zabbix.zabbix_host_payload_9erm4e1t/ansible_community.zabbix.zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/module_utils/base.py", line 20, in __init__
  File "/tmp/ansible_community.zabbix.zabbix_host_payload_9erm4e1t/ansible_community.zabbix.zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/module_utils/api_request.py", line 53, in api_version
  File "/tmp/ansible_community.zabbix.zabbix_host_payload_9erm4e1t/ansible_community.zabbix.zabbix_host_payload.zip/ansible/module_utils/connection.py", line 200, in __rpc__
ansible.module_utils.connection.ConnectionError: Invalid JSON response: File not found.

fatal: [host -> zabbix.fqdn.com]: FAILED! => {
    "attempts": 3,
    "changed": false,
    "module_stderr": "Traceback (most recent call last):\n  File \"<stdin>\", line 107, in <module>\n  File \"<stdin>\", line 99, in _ansiballz_main\n  File \"<stdin>\", line 47, in invoke_module\n  File \"/usr/lib64/python3.9/runpy.py\", line 225, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib64/python3.9/runpy.py\", line 97, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib64/python3.9/runpy.py\", line 87, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_community.zabbix.zabbix_host_payload_9erm4e1t/ansible_community.zabbix.zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/modules/zabbix_host.py\", line 1237, in <module>\n  File \"/tmp/ansible_community.zabbix.zabbix_host_payload_9erm4e1t/ansible_community.zabbix.zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/modules/zabbix_host.py\", line 1042, in main\n  File \"/tmp/ansible_community.zabbix.zabbix_host_payload_9erm4e1t/ansible_community.zabbix.zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/module_utils/base.py\", line 20, in __init__\n  File \"/tmp/ansible_community.zabbix.zabbix_host_payload_9erm4e1t/ansible_community.zabbix.zabbix_host_payload.zip/ansible_collections/community/zabbix/plugins/module_utils/api_request.py\", line 53, in api_version\n  File \"/tmp/ansible_community.zabbix.zabbix_host_payload_9erm4e1t/ansible_community.zabbix.zabbix_host_payload.zip/ansible/module_utils/connection.py\", line 200, in __rpc__\nansible.module_utils.connection.ConnectionError: Invalid JSON response: File not found.\n\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

andrew-landsverk-win avatar May 24 '23 15:05 andrew-landsverk-win

Please provide all Zabbix API variables you use to call the role.

BGmot avatar May 24 '23 15:05 BGmot

zabbix_api_server_url: "https://zabbix.fqdn.com"
zabbix_api_server_host: zabbix.fqdn.com
zabbix_api_login_user: Admin
zabbix_api_login_pass: thepassword
zabbix_api_use: true
zabbix_api_create_hosts: true
ansible_zabbix_url_path: ''
ansible_network_os: community.zabbix.zabbix
ansible_connection: httpapi
ansible_httpapi_port: 443
ansible_httpapi_use_ssl: true
ansible_httpapi_validate_certs: true

andrew-landsverk-win avatar May 24 '23 15:05 andrew-landsverk-win

zabbix_api_server_url does not exist in 2.0.0 You are saying that running Zabbix on a subdomain but setting ansible_zabbix_url_path to '', you need to provide the path you have after Zabbix server FQDN.

BGmot avatar May 24 '23 15:05 BGmot

There is no path, Zabbix is available at https://zabbix.fqdn.com and the API PHP file is available at https://zabbix.fqdn.com/api_jsonrpc.php

andrew-landsverk-win avatar May 24 '23 15:05 andrew-landsverk-win

then please try

ansible_zabbix_url_path: '/'

BGmot avatar May 24 '23 16:05 BGmot

I just tried again with '/' and I get the same error that I posted above unfortunately.

andrew-landsverk-win avatar May 24 '23 16:05 andrew-landsverk-win

Then I don't know how to help. Ansible literally cannot find https://zabbix.fqdn.com/api_jsonrpc.php. If you use 2.0.0 version of this collection this should not be a problem with variables set as above. Try turning on logging of all HTTP requests.

BGmot avatar May 24 '23 16:05 BGmot

Thank you for your help today. I added some debugging to the python module zabbix.py and found that regardless of my setting for url_path it wasn't changing, found out that I wasn't setting it in the right place somehow, so I corrected that and now I'm onto different errors with our setup. Thanks again!

andrew-landsverk-win avatar May 24 '23 16:05 andrew-landsverk-win

In my opinion, the problem is on this line

https://github.com/ansible-collections/community.zabbix/blob/1b72e139b11cec13173e6220c5e1b20a9f816733/plugins/httpapi/zabbix.py#L132

variable url_path have by default '/zabbix' value

vsegfault avatar May 25 '23 14:05 vsegfault

sorry for so late answer so i tried to update to 2.0.0 but still same problem. I add one debug task which just print two variables so you can see i have both of them correct

- name: Print server url
  ansible.builtin.debug:
    msg: zabbix_api_server_host variable is  {{ zabbix_api_server_host }} and ansible_zabbix_url_path variable is {{ ansible_zabbix_url_path }}

even if I run ansible-playbook without -vvv I can clearly see that it still won't use the correct url result is: (i changed our domain in this extract and host)

TASK [community.zabbix.zabbix_agent : Print server url] *******************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [somehost] => {
    "msg": "zabbix_api_server_host variable is  zabbix.example.net and ansible_zabbix_url_path variable is /"
}

TASK [community.zabbix.zabbix_agent : API | Create host groups] ***********************************************************************************************************************************************************************************************************************************************************************************************************************
FAILED - RETRYING: [somehost -> zabbix.example.net]: API | Create host groups (3 retries left).
FAILED - RETRYING: [somehost -> zabbix.example.net]: API | Create host groups (2 retries left).
FAILED - RETRYING: [somehost -> zabbix.example.net]: API | Create host groups (1 retries left).
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.module_utils.connection.ConnectionError: Invalid JSON response: File not found.

as mentioned before it looks like path /zabbix is hardcoded in source code on the row @vsegfault wrote

drMARTINS13 avatar May 26 '23 12:05 drMARTINS13

Hi I'm getting related issue, the role is calling index.php instead of /api_jsonrpc.php This is what i'm getting in apache access log:

10.0.105.10 - - [19/Jun/2023:10:09:41 +0200] "GET / HTTP/1.0" 200 6630 "-" "Python-urllib/3.9"
10.0.105.10 - - [19/Jun/2023:10:09:46 +0200] "GET / HTTP/1.0" 200 6630 "-" "Python-urllib/3.9"
10.0.105.10 - - [19/Jun/2023:10:09:47 +0200] "GET / HTTP/1.0" 200 6630 "-" "Python-urllib/3.9"

And this is my playbook roles: - role: zabbix-agent-v2 zabbix_agent_server: 10.0.105.31 zabbix_agent_serveractive: 10.0.105.31 zabbix_api_server_host: zabbix.xxx.xx.xxx.xx zabbix_api_login_user: user zabbix_api_login_pass: password zabbix_api_create_hostgroup: true zabbix_api_create_hosts: true zabbix_agent_host_state: present zabbix_host_groups: - Linux Servers zabbix_agent_link_templates: - Template OS Linux

I'm getting invalid JSON Response with the source code for login page. Username/password works fine with browser.

rem22 avatar Jun 19 '23 08:06 rem22

then please try

ansible_zabbix_url_path: '/'

Got the same problem and set to / and it worked. But you could really add more specific information about this in the documentation, not just # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu because it's not visible enough.

electropolis avatar Sep 01 '23 12:09 electropolis

Where would you like this to be documented?

BGmot avatar Sep 01 '23 13:09 BGmot

On each zabbix module page that require this.

electropolis avatar Sep 03 '23 13:09 electropolis

Hi @BGmot, I just stumbled accross the same issue, a simple "/" in ansible_zabbix_url_path resolved it. i had a look in the offical documentation of this Module and nowhere is mentioned that you should set it to "/" when you run it on the default path. so i would prefer it to have in the ansible documentation

aaimreimers avatar Oct 17 '23 13:10 aaimreimers

Hi @aaimreimers, the problem is there is not "default path". Zabbix installation on different Linux distros, in containers use different path. ansible_zabbix_url_path variable documented so everybody just need to set it to appropriate value.

BGmot avatar Oct 17 '23 13:10 BGmot

@pyrodie18

Thank you. Indeed. It's a parameter deviation in the module community.zabbix.zabbix_user (and in the module community.zabbix.zabbix_template for sure as well.). Not all modules are affected by that.

https://github.com/ansible-collections/community.zabbix/issues/1117

ghost avatar Nov 11 '23 11:11 ghost

then please try

ansible_zabbix_url_path: '/'

This yields a POST request like this: POST ///api_jsonrpc.php

The correct value is:

ansible_zabbix_url_path: ''

This results in: POST /api_jsonrpc.php

juresaht2 avatar Dec 04 '23 17:12 juresaht2

@BGmot @pyrodie18 Are we sure that ansible_zabbix_url_path still get's processed? Just tried with '' like with 2.x fork & '/' on 3.0.4 but not honored in the server logs: "POST /zabbix/api_jsonrpc.php HTTP/1.1" 404 27 "-" "Python-urllib/3.10"

Usage with community.zabbix.zabbix_agent as var on include_role doesn't work: #1072 ? But also not in role via vars on with zabbix_group or zabbix_host modules tasks: #1073 ? Can you please confirm it's still possible to override /zabbix in all our API queries?

krauthosting avatar Jul 22 '24 15:07 krauthosting

FYI a nasty hack to hot fix this breakage is a server side rewrite in Nginx:

--- a/nginx/conf.d/zabbix.conf
+++ b/nginx/conf.d/zabbix.conf
@@ -48,6 +48,10 @@ server {
         log_not_found   off;
     }
 
+    location ~ /zabbix/api_jsonrpc.php {
+        rewrite ^/zabbix/(.*)$ /$1 last;
+    }
+
     location / {
         try_files       $uri $uri/ =404;
     }

krauthosting avatar Jul 23 '24 14:07 krauthosting