Hey guys, I’m trying to get Notifications done with my Gotify instance.
Gotify got a simple API to get notified. Unfortunately if I use the uri command inside my playbook I get errors
- name: send message when disk space is over 80%
uri:
url: https://gotify.mydomain.com/message?token=XXXXXXX -F '"message=Disk space on {{ inventory_hostname }} is above 90%!"'
method: POST
#body_format: json
#body: '{"content": "Disk space on {{ inventory_hostname }} is above 80%!"}'
#headers:
# Content-Type: application/json
#status_code: 200
when: disk_usage.stdout[:-1]|int > 80
As you can see I already commented out some parts that I was testing as well.
Right now it tells me this:
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: http.client.InvalidURL: URL can't contain control characters. '/message?token=XXXXXXX -F \'"message=Disk space on XXXXXXX is above 90%!"\'' (found at least ' ')
Hope someone can help. Official Documentation for the API can be found here: Push messages · Gotify
Or omit the query string altogether and submit the data as JSON, there seems to be little point in submitting data the using a query string unless you are forced to use GET rather than POST? It would also be a better in terms of security to not submit the token value in the URL.
See also this thread relating to the same error:
Which was automatically closed 17 hours ago, I personally think threads should be left open for longer than the current time limit, this discussion could have taken place on that thread or this thread could have been merged into that one…
Both threads relate to the same error message from the URI module:
I tried it with this command you provided, unfortunately it doesn’t recognize the correct token I provide now.
{"changed": false, "connection": "close", "content_length": "141", "content_type": "application/json", "date": "Thu, 04 Jan 2024 14:25:56 GMT", "elapsed": 0, "json": {"error": "Unauthorized", "errorCode": 401, "errorDescription": "you need to provide a valid access token or user credentials to access this api"}, "msg": "Status code was 401 and not [200]: HTTP Error 401: Unauthorized", "redirected": false, "server": "openresty", "status": 401, "strict_transport_security":...........
thank you for clearing this up, how to understand that json format.
unfortunately it isn’t working this way
The access token isn’t accepted or recognized. I guess the only way of getting something out to gotify will be to write a bash script and then run this.
Or in a shell command.
This here is working f.e.:
- name: Send Gotify Message via Shell`
ansible.builtin.shell: curl "https://gotify.mydomain.com/message?token=XXXXXXXX" -F "message=Disk space on {{ inventory_hostname }} is above 90%!"`
EDIT: It is fine for me for now to just work with the shell command, still I like your attempt much more, looks so much cleaner and more readable Thank you so far but I don’t want you to spend even more of your free time on my project!