Ansible allows you to extend the system by using plugins. Plugins are executed at vaious stages of a run and allow you to hook into the system to add your own logic. Plugins are written in python.

Callback plugins respond to events Ansible sends and can be used to notify external systems. There are many use cases for this, for example they can be used to notify Slack whenever an Ansible playbook failed.

By default there is a Slack callback plugin already implemented into Ansible from version 2.1.

Prerequisites

For the Slack plugin to work there are two requirements that need to be fulfilled a python library needs to be installed and a webhook needs to be created in the Slack workspace.

Python Prettytables

Prettytables is a Python library for generating simple ASCII tables. It can be installed with pip using pip install prettytables

Slack Webhooks

Creating webhooks for your slack workspace is pretty simple. Navigate to your admin settings -> Configure Apps and add the app "Incoming Webhooks". This will enable you to create a webhook for your workspace. If you get stuck give it a Google, its pretty simple. Note that you need to keep the webhook URL to use within the ansible settings.

Configuring

Once you have setup the prerequisites there a few more things that need to be done.

A few things within the ansible.cfg need to be configured:

[defaults]
callback_whitelist = slack

[callback_slack]
channel = #axians-internal
username = Ansible-Blog
webhook_url = https://hooks.slack.com/services/<data>

These settings perform a few functions. Channel will select what channel within the workgroup ansible will send the messages to. Username is what the username of the user the message will come from. The webhook_url parameter will be the url provided by slack to send the messages to.

Please note! If you are going to use this in production you can also store this data within environment variables, see documentation for this.

Running

When you next run a playbook you will see the messages come through into the selected channel.

Slack

slack-ansible-min-1