What's New in Tower 3: Notifications

October 14, 2016 by Matt Jones

Ansible-Tower-3-blog-series.png

In July, we released Ansible Tower 3. In this blog series, we will take a deeper dive into Tower changes that were all designed to make our product simpler and easier to scale Ansible automation across your environments. In our last post, Jared Tabor, Ansible Tower Software Engineer, highlights what's new in the Tower 3 user interface.

If you’d like to learn more about the release, our Director of Product Bill Nottingham wrote a complete overview of the Ansible Tower 3 updates.

Introducing Tower Notifications

One of the exciting new features of Ansible Tower 3 is notifications. Tower notifications provide a mechanism of signaling when Tower jobs succeed or fail. This can take the form of sending a message to a Slack channel or sending an HTTP POST to another service to trigger other actions.

In Tower 3 we support the following notification types:

  • Email
  • Slack
  • Hipchat
  • Pagerduty
  • Twilio
  • IRC
  • Webhook (POST)


Concepts

There are two important concepts to understand when it comes to notifications in Tower.

1. Notification Templates

These define the properties of where a notification will be sent and who will get notified. If you are using the Slack notification type, then this will include the token and a list of channels that will receive the notification. Notification templates are owned by an organization and managed by organization administrators.

2. Triggers

These are special properties of objects in Ansible Tower to which you can assign a notification template. The triggers can fire on the following events:

Event Happens when...
Success A job finishes without error. For a job template, this happens when the ansible-playbook return code is 0.
Error A job finishes with an error. For a project github SCM sync, this can happen when you do not have permission to the repository.
Any A job finishes. Success or Error.

This is especially obvious in the case of a job template, but you can also assign notification templates to cloud inventory groups and projects. In that case, whenever you run an inventory or project synchronization, you can be notified when each succeeds or fails.

Hierarchy

Notification templates can also be assigned to other objects in Tower allowing them to be inherited by other objects. The best example of this is assigning a notification template to a project so that the notification template can used by any job template that uses that project.

Notification templates assigned to an organization will cause any job run within that organization to inherit those notifications.

When notification templates are assigned to cloud inventory groups, any ad-hoc command run on those hosts will trigger notifications from those templates.

Slack Example

In this first example, we’ll create a Slack bot and use it to send a notification. First we need to create the slack bot.

Tower3-Custom-Integrations.png

Select the “Custom Integrations” option in the “Manage” section of your team’s Slack settings. After you create the bot, make a note of its token. Then be sure to invite it to the channels to which you want it to be able to send notifications.

Now we’ll set up the notification template in Tower.

Tower3-Settings.png

Notifications are configured from Tower’s settings screen.

Tower-3-Notifications-4.png

We’ll create a notification template using the token generated by Slack and any destination channels.

Tower3-Notifications-5.png

You did already invite the bot to the channel you wanted to send a notification to, right?

Tower3-Notifications-6.png

Tower provides a button for sending a test notification.

Tower3-Notifications-7.png

Now that we know the notification template works, let’s assign it to a job template.

Tower3-Notifications-8.png

And use the “Success” and “Failure” triggers.

Tower3-Notifications-9.png

Now launch the job template and watch the Slack channel.

Tower3-Notifications-10.png

You aren’t limited to just assigning one notification template to a specific trigger, you can assign as many as you want. If any are duplicated, (e.g., if you use the same notification template on a project or organization that will be inherited when this job template runs) it will only send one notification.

Webhook Example

Webhooks extend general informative notifications to give you the ability to trigger other actions or integrate with other systems.

First, let’s set up a basic web service that can accept POST requests and verify the request by examining the header.

gist.github.com/matburt/db023a9c8e5361f897ddfbc7e48f82b8
#!/usr/bin/env python
from flask import Flask, request, abort, jsonify
app = Flask(__name__)

@app.route('/push', methods=['POST'])
def handle_push():
    if not request.json:
        abort(400)
    if 'AnsibleTower' in request.headers and request.headers['AnsibleTower'] == 'xSecretx':
        # Trigger some other external action
        print("Request dictionary: {}".format(request.json))
        return jsonify({'status': 'triggered'}), 201
    return jsonify({'status': 'ok'}), 200

if __name__ == '__main__':
    print("Listening...")
    app.run(debug=True, host='0.0.0.0', port=8085)

This is a basic Flask web app that listens on a port and looks for a key and secret in the request header.

You’ll need Flask installed:

$ pip install flask

Running it is simple:

python flask_handler.py

We can verify it with a simple curl command:

$ curl -H "AnsibleTower: xSecretx" -X POST -d '{"test": "Test"}' 
-H "Content-Type: application/json" http://localhost:8085/push { "status": "triggered" }

Now let’s configure the notification template in Tower:

Tower3-Notifications-11.png

Then after associating it with a job template and running you’ll see the output on the console:

Request dictionary: 
{u'status': u'successful', u'credential': u'Demo Credential', u'name': u'Demo Job Template', u'started': u'2016-07-25T16:42:02.007517+00:00', u'extra_vars': u'{}', u'traceback': u'', u'friendly_name': u'Job', u'created_by': u'admin', u'project': u'Demo Project',u'url': u'https://towerhost/#/jobs/16', u'finished': u'2016-07-25T16:42:13.569564+00:00', u'hosts': {}, u'playbook': u'hello_world.yml', u'limit': u'', u'id': 16, u'inventory': u'Demo'}
127.0.0.1 - - [25/Jul/2016 16:42:15] "POST /push HTTP/1.1" 201 -

Try the new notifications in Ansible Tower now

Ansible Tower 3 is available for anyone to try via local install, Vagrant image, or AMI. Try the latest version of Tower today.

Share:

Topics:
Ansible Tower, Tower 3


 

Matt Jones

Principal Software Engineer


rss-icon  RSS Feed

Ansible Tower by Red Hat
Learn About Ansible Tower