Randomly Expressed


Welcome to my blog “randomly expressed”. I created this website to publish helpful tips. It’s mainly technology driven, but I will blog about other topics. I am a Unix sysadmin that is always looking to learn new things. My goal is to be able to share knowledge that others may find useful. xkcd.com

Continue Reading »


Connect With US

Connect with us on the following social networking sites.

Most Popular Posts.

Add Some Content to This Area

You should either deactivate this panel on the Theme Settings page, or add some content via the Widgets page in your WordPress dashboard.

Ansible Slack playbook

By on February 4, 2017 in Technology with No Comments

Creating an Ansible Slack playbook is a great way of posting messages in your slack channel from your various playbook runs. This keeps everyone else in the loop of the changes that are happening. The following is an Ansible playbook that uses the Slack module. The token is derived from your web hook URL that posts to a channel of your choice in Slack. I use the Incoming WebHooks integration in Slack to have the ability to post in channels via web hooks. Please keep in mind the tokens are not the API tokens but are the webhook tokens. In slack these are found in the webhook URL which are obtained under the apps and integrations. The following Ansible URL has all of the documentation for the Slack module. Ansible Slack Module Documentation
Ansible Slack playbook example

- name: Slack Notification

  become: yes
  become_user: root
    - /etc/ansible/group_vars/crypto.yml

  - name: Sending message to slack channel
      token: '{{ slack_token }}'
      channel: "#ops"
      domain: "company.slack.com"
      parse: "full"
      color: "good"
      msg: 'The blah playbook was ran on {{ inventory_hostname }}.'

Creating an Ansible Slack role
I then created a role in Ansbile so that I can easily add it to other playbooks with two lines of code instead adding all of the above code. In order to create a role in Ansible you need to create a slack directory under the roles directory and add the following two main.yml files to your defaults and tasks directories. The name of your role directory will be the name of the role called by the playbook.

├── defaults/main.yml
└── tasks/main.yml

In this example I created a role directory named slack under the roles directory. The first file will be the tasks main.yml. It will be placed under the following directory.
It will contain the following. I made the message a variable so that it can be defined in your playbook and passed on to the Slack role.

  - name: Send message to slack channel
      token: '{{ slack_token }}'
      channel: "#ops"
      domain: "company.slack.com"
      parse: "full"
      color: "good"
      msg: '{{ message }}.'

The second file needed for your Ansible Slack role is the defaults main.yml file. It will be placed under the slack following directory.
It will contain the message and slack token. I like to use the inventory_hostname variable so that I can add the hostname of the server the playbook is running on to the message.

# defaults file slack role

message: '{{ inventory_hostname }}.'

slack_token: **************

Now whenever I create an Ansible playbook I can simply add the Slack role to it, which is much faster. Here is an example of an Ansible cron playbook that is using the Slack role. The name of the role is slack and it’s based on the name of directory that I created under the roles directory. For my example it was /etc/ansible/roles/slack. It will now call that Slack role and pass the message to the message variable of the slack role we created.

- hosts: prod-gearman
  become: yes
  become_user: root

  - name: Creates Gearman cronjob
    cron: minute="*" hour="*" weekday="*" month="*"
          job="/usr/bin/php /home/gearman/gearmanRunWorker.php"

    - { role: slack,  message: 'Crontab for Gearman has been installed on {{ inventory_hostname }}'}

Facebook Comments

Tagged With: ,

Post a Comment

Your email address will not be published. Required fields are marked *