ansible-collections / community.zabbix

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

variable ansible_zabbix_url_path does not work #970

Open drMARTINS13 opened 1 year ago

drMARTINS13 commented 1 year ago
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
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:

role/task/main.yml

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.

flowerysong commented 1 year ago

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

drMARTINS13 commented 1 year ago

sorry for late answer ansible.netcommon 4.1.0

BGmot commented 1 year ago

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

andrew-landsverk-win commented 1 year ago

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
}
BGmot commented 1 year ago

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

andrew-landsverk-win commented 1 year ago
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
BGmot commented 1 year ago

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.

andrew-landsverk-win commented 1 year ago

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

BGmot commented 1 year ago

then please try

ansible_zabbix_url_path: '/'
andrew-landsverk-win commented 1 year ago

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

BGmot commented 1 year ago

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.

andrew-landsverk-win commented 1 year ago

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!

vsegfault commented 1 year ago

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

drMARTINS13 commented 1 year ago

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

rem22 commented 1 year ago

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:

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

electropolis commented 12 months ago

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.

BGmot commented 12 months ago

Where would you like this to be documented?

electropolis commented 12 months ago

On each zabbix module page that require this.

aaimreimers commented 10 months ago

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

BGmot commented 10 months ago

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.

ghost commented 9 months ago

@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

juresaht2 commented 8 months ago

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

krauthosting commented 1 month ago

@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 commented 1 month ago

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;
     }