Search code examples
ansibleslackslack-api

Slack message from Ansible is being sent as plaint text json


I'm using Ansible to send a message to Slack using the ansible guidelines, but the message isn't formatting. For example, If I have

- name: "Slack test"
    slack:
      token: "abc123"
      channel: "some_channel"
      color: good
      msg: '{"text": "This is a line of text.\nAnd this is another one."}'

in my Ansible task, then it will post the unformatted json {"text": "This is a line of text.\nAnd this is another one."} to the Slack channel. How can I have the JSON messages be formatted like in Slack's message formatting guide?


Solution

  • I think you are not using the correct syntax for Ansible.

    According to the documentation you linked the msg property should contain the text of the message directly, not a JSON structure with additional properties.

    So this should be a corrected example:

    - name: "Slack test"
        slack:
          token: "abc123"
          channel: "some_channel"
          color: good
          msg: "This is a line of text.\nAnd this is another one."
    

    To add formatting to your text you should be able use to Slack markup in the msg property. Example for bold:

    - name: "Slack test"
            slack:
              token: "abc123"
              channel: "some_channel"
              color: good
              msg: "This is a *bold line* of text.\nAnd this is another one."