0

I try to communicate with zabbix API with ansible. It waits for a json input and process it, however, it does not work. My playbook task looks like this:

- name: Login to zabbix
  uri:
    url: https://{{ zabbix_server_ip }}/api_jsonrpc.php
    method: POST
    validate_certs: false
    body:
      jsonrpc: 2.0
      method: user.login
      params:
        user: admin
        password: XXX
      id: 1
      auth: null
    body_format: json
  register: zabbix_login

If I run the playbook with -vvvv then I see what was posted to the API by ansible. The interesting part looks like this:

        "body": {
            "auth": null,
            "id": 1,
            "jsonrpc": 2.0,
            "method": "user.login",
            "params": {
                "password": "XXX",
                "user": "admin"
            }

Which is correct, the same should be posted, but zabbix_login remains empty from the API answer, because of this error message (also parsed from -vvvv):

"json": {
    "error": {
        "code": -32600,
        "data": "Invalid parameter \"/jsonrpc\": a character string is expected.",
        "message": "Invalid Request."
    },
    "id": 1,
    "jsonrpc": "2.0"
},

I belive the problem should be that the API wants jsonrpc: 2.0 parameter at the beginning of the request, as it's documentation described it. But it seems ansible reordering the json request somehow to alphabetical order. How do I prevent this?

I already read this question: How to prevent Ansible from re-ordering JSON? and also the answer, but it just reorders the answer for the request. I need to keep the same order of the request, as wrote in the playbook.

Darwick
  • 357
  • 3
  • 14
  • 2
    It's not that it wants a json key "at the beginning," it's that it wants `"jsonrpc": "2.0"` as one can see in its response payload to you. Merely quote the `: 2.0` (as `: '2.0'`) to keep it from being sent as a number – mdaniel Apr 23 '21 at 16:04
  • @mdaniel you're right. The strange thing is that it worked before, I thought that the zabbix update made it more strict. Based on your comment, I checked that the PHP upgrade spoiled it. Anyway, thank you for the point. :) – Darwick Apr 23 '21 at 19:11

1 Answers1

0

Based on comment, I fixed the playbook and quoted every input.

- name: Login to zabbix
  uri:
    url: https://{{ zabbix_server_ip }}/api_jsonrpc.php
    method: POST
    validate_certs: false
    body:
      jsonrpc: '2.0'
      method: 'user.login'
      params:
        user: 'admin'
        password: 'XXX'
      id: '1'
      auth: null
    body_format: json
  register: zabbix_login

It seems it is fixed the issue.

Darwick
  • 357
  • 3
  • 14