lucasheld / ansible-uptime-kuma

Ansible collection of modules to configure Uptime Kuma
GNU General Public License v3.0
135 stars 19 forks source link

Double Quotes in parameter headers #1

Closed JimTim closed 1 year ago

JimTim commented 1 year ago

Hey, we are trying to manage our uptime kuma with this Ansible playbook and fail with the parameter headers. We need to pass custom headers on a monitoring request and are trying to do this using headers and a JSON. But with the JSON, double quotes are converted to single quotes either by this module or Ansible+Jinja. So do you have a working example of setting up monitoring with custom headers?

headers: '{"customer-header-1": "value-of-header-1", "customer-header-2": "value-of-header-2"}'

... becomes {'customer-header-1':'value-of-header-1','customer-header-2':'value-of-header-2'} in uptime kuma and then ends in a 401 error at uptime kuma, because it is not a valid json for uptime kuma.

Thx for your help :-)

lucasheld commented 1 year ago

I am not sure how you are trying to pass the headers to the request.

I added the functionality to send custom headers to the connection request in another branch (https://github.com/lucasheld/uptime-kuma-api/commit/a886f57e9a3792d3ce7e8316c7db59b95574bac4 / https://github.com/lucasheld/ansible-uptime-kuma/commit/2e749279b317f6747709b6ed49c072fc91700f45). It uses the same type (dict) as the module ansible.builtin.uri for custom headers.

Please report if these changes work for you.

First install the modified ansible collection:

ansible-galaxy collection install git+https://github.com/lucasheld/ansible-uptime-kuma.git@custom-headers

Then use the following tasks to install the modified Python module and send a request with custom headers.

- name: install uptime-kuma-api from the branch custom-headers
  ansible.builtin.pip:
    name: git+https://github.com/lucasheld/uptime-kuma-api.git@custom-headers

- name: send request with custom headers
  lucasheld.uptime_kuma.monitor_info:
    api_url: http://127.0.0.1:3001
    api_username: admin
    api_password: secret123
    api_headers:
      key1: value1
      key2: value2
eddyfussel commented 1 year ago

Hi Lucas, thanks for your fast response and work! Have you a buy me a coffee link?

Unfortunately I think there is a misunderstanding here: We don't want to add custom headers to the api request to the uptime kuma socket api, we would like to use custom headers in the uptime kuma monitor, like in the yaml below:

- name: check with headers
  lucasheld.uptime_kuma.monitor:
    api_url: "http://localhost:3001"
    api_token: "{{ api_token}}"
    name: "test with custom http headers"
    type: http
    url: "test.site/health"
    basic_auth_user: monitoring
    basic_auth_pass: secret
    accepted_statuscodes:
      - "200"
    state: present
    notification_names:
      - "test"
    headers: '{"customer-header-1": "value-of-header-1","customer-header-2": "value-of-header-2"}'
    #headers: >
    #  {% raw %}{
    #      \"customer-header-1": \\"{% endraw %}{{ test__value-of-header-1|string }}{% raw %}\\",
    #      \\"customer-header-2\\": \\"{% endraw %}{{ test__value-of-header-2|string }}{% raw %}\\"
    #  }{% endraw %}
    # 

but if the var headers contains a valid json with values in double quotes, it gets converted to a json with single quotes, like:

{'customer-header-1':'value-of-header-1','customer-header-2':'value-of-header-2'} - this is also available in the frontend after executing the ansible-playbook

and the uptime kuma can't accept this values if there is no "valid" json with double quotes. You can test this in the frontend as well, just try to add the string above into the webform:

image

But if you replace the single quotes with doubles (in the frontend) the request works fine:

image
lucasheld commented 1 year ago

Oh yeah, I misunderstood that, sorry.

Unfortunately I can not reproduce this problem. When I run the same task, the headers are also formatted correctly in the Uptime Kuma frontend. Also, I don't parse the json anywhere, the type is a string and is passed through to uptime kuma that way.

The Playbook:

---
- hosts: localhost
  connection: local
  tasks:
    - name: Add notification
      lucasheld.uptime_kuma.notification:
        api_url: http://127.0.0.1:3001
        api_username: admin
        api_password: secret123
        name: test
        type: telegram
        telegramBotToken: 1111
        telegramChatID: 2222
        state: present

    - name: send request with custom headers
      lucasheld.uptime_kuma.monitor:
        api_url: http://127.0.0.1:3001
        api_username: admin
        api_password: secret123
        name: "test with custom http headers"
        type: http
        url: "test.site/health"
        basic_auth_user: monitoring
        basic_auth_pass: secret
        accepted_statuscodes:
          - "200"
        state: present
        notification_names:
          - "test"
        headers: '{"customer-header-1": "value-of-header-1","customer-header-2": "value-of-header-2"}'

image

Tested with ansible-uptime-kuma 0.1.1, uptime-kuma-api 0.1.1 and Uptime Kuma 1.17.1 (docker run -it --rm -p 3001:3001 louislam/uptime-kuma:1.17.1).