v2 callback plugin migration

1530 views
Skip to first unread message

jhawkesworth

unread,
Jan 14, 2016, 1:50:27 AM1/14/16
to Ansible Development
I started work on porting my callback plugin and I'm hoping someone can help me get going.

My old plugin used to get at a list of hosts like this (in on_start):

    hosts = self.playbook.inventory.get_hosts()

Then I'd loop through the hosts and fetch out host_vars for each one like this:

   host_vars = self.play.playbook.inventory.get_variables(host.name)



Is there any equivalent way of doing the above in V2?

I've been playing with the context_demo plugin and can see it gets passed an

ansible.playbook.Playbook object

when I look at that it has a get_plays method, but that doesn't seem to return anything.  Looking through the Playbook code, one of the things it does is load the plugins, so its like I'm trying to get at inventory (via the playbook) before it has been initialised.

Is there another way to get at host vars?  Or is there a different callback I can use, after the inventory has been loaded?

Many thanks,

Jon




Matt Martz

unread,
Jan 14, 2016, 11:12:37 AM1/14/16
to jhawkesworth, Ansible Development
Jon,

You may be able to access that data through `play._variable_manager` within `v2_playbook_on_play_start`

--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

jhawkesworth

unread,
Jan 14, 2016, 12:42:27 PM1/14/16
to Ansible Development, j.r.haw...@googlemail.com
Thank you! 

Using v2_playbook_on_play_start was a huge help.

so, the following is a bit messy but hopefully will help anyone else trying to do something similar...

in my  def v2_playbook_on_play_start(self, *args, **kwargs)

args[0] turned out to be Playbook object, which you can call get_variable_manager() on

you can get inventory from variable manager via 
_inventory

then you can get hosts by calling 
get_hosts() on the inventory

then you can iterate through the hosts 
and call get_vars()
or get_group_vars()
both of which are dicts, so you can lookup whatever values you are interested in.

Jon

tiny...@gmail.com

unread,
Feb 15, 2016, 8:14:47 AM2/15/16
to Ansible Development
This approach works for me.
Thank you kindly ;)

Naveen reddy

unread,
Jan 27, 2017, 1:45:14 AM1/27/17
to Ansible Development
hi,

can you please post your code..

Regards
Naveen

jhawkesworth

unread,
Jan 27, 2017, 4:06:46 AM1/27/17
to Ansible Development
My callback plugin has recently been superceeded by built-in support for acquiring kerberos tickets by calling kinit within ansible.

To try it out, use the latest development version of ansible.  Instructions on how to set this up are here: http://docs.ansible.com/ansible/intro_installation.html#running-from-source

The change will be in forthcoming ansible 2.3 version.

Jon

Ludovic Petetin

unread,
Aug 30, 2018, 5:53:54 AM8/30/18
to Ansible Development
I've been looking around for a while for the same thing for action plugins. As I've not been able to find the answer, here is what I came for.


Within the ActionModule class, you can get an inventory with :

        inventory = InventoryManager(self._loader, C.DEFAULT_HOST_LIST)

To access to the host variables, you can do : 

       inventory.get_host(self._connection.host).vars

You also need these imports :

      from ansible.inventory.manager import InventoryManager
      from ansible import constants as C

Hope this will help someone as I've been searching this for a while !!!

Reply all
Reply to author
Forward
0 new messages