community.general.slack (8.5.0) — module

Send Slack notifications

Authors: Ramon de la Fuente (@ramondelafuente)

Install collection

Install with ansible-galaxy collection install community.general:==8.5.0


Add to requirements.yml

  collections:
    - name: community.general
      version: 8.5.0

Description

The M(community.general.slack) module sends notifications to U(http://slack.com) via the Incoming WebHook integration

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Send notification message via Slack
  community.general.slack:
    token: thetoken/generatedby/slack
    msg: '{{ inventory_hostname }} completed'
  delegate_to: localhost
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Send notification message via Slack all options
  community.general.slack:
    token: thetoken/generatedby/slack
    msg: '{{ inventory_hostname }} completed'
    channel: '#ansible'
    thread_id: '1539917263.000100'
    username: 'Ansible on {{ inventory_hostname }}'
    icon_url: http://www.example.com/some-image-file.png
    link_names: 0
    parse: 'none'
  delegate_to: localhost
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Insert a color bar in front of the message for visibility purposes and use the default webhook icon and name configured in Slack
  community.general.slack:
    token: thetoken/generatedby/slack
    msg: '{{ inventory_hostname }} is alive!'
    color: good
    username: ''
    icon_url: ''
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Insert a color bar in front of the message with valid hex color value
  community.general.slack:
    token: thetoken/generatedby/slack
    msg: 'This message uses color in hex value'
    color: '#00aacc'
    username: ''
    icon_url: ''
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Use the attachments API
  community.general.slack:
    token: thetoken/generatedby/slack
    attachments:
      - text: Display my system load on host A and B
        color: '#ff00dd'
        title: System load
        fields:
          - title: System A
            value: "load average: 0,74, 0,66, 0,63"
            short: true
          - title: System B
            value: 'load average: 5,16, 4,64, 2,43'
            short: true
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Use the blocks API
  community.general.slack:
    token: thetoken/generatedby/slack
    blocks:
      - type: section
        text:
          type: mrkdwn
          text: |-
            *System load*
            Display my system load on host A and B
      - type: context
        elements:
        - type: mrkdwn
          text: |-
            *System A*
            load average: 0,74, 0,66, 0,63
        - type: mrkdwn
          text: |-
            *System B*
            load average: 5,16, 4,64, 2,43
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Send a message with a link using Slack markup
  community.general.slack:
    token: thetoken/generatedby/slack
    msg: We sent this message using <https://www.ansible.com|Ansible>!
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Send a message with angle brackets and ampersands
  community.general.slack:
    token: thetoken/generatedby/slack
    msg: This message has &lt;brackets&gt; &amp; ampersands in plain text.
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Initial Threaded Slack message
  community.general.slack:
    channel: '#ansible'
    token: xoxb-1234-56789abcdefghijklmnop
    msg: 'Starting a thread with my initial post.'
  register: slack_response
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add more info to thread
  community.general.slack:
    channel: '#ansible'
    token: xoxb-1234-56789abcdefghijklmnop
    thread_id: "{{ slack_response['ts'] }}"
    color: good
    msg: 'And this is my threaded response!'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Send a message to be edited later on
  community.general.slack:
    token: thetoken/generatedby/slack
    channel: '#ansible'
    msg: Deploying something...
  register: slack_response
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Edit message
  community.general.slack:
    token: thetoken/generatedby/slack
    # The 'channel' option does not accept the channel name. It must use the 'channel_id',
    # which can be retrieved for example from 'slack_response' from the previous task.
    channel: "{{ slack_response.channel }}"
    msg: Deployment complete!
    message_id: "{{ slack_response.ts }}"

Inputs

    
msg:
    description:
    - Message to send. Note that the module does not handle escaping characters. Plain-text
      angle brackets and ampersands should be converted to HTML entities (e.g. & to &amp;)
      before sending. See Slack's documentation (U(https://api.slack.com/docs/message-formatting))
      for more.
    type: str

color:
    default: normal
    description:
    - Allow text to use default colors - use the default of 'normal' to not send a custom
      color bar at the start of the message.
    - Allowed values for color can be one of 'normal', 'good', 'warning', 'danger', any
      valid 3 digit or 6 digit hex color value.
    type: str

parse:
    choices:
    - full
    - none
    description:
    - Setting for the message parser at Slack
    type: str

token:
    description:
    - Slack integration token. This authenticates you to the slack service. Make sure
      to use the correct type of token, depending on what method you use.
    - 'Webhook token: Prior to Ansible 1.8, a token looked like V(3Ffe373sfhRE6y42Fg3rvf4GlK).  In
      Ansible 1.8 and above, Ansible adapts to the new slack API where tokens look like
      V(G922VJP24/D921DW937/3Ffe373sfhRE6y42Fg3rvf4GlK).  If tokens are in the new format
      then slack will ignore any value of domain.  If the token is in the old format the
      domain is required.  Ansible has no control of when slack will get rid of the old
      API.  When slack does that the old format will stop working.  ** Please keep in
      mind the tokens are not the API tokens but are the webhook tokens.  In slack these
      are found in the webhook URL which are obtained under the apps and integrations.
      The incoming webhooks can be added in that area.  In some cases this may be locked
      by your Slack admin and you must request access.  It is there that the incoming
      webhooks can be added.  The key is on the end of the URL given to you in that section.'
    - 'WebAPI token: Slack WebAPI requires a personal, bot or work application token.
      These tokens start with V(xoxp-), V(xoxb-) or V(xoxa-), for example V(xoxb-1234-56789abcdefghijklmnop).
      WebAPI token is required if you intend to receive thread_id. See Slack''s documentation
      (U(https://api.slack.com/docs/token-types)) for more information.'
    required: true
    type: str

blocks:
    description:
    - Define a list of blocks. This list mirrors the Slack JSON API.
    - For more information, see U(https://api.slack.com/block-kit).
    elements: dict
    type: list
    version_added: 1.0.0
    version_added_collection: community.general

domain:
    description:
    - Slack (sub)domain for your environment without protocol. (For example V(example.slack.com).)
      In Ansible 1.8 and beyond, this is deprecated and may be ignored.  See token documentation
      for information.
    type: str

channel:
    description:
    - Channel to send the message to. If absent, the message goes to the channel selected
      for the O(token).
    type: str

icon_url:
    default: https://docs.ansible.com/favicon.ico
    description:
    - URL for the message sender's icon.
    type: str

username:
    default: Ansible
    description:
    - This is the sender of the message.
    type: str

thread_id:
    description:
    - Optional. Timestamp of parent message to thread this message. https://api.slack.com/docs/message-threading
    type: str

icon_emoji:
    description:
    - Emoji for the message sender. See Slack documentation for options.
    - If O(icon_emoji) is set, O(icon_url) will not be used.
    type: str

link_names:
    choices:
    - 1
    - 0
    default: 1
    description:
    - Automatically create links for channels and usernames in O(msg).
    type: int

message_id:
    description:
    - Optional. Message ID to edit, instead of posting a new message.
    - If supplied O(channel) must be in form of C(C0xxxxxxx). use C({{ slack_response.channel_id
      }}) to get RV(ignore:channel_id) from previous task run.
    - Corresponds to C(ts) in the Slack API (U(https://api.slack.com/messaging/modifying)).
    type: str
    version_added: 1.2.0
    version_added_collection: community.general

attachments:
    description:
    - Define a list of attachments. This list mirrors the Slack JSON API.
    - For more information, see U(https://api.slack.com/docs/attachments).
    elements: dict
    type: list

prepend_hash:
    choices:
    - always
    - never
    - auto
    default: auto
    description:
    - Setting for automatically prepending a V(#) symbol on the passed in O(channel).
    - The V(auto) method prepends a V(#) unless O(channel) starts with one of V(#), V(@),
      V(C0), V(GF), V(G0), V(CP). These prefixes only cover a small set of the prefixes
      that should not have a V(#) prepended. Since an exact condition which O(channel)
      values must not have the V(#) prefix is not known, the value V(auto) for this option
      will be deprecated in the future. It is best to explicitly set O(prepend_hash=always)
      or O(prepend_hash=never) to obtain the needed behavior.
    type: str
    version_added: 6.1.0
    version_added_collection: community.general

validate_certs:
    default: true
    description:
    - If V(false), SSL certificates will not be validated. This should only be used on
      personally controlled sites using self-signed certificates.
    type: bool