How to get the ansible command line information like current tags, skip-tags, limit, etc?

261 views
Skip to first unread message

Jordan Cohen

unread,
Sep 1, 2016, 11:55:01 AM9/1/16
to Ansible Project
I am integrating some automated slack messaging in ansible to inform on ansible runs and it would be useful to display information such as:

Current tags in push
Current skip-tags in push
Current limit (though I know i can use play_hosts for listing the servers)

To be honest, the whole ansible command would be helpful.  I am doing this with a local action shell now:

shell ps aux | grep 'ansible-playbook' | grep -v grep

...but besides it being very janky, this is unpredictable if there are multiple ansible-playbooks running on one host, like in my jenkins server.

I scanned all the built in vars that I know of for some of this information but no luck.  

Thanks!

Matt Martz

unread,
Sep 1, 2016, 12:07:18 PM9/1/16
to ansible...@googlegroups.com
Jordan,

Have you looked at the slack callback plugin that is already bundled with ansible?

In your ansible.cfg you would need to have something like:

[defaults]
callback_whitelist = slack

The docstring for the callback has information about what environment variables need to be set up for it to work:


To get it to display things like tags, skip tags, limit, etc...you will need to set SLACK_INVOCATION=True

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/97c23341-c49a-447c-beea-a9592c8a1959%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Jordan Cohen

unread,
Sep 1, 2016, 12:26:10 PM9/1/16
to Ansible Project
The callback plugin is really nice, i didn't know it existed. 

For my purposes though, I would like to use the slack module so that I can more custom tailor the message, the channel to post to, and when to skip a slack message (for local/dev environments)

But thank you, this is cool.


On Thursday, September 1, 2016 at 12:07:18 PM UTC-4, Matt Martz wrote:
Jordan,

Have you looked at the slack callback plugin that is already bundled with ansible?

In your ansible.cfg you would need to have something like:

[defaults]
callback_whitelist = slack

The docstring for the callback has information about what environment variables need to be set up for it to work:


To get it to display things like tags, skip tags, limit, etc...you will need to set SLACK_INVOCATION=True
On Thu, Sep 1, 2016 at 10:40 AM, Jordan Cohen <hype...@gmail.com> wrote:
I am integrating some automated slack messaging in ansible to inform on ansible runs and it would be useful to display information such as:

Current tags in push
Current skip-tags in push
Current limit (though I know i can use play_hosts for listing the servers)

To be honest, the whole ansible command would be helpful.  I am doing this with a local action shell now:

shell ps aux | grep 'ansible-playbook' | grep -v grep

...but besides it being very janky, this is unpredictable if there are multiple ansible-playbooks running on one host, like in my jenkins server.

I scanned all the built in vars that I know of for some of this information but no luck.  

Thanks!

--
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

unread,
Sep 1, 2016, 12:31:38 PM9/1/16
to ansible...@googlegroups.com
So I guess to answer your question directly, there is no way to access tags, skip tags, limit and most other command line options from within a play.

It's really only achievable via a callback.

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/de06d9d3-f81a-4993-9c5a-3a5826e1ce2b%40googlegroups.com.

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

epiq sty

unread,
Mar 13, 2017, 10:33:25 AM3/13/17
to Ansible Project
Hello!

I have the same issue with necessity of tags, limits, etc using slack as callback , and found strange thing:

If i set SLACK_INVOCATION=True , nothing happens.

Actually, in ansible/plugins/callback/slack.py where is another dependency:

    def v2_playbook_on_start(self, playbook):
        self.playbook_name = os.path.basename(playbook._file_name)

        title = [
            '*Playbook initiated* (_%s_)' % self.guid
        ]
        invocation_items = []
------>>    if self._options and self.show_invocation:
            tags = self._options.tags
            skip_tags = self._options.skip_tags
            extra_vars = self._options.extra_vars
            subset = self._options.subset

And by default seem's `self._options` set to none:

How I can properly  set "self._options" to get all necessary details?

Now I found only one way , by hacking ansible/plugins/callback/slack.py with 

    def v2_playbook_on_start(self, playbook):
        self.playbook_name = os.path.basename(playbook._file_name)

        title = [
            '*Playbook initiated* (_%s_)' % self.guid
        ]
        invocation_items = []
------>>    self._options = cli.options
          if self._options and self.show_invocation:
            tags = self._options.tags
            skip_tags = self._options.skip_tags
            extra_vars = self._options.extra_vars
            subset = self._options.subset

But I am sure, must been some other , better way here ;)
Can anyone advise ? 
Thanks in advance!

ps:
$ ansible --version
ansible
2.2.1.0
$
set | grep INV
SLACK_INVOCATION
=True



Reply all
Reply to author
Forward
0 new messages