# Access to hostvars from Ansible callback

**URL:** <https://forum.ansible.com/t/access-to-hostvars-from-ansible-callback/17439>\
**Category:** Ansible Developer\
**Created:** [February 4, 2015, 9:21am UTC](https://forum.ansible.com/t/access-to-hostvars-from-ansible-callback/17439 "2015-02-04T09:21:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxime.gerges](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@maxime.gerges](https://forum.ansible.com/u/maxime.gerges)\
**Post date:** [February 4, 2015, 9:21am UTC](https://forum.ansible.com/t/access-to-hostvars-from-ansible-callback/17439/1 "2015-02-04T09:21:31Z")

</div>

Hello guys,

I have a question regarding ansible callback\_plugins and hostvars in the runner\_ok() method.

I can retrieve facts on a specific host from this method by using the ‘res’ argument provided.  
However I’d like to collect hostvars too in this method but from my custom CallbackModule I don’t have access to such information, nor do I have access to the runner itself which holds hostvars.

Do you have any trick or idea to be able to retrieve hostvar from a custom callback\_plugin runner\_ok() method ?

Thanks in advance 🙂

N.B : The first thing I think of is to create a new class that inherits from DefaultRunnerCallbacks in ansible.callback.py module

---

<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:** [February 4, 2015, 2:52pm UTC](https://forum.ansible.com/t/access-to-hostvars-from-ansible-callback/17439/2 "2015-02-04T14:52:39Z")

</div>

The big thing to know is that you have access to the play and playbook instances from your callback, such as:

self.playbook and self.play.

As such you have access to things like self.playbook.SETUP\_CACHE, self.playbook.VARS\_CACHE and self.playbook.inventory

---

<div class="post-metadata">

**Author:** ![Justin\_Lee](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@Justin\_Lee](https://forum.ansible.com/u/Justin_Lee)\
**Post date:** [February 4, 2015, 11:32pm UTC](https://forum.ansible.com/t/access-to-hostvars-from-ansible-callback/17439/3 "2015-02-04T23:32:58Z")

</div>

The Runner class has a method called get\_inejct\_vars.

inject = runner.get\_inject\_vars(host)

If you specify a host, it will yield a dictionary of variables. I’m not sure, but there might be a key in the inject dictionary that contains host\_vars and holds the variables. Hope that helps a bit.

Justin

---

<div class="post-metadata">

**Author:** ![maxime.gerges](https://avatars.discourse-cdn.com/v4/letter/m/e36b37/32.png) [@maxime.gerges](https://forum.ansible.com/u/maxime.gerges)\
**Post date:** [February 5, 2015, 6:47am UTC](https://forum.ansible.com/t/access-to-hostvars-from-ansible-callback/17439/4 "2015-02-05T06:47:25Z")

</div>

Thanks for the answer. I forgot to mention that I am using ansible as python API - meaning using directly the ansible Runner class.  
I don’t think I will have access to any playbook object in that situation.
