Environment Variables - Slack.py

293 views
Skip to first unread message

Mark Matthews

unread,
Feb 4, 2016, 8:28:29 AM2/4/16
to Ansible Project

Hi @sivel

 

I am trying to get an Slack callback plugin to work (https://github.com/sivel/ansible/blob/slack-callback/plugins/callbacks/slack.py#L142-L147), but I am getting the following error:#

 

Unexpected Exception: 'module' object has no attribute 'warning'

 

You can see the what I am trying to achieve here:  https://github.com/ansible/ansible/pull/8955

 

When looking at the file I see that it mentions the following:

class CallbackModule(object):

    """This is an example ansible callback plugin that sends status

    updates to a Slack channel during playbook execution.

    This plugin makes use of the following environment variables:

        SLACK_TOKEN    (required): Slack Integration token

        SLACK_TEAM     (required): Slack team name TEAM.slack.com

        SLACK_CHANNEL  (optional): Slack room to post in. Default: #ansible

        SLACK_USERNAME (optional): Username to post as. Default: ansible

    Requires:

        prettytable

    """

 

Where would I need to insert these SLACK environment variables in order for this file to access this information?

Matt Martz

unread,
Feb 4, 2016, 8:47:48 AM2/4/16
to ansible...@googlegroups.com
Those environment variables need to be set in the shell that you execute Ansible from.  Also that branch/PR that you reference is for Ansible 1.9.  If you need a version that works with Ansible 2.0, see https://github.com/ansible/ansible/pull/13408
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/80f9eb05-539d-475a-832d-5d51a68f90c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Mark Matthews

unread,
Feb 4, 2016, 9:07:16 AM2/4/16
to Ansible Project
Hi Matt

I have now updated the slack.py with the following code (https://github.com/sivel/ansible/blob/v2-slack-callback/lib/ansible/plugins/callback/slack.py), as I am running Ansible v2.

I have added the environmental variables into a ver basic playbook to see if it posts the results to Slack (see below):
---
- name: Check if line is present in config
  hosts: all
  environment:
        SLACK_TOKEN: xxxxxxx/yyyyyyy/token
        SLACK_TEAM: companyname.slack.com
        SLACK_CHANNEL: #ansible

  tasks:
    - name: Check if line is present in config
      win_lineinfile:
        dest: C:\Websites\Live\Web.config
        regexp: <test>
        line: ' <test>'

The Playbook runs successfully, but nothing is being sent to Slack.
Is there something that I am missing? Or am I putting the environmental variables in the wrong place?

Cheers
Mark

On Thursday, February 4, 2016 at 1:47:48 PM UTC, Matt Martz wrote:
Those environment variables need to be set in the shell that you execute Ansible from.  Also that branch/PR that you reference is for Ansible 1.9.  If you need a version that works with Ansible 2.0, see https://github.com/ansible/ansible/pull/13408

On Thursday, February 4, 2016, Mark Matthews <mdmat...@gmail.com> wrote:

Hi @sivel

 

I am trying to get an Slack callback plugin to work (https://github.com/sivel/ansible/blob/slack-callback/plugins/callbacks/slack.py#L142-L147), but I am getting the following error:#

 

Unexpected Exception: 'module' object has no attribute 'warning'

 

You can see the what I am trying to achieve here:  https://github.com/ansible/ansible/pull/8955

 

When looking at the file I see that it mentions the following:

class CallbackModule(object):

    """This is an example ansible callback plugin that sends status

    updates to a Slack channel during playbook execution.

    This plugin makes use of the following environment variables:

        SLACK_TOKEN    (required): Slack Integration token

        SLACK_TEAM     (required): Slack team name TEAM.slack.com

        SLACK_CHANNEL  (optional): Slack room to post in. Default: #ansible

        SLACK_USERNAME (optional): Username to post as. Default: ansible

    Requires:

        prettytable

    """

 

Where would I need to insert these SLACK environment variables in order for this file to access this information?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Matt Martz

unread,
Feb 4, 2016, 9:12:39 AM2/4/16
to ansible...@googlegroups.com
In Ansible v2 you must whitelist callback plugins in ansible.cfg.  Additionally, the environment vars must be set in your shell, not in the playbook.

In bash this would be done from a .bash_profile or directly on the command line by using:

export SLACK_TOKEN=mytoken 

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.


--
Matt Martz
@sivel
sivel.net

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d5cd76d1-6491-4a02-8799-0e01613c6756%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Mark Matthews

unread,
Feb 4, 2016, 9:45:27 AM2/4/16
to Ansible Project
Hi Matt

Thanks for your quick responses.

I have now added SLACK_WEBHOOK_URL and SLACK_CHANNEL to the environment var in the shell

        SLACK_CHANNEL=#ansible

I have also added the withlist entry into ansible.cfg:

# set plugin path directories here, separate with colons
action_plugins     = /usr/share/ansible_plugins/action_plugins
callback_plugins   = /usr/share/ansible_plugins/callback_plugins
connection_plugins = /usr/share/ansible_plugins/connection_plugins
lookup_plugins     = /usr/share/ansible_plugins/lookup_plugins
vars_plugins       = /usr/share/ansible_plugins/vars_plugins
filter_plugins     = /usr/share/ansible_plugins/filter_plugins

callback_whitelist = slack.py


I then try run the playbook:

---
- name: Check if line is present in config
  hosts: all
  tasks:
    - name: Check if line is present in config
      win_lineinfile:
        dest: C:\Websites\Live\Web.config
        regexp: <test>
        line: ' <test>'

The playbook run successfully, but nothing is sent to Slack. And im not getting any errors.

Is there anything that you can see that I am missing, or that I have done something wrong?

Cheers
Mark
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.


--
Matt Martz
@sivel
sivel.net

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Matt Martz

unread,
Feb 4, 2016, 10:12:22 AM2/4/16
to ansible...@googlegroups.com
I think the whitelist is just slack without the file extension.

Also, if you run Ansible with higher verbosity, it will show you which callbacks are being loaded.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.


--
Matt Martz
@sivel
sivel.net

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.


--
Matt Martz
@sivel
sivel.net

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a85ec092-9164-4068-9566-df9c3e43532c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Feb 4, 2016, 10:54:59 AM2/4/16
to Ansible Project
you should not need to whitelist custom plugins, that was done only
for the plugins shipped with ansible (which have a variable that
indicates they NEED the whitelist to work).


--
Brian Coca

Mark Matthews

unread,
Feb 4, 2016, 10:55:28 AM2/4/16
to Ansible Project
All working mate! Thank you so much for your help!

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.


--
Matt Martz
@sivel
sivel.net

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.


--
Matt Martz
@sivel
sivel.net

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Mark Matthews

unread,
Feb 8, 2016, 12:40:40 PM2/8/16
to Ansible Project
Hi guys

This is working perfectly running playbooks from the server. But when I run any playbooks from Ansible Tower nothing is being send to Slack.

Is there something that you know that needs to be done in order for this to work?


Cheers
Mark
Reply all
Reply to author
Forward
0 new messages