Ansible to Slack

These snippets will get you setup with sending slack messages in a jiffy from your ansible playbooks.

3 months ago

Latest Post Ansible to Slack by Matthew Davis

These snippets will get you setup with sending slack messages in a jiffy from your ansible playbooks.

The Task

First we'll setup a simple wrapper task. Create a file called tasks/slack.yaml. We'll use import_tasks later to call this task.

- name: Send slack notification
  slack:
    token: "your/token/here..."
    msg: "{{ message }}"
    channel: "{{ channel }}"
    username: "ansible"
    color: "{{ color | default('good') }}"
    link_names: 1
tasks/slack.yaml

The Playbook

Simply add the following block to your existing playbook(s). Notice how we pass vars to our task wrapper (above).

- name: Post-rollout
  tasks:
    - block:
      - import_tasks: tasks/notifications/slack.yaml
        vars:
          channel: "#mychannel"
          message: "Rollout finished! Please resume active monitoring!"
playbook.yaml

See also

https://docs.ansible.com/ansible/latest/modules/slack_module.html

Matthew Davis

Published 3 months ago

Comments?

Leave us your opinion.