Ansible with Gotify Notifications

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 :frowning:

- 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

Try quoting and URL encoding the URL and perhaps omit the quotes from the URL, eg:

url: "https://gotify.mydomain.com/message?token=XXXXXXX%20-F%20message=Disk%20space%20on%20{{ inventory_hostname }}%20is%20above%2090%!"

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:

URL can’t contain control characters.

1 Like

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":...........

ofc I exchanged it with the right token :wink:

Try submitting the data as JSON rather than using a query string? :man_shrugging:

1 Like

I would do so if I will not be an absolute newbie what that kind of stuff should look like in this example.

Unlikly there is no example on the gotify documentation on how I can use it with the json part :confused:

May you help me out once more?

BTW: also tried it with uri encode part from the other post.

Take a look at the URL module documentation, there are some examples there that might help.

I alredy know this documentation, what I don’t know is on how to paste my stuff in such an example.
There is only one example on the page:

- name: Create a JIRA issue
  ansible.builtin.uri:
    url: https://your.jira.example.com/rest/api/2/issue/
    user: your_username
    password: your_pass
    method: POST
    body: "{{ lookup('ansible.builtin.file','issue.json') }}"
    force_basic_auth: true
    status_code: 201
    body_format: json

So if I try to translate my stuff into this, I will end up with this:

- name: Send Gotify Message
  ansible.builtin.uri:
    url: https://gotify.mydomain.com/
    token: XXXXXXX
    method: POST
    message: message=Disk space on {{ inventory_hostname }} is above 90%!
    body_format: json

There is no message or token option with the URI module, you need to use body for your POST content, try something like this?

- name: Send Gotify Message
  ansible.builtin.uri:
    url: https://gotify.mydomain.com/
    method: POST
    body_format: json
    body:
      token: XXXXXXX
      message: "Disk space on {{ inventory_hostname }} is above 90%!"
1 Like

thank you for clearing this up, how to understand that json format.
unfortunately it isn’t working this way :confused:

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%!"`

Try something like this? :man_shrugging:

- name: Send Gotify Message
  ansible.builtin.uri:
    url: "https://gotify.mydomain.com/message?token=XXXXXXXX"
    method: POST
    body_format: form-urlencoded
    body: "message=Disk space on {{ inventory_hostname }} is above 90%!"

error:

TASK [Send Gotify Message] *****************************************************
9:39:46 PM
fatal: [HOSTNAME]: FAILED! => {"changed": false, "connection": "close", "content_length": "86", "content_type": "application/json", "date": "Thu, 04 Jan 2024 20:39:46 GMT", "elapsed": 0, "json": {"error": "Bad Request", "errorCode": 400, "errorDescription": "invalid URL escape \"%!\""}, "msg": "Status code was 400 and not [200]: HTTP Error 400: Bad Request", "redirected": false, "server": "openresty", "status": 400, "strict_transport_security": "max-age=63072000;includeSubDomains; preload", "url": "https://gotify.mydomain.com/message?token=XXXXXXXXX"}

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 :slight_smile: Thank you so far but I don’t want you to spend even more of your free time on my project!

1 Like

this works for me.

- name: Send Gotify Message
      ansible.builtin.uri:
        url: "https://gotify.doma.in/message?token=<app_token>"
        method: POST
        body_format: json
        body:
          title: Hello World
          message: "Hello from Ansible via Gotify"
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.