
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
vars_files:
- /etc/ansible/group_vars/crypto.yml
tasks:
- name: Sending message to slack channel
slack:
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.
/etc/ansible/roles/slack/
├── 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.
root@ansible:/etc/ansible/roles/slack/tasks/main.yml
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
slack:
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.
/etc/ansible/roles/slack/defaults/main.yml
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
tasks:
- name: Creates Gearman cronjob
cron: minute="*" hour="*" weekday="*" month="*"
name="Gearman-worker-run"
cron_file="gearman-worker-run"
user="gearman"
job="/usr/bin/php /home/gearman/gearmanRunWorker.php"
roles:
- { role: slack, message: 'Crontab for Gearman has been installed on {{ inventory_hostname }}'}
Tagged With: Ansible, Slack
Facebook Comments