v2 callback plugin migration

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

Jon,

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

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

This approach works for me.
Thank you kindly :wink:

hi,

can you please post your code…

Regards
Naveen

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

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