lucasheld / ansible-uptime-kuma

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

monitor_info - pushToken and url equals null #5

Closed yurinek closed 1 year ago

yurinek commented 1 year ago

Hello,

we need to parse pushToken and url to use it in our scripts to push messages to a push-monitor.

  lucasheld.uptime_kuma.monitor:
    api_url: "http://{{ kuma_host }}"
    api_token: "{{ api_token }}"
    name: BackupScript
    type: push
    interval: 60
    maxretries: 0
    state: present

- name: get push monitor
  lucasheld.uptime_kuma.monitor_info:
    api_url: "http://{{ kuma_host }}"
    api_token: "{{ api_token }}"
    name: BackupScript
  register: push_monitor_result

- name: debug push_monitor_pushToken
  debug: msg="{{ push_monitor_result.monitors }}"

however the above outputs Null for these return values


ok: [localhost] => {
    "msg": [
        {
            "accepted_statuscodes": [
                "200-299"
            ],
            "active": true,
            "authDomain": null,
            "authMethod": "",
            "authWorkstation": null,
            "basic_auth_pass": null,
            "basic_auth_user": null,
            "body": null,
            "databaseConnectionString": null,
            "databaseQuery": null,
            "dns_last_result": null,
            "dns_resolve_server": "1.1.1.1",
            "dns_resolve_type": "A",
            "docker_container": null,
            "docker_host": null,
            "expiryNotification": false,
            "headers": null,
            "hostname": null,
            "id": 2,
            "ignoreTls": false,
            "interval": 60,
            "keyword": null,
            "maxredirects": 10,
            "maxretries": 0,
            "method": "GET",
            "mqttPassword": null,
            "mqttSuccessMessage": null,
            "mqttTopic": null,
            "mqttUsername": null,
            "name": "BackupScript",
            "notificationIDList": [],
            "port": 53,
            "proxyId": null,
            "pushToken": null,
            "radiusCalledStationId": null,
            "radiusCallingStationId": null,
            "radiusPassword": null,
            "radiusSecret": null,
            "radiusUsername": null,
            "resendInterval": 0,
            "retryInterval": 60,
            "tags": [],
            "type": "push",
            "upsideDown": false,
            "url": null,
            "weight": 2000
        }
    ]
}```
lucasheld commented 1 year ago

Thanks for your report. This is an uptime-kuma-api issue.

The bug is fixed in uptime-kuma-api 0.2.1 (https://github.com/lucasheld/uptime-kuma-api/commit/12cd8067e469d83912fd8417ffb768c3d7572887) Now the pushToken is generated when a monitor is saved.

It is expected that the url is unset. This url is only used for http and keyword monitor types. The push url must be generated from the pushToken as follows: "http://{{ kuma_host }}/api/push/{{ pushToken }}?status=up&msg=OK&ping="

yurinek commented 1 year ago

now the new issue is that none of Return Values work except "monitors" key. e.g.

- name: get push monitor
  lucasheld.uptime_kuma.monitor_info:
    api_url: "http://{{ kuma_host }}"
    api_token: "{{ api_token }}"
    name: "{{ push_monitor_name }}" 
  register: push_monitor_result

- name: debug 2
  debug: msg="{{ push_monitor_result.pushToken }}"

with

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'pushToken

same error for other return keys except push_monitor_result.monitors

lucasheld commented 1 year ago

The monitor_info module returns a list of monitors. If you specifiy a monitor name or id, the list contains only one entry. Otherwise the list contains all available monitors. The list is stored in the value of the monitors key.

In your case, the requested monitor is the first entry in the list (index 0). You can use this entry to access the pushToken.

- name: add monitor
  lucasheld.uptime_kuma.monitor:
    api_url: "http://{{ kuma_host }}"
    api_token: "{{ api_token }}"
    name: BackupScript
    type: push
    interval: 60
    maxretries: 0
    state: present

- name: get push monitor
  lucasheld.uptime_kuma.monitor_info:
    api_url: "http://{{ kuma_host }}"
    api_token: "{{ api_token }}"
    name: BackupScript
  register: push_monitor_result

- set_fact:
    monitor_push_token: "{{ push_monitor_result.monitors[0].pushToken }}"

- set_fact:
    monitor_push_url: "http://{{ kuma_host }}/api/push/{{ monitor_push_token }}?status=up&msg=OK&ping="

- debug:
    var: monitor_push_token

- debug:
    var: monitor_push_url