# v2 callback plugin migration

**URL:** <https://forum.ansible.com/t/v2-callback-plugin-migration/20209>\
**Category:** Ansible Developer\
**Created:** [January 14, 2016, 6:50am UTC](https://forum.ansible.com/t/v2-callback-plugin-migration/20209 "2016-01-14T06:50:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [January 14, 2016, 6:50am UTC](https://forum.ansible.com/t/v2-callback-plugin-migration/20209/1 "2016-01-14T06:50:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 14, 2016, 4:11pm UTC](https://forum.ansible.com/t/v2-callback-plugin-migration/20209/2 "2016-01-14T16:11:55Z")

</div>

Jon,

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

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [January 14, 2016, 5:42pm UTC](https://forum.ansible.com/t/v2-callback-plugin-migration/20209/3 "2016-01-14T17:42:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tinyzais](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@tinyzais](https://forum.ansible.com/u/tinyzais)\
**Post date:** [February 15, 2016, 1:14pm UTC](https://forum.ansible.com/t/v2-callback-plugin-migration/20209/4 "2016-02-15T13:14:46Z")

</div>

This approach works for me.  
Thank you kindly 😉

---

<div class="post-metadata">

**Author:** ![Naveen\_reddy](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@Naveen\_reddy](https://forum.ansible.com/u/Naveen_reddy)\
**Post date:** [January 27, 2017, 6:45am UTC](https://forum.ansible.com/t/v2-callback-plugin-migration/20209/5 "2017-01-27T06:45:14Z")

</div>

hi,

can you please post your code…

Regards  
Naveen

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [January 27, 2017, 9:06am UTC](https://forum.ansible.com/t/v2-callback-plugin-migration/20209/6 "2017-01-27T09:06:46Z")

</div>

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](http://docs.ansible.com/ansible/intro_installation.html#running-from-source)

The change will be in forthcoming ansible 2.3 version.

Jon

---

<div class="post-metadata">

**Author:** ![Ludovic\_Petetin](https://avatars.discourse-cdn.com/v4/letter/l/f14d63/32.png) [@Ludovic\_Petetin](https://forum.ansible.com/u/Ludovic_Petetin)\
**Post date:** [August 30, 2018, 9:53am UTC](https://forum.ansible.com/t/v2-callback-plugin-migration/20209/7 "2018-08-30T09:53:53Z")

</div>

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