Introduction

StackStorm is IFTTT for ops. Its original claim to fame was popularizing chat driven workflows for ops know as ChatOps. In this post I will cover integrating ST2 with Slack to enable ChatOps.

Lab Environment

The ST2 instance connects out to Slack which is a SAAS application. This means ST2 must have access to the internet (at least to Slack) for the setup to function.

For reference I am using Vagrant to build this lab and utilizing the following code versions.

  • StackStorm - 2.10.1
  • Juniper vMX - 18.2R1.9

This post assumes that ST2 is already installed with the ChatOps service enabled. This should already be done if you used one of the installer methods eg: Docker, Puppet, etc.. The Napalm pack should be installed and configured and you also have a Slack workspace configured with permissions to add an app integration.

Slack Configuration

First up lets tackle the Slack piece. Navigate to https://api.slack.com/apps and click on Create New App.

Enter the app name and select the workspace the click on Create App.

Click on the Bots option.

Click on Add a Bot User.

Enter the display name and default username of the bot and click on Add Bot User.

On the left menu select OAuth & Permissions then click on Install App to Workspace.

Confirm the details are correct and click on Authorize.

Take note of the Bot User OAuth Access Token as it will need to be defined in the st2chatops.env configuration file.

Now head over to the Slack chat and invite the bot to a channel by referring to it by @username.

StackStorm Configuration

ChatOps configurations live under the /opt/stackstorm/chatops directory.

Edit the st2chatops.env file adding/editing the following parameters.

bash

export HUBOT_NAME="bottlo"
export HUBOT_ALIAS='$bottlo'

export ST2_AUTH_USERNAME="${ST2_AUTH_USERNAME:-st2admin}"
export ST2_AUTH_PASSWORD="${ST2_AUTH_PASSWORD:-Ch@ngeMe}"

export HUBOT_ADAPTER=slack
export HUBOT_SLACK_TOKEN="xoxb-BOT-TOKEN-FROM-SLACK-FROM-PREVIOUS-STEP"
            

Note

For simplicity I am using the default credentials for ST2 that are configured out of the box from install. In production you would use PAM/LDAP or one of the other authentication backends and use an authentication token instead of a user password to authenticate to the ST2 API.

Now restart the ChatOps service.

cmd

sudo systemctl restart st2chatops
            

Bot Interaction

First lets call out to the bot and ensure it is connected to Slack correctly. Initially you may have to @botname to register it in the channel. Once it is connected you can call it via the alias you configured on the st2chatops.env file. In this case $bottlo

$botname help will give you a list of ChatOps aliases that are available to have the bot do some work.

In the below example I used the bot to install the napalm pack.

Add ChatOps Commands

Aliases allow you to add commands to your bot. ST2 has a couple of aliases out of the box but in order to really make the bot useful you have to customize it to your needs.

Aliases belong to packs. Packs live in the the /opt/stackstorm/packs/ directory. Create a directory for a pack to define your custom aliases.

cmd

sudo mkdir -p /opt/stackstorm/packs/bottlops/{actions,rules,sensors,aliases}
            

Lets add a command that allows us to view the LLDP neighbors of a network device. Create a file named lldp.yaml under the /opt/stackstorm/packs/bottlops/aliases/ directory.

yaml

sudo tee /opt/stackstorm/packs/bottlops/aliases/lldp.yaml > /dev/null << "EOF"
---
name: "napalm_lldp"
action_ref: "napalm.get_lldp_neighbors"
description: "Get devices lldp neighbors"
formats:
  - "get lldp neighbors {{hostname}}"
EOF
            

Now register the new commands and restart the ChatOps service.

cmd

sudo st2ctl reload --register-aliases
sudo systemctl restart st2chatops
            

Confirm the new command is registered.

cmd

sudo st2 action-alias list

# output
+---------------------------+------------+-----------------------------------------+---------+
| ref                       | pack       | description                             | enabled |
+---------------------------+------------+-----------------------------------------+---------+
| bottlops.napalm_lldp      | bottlops   | Get devices lldp neighbors              | True    |
| napalm.check_consistency  | napalm     | Check consistency of the device's       | True    |
|                           |            | configuration                           |         |
| packs.pack_get            | packs      | Get information about installed         | True    |
|                           |            | StackStorm pack.                        |         |
| packs.pack_install        | packs      | Install/upgrade StackStorm packs.       | True    |
| packs.pack_search         | packs      | Search for packs in StackStorm Exchange | True    |
|                           |            | and other directories.                  |         |
| packs.pack_show           | packs      | Show information about the pack from    | True    |
|                           |            | StackStorm Exchange.                    |         |
| st2.st2_actions_list      | st2        | List available StackStorm actions.      | True    |
| st2.st2_executions_get    | st2        | Retrieve details for a single           | True    |
|                           |            | execution.                              |         |
| st2.st2_executions_list   | st2        | List available StackStorm executions.   | True    |
| st2.st2_executions_re_run | st2        | Re-run an action execution.             | True    |
| st2.st2_inquiry_respond   | st2        | Respond to an Inquiry                   | True    |
| st2.st2_rules_list        | st2        | List available StackStorm rules.        | True    |
| st2.st2_sensors_list      | st2        | List available StackStorm sensors.      | True    |
+---------------------------+------------+-----------------------------------------+---------+
            

Great the command is there, now go back to slack and test it out.

Success! ST2 is connected to Slack and our custom command is understood and actioned by the bot.

Summary

ChatOps is a pretty neat way to interact with you infrastructure. ST2 makes integration and configuration of ChatOps with a chat service such as Slack relatively painless. Teaching the bot new commands is also pretty straight forward once you get your head around all the pieces.

stackstorm chatops