- Ansible has no control of when slack will get rid of the old API. When slack does that the old format will stop working. No: Ansible: This is the sender of.
- Ansible + hubot + slack integration. GitHub Gist: instantly share code, notes, and snippets.
- Ansible collections is the main repository for Ansible-maintained and community collections, including collections for network devices. IRC and Slack ¶ Join us on.
So you want a simple Slack failure handler for Ansible to ping your alerts channel whenever a deployment fails. Despite a thorough search I couldn’t find any examples that did this adequately, and even the solution here is a little constrained. The basic requirements are:
- A Slack notification on any task failure.
- The name of the task.
- The name of the host.
- The error debug message.
This is an ansible callback plugin that sends status updates to a Slack channel during playbook execution. Before 2.4 only environment variables were available for configuring this plugin. I am trying to use the slack module with ansible 2.1. I created a slack api token (recreated it today to test) and used it inside a role's task. However, when the task gets called, I get the error.
Sadly there isn’t one global failure handler configuration in Ansible. I investigated the native Slack Callback plugin available in Ansible 2.x (and not to be confused with the existing Slack module) but this seemed to be more of a generic passthrough for outputting all plays into Slack, failed or not, which isn’t what I wanted. After discarding the idea of a custom callback plugin for my purposes (which could work, but felt overly complex), I settled on a per-playbook failure role.
This is not an actual ‘handler’ as per Ansible parlance, but I was coming from experience of Chef where I could have a global failure handler baked into the Chef-client config.
To make this work we need to use Playbook Blocks (as of 2.0) and essentially enclose the entire playbook in a block/rescue. The main hassle here is that block does not support the top-level pre-task role block (and wouldn’t catch any failures therein), and so I had to convert all of my role calls to tasks that used include_role instead.
A simple playbook example looks as follows:
playbooks/playbook.yml
And in my slack_handler role (for reusability):
roles/slack_handler/tasks/failure.yml
ansible_failed_task and ansible_failed_result are two currently painfully undocumented (shout-out to Brian Coca for pointing me in the right direction) but delightfully detailed variables that are populated on playbook failure. ansible_failed_task is a map that contains a lot of data, so you may want to add additional debug for your purposes. The raw error message is single-line JSON so we prettify it before sending it using the Slack module using our token. The string split is a bit of an ugly hack to extract the webhook token part from the full webhook URL which is used elsewhere in my plays and passed at deploy time. For some reason the Slack module requires only the token rather than the full URL, in contrast to a lot of other integrations that want the whole thing.
The remaining hassle is the need to implement this per playbook, but aside from the possibility of a custom callback plugin this seems like the simplest way to implement this in Ansible currently.
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:
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.
Servicenow Ansible Tower
Running
Ansible Slack Upload File
When you next run a playbook you will see the messages come through into the selected channel.