# Using setup facts in the following callback

**URL:** <https://forum.ansible.com/t/using-setup-facts-in-the-following-callback/12467>\
**Category:** Ansible Project\
**Created:** [March 7, 2013, 2:13pm UTC](https://forum.ansible.com/t/using-setup-facts-in-the-following-callback/12467 "2013-03-07T14:13:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Laredo](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@Tom\_Laredo](https://forum.ansible.com/u/Tom_Laredo)\
**Post date:** [March 7, 2013, 2:13pm UTC](https://forum.ansible.com/t/using-setup-facts-in-the-following-callback/12467/1 "2013-03-07T14:13:29Z")

</div>

Hi everyone,

I’m writing several callbacks and modules for my ansible set and I’m wondering if there is a preexisting/simple way to use ansible facts given by setup module in a following callback (different from the first one).  
In my case, I have a callback in which I want to use the hostname and the architecture of the node.  
In my playbook, setup runs well, a first callback is called and then my playbook continues with a second (custom) module.  
After this second module execution I would use the ansible facts provided by the setup module and the only way I found to do this is to write facts in a file when the first callback is executed and then open and read this file in the second callback.

Thanks for reading.

---

<div class="post-metadata">

**Author:** ![Tom\_Laredo](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@Tom\_Laredo](https://forum.ansible.com/u/Tom_Laredo)\
**Post date:** [March 7, 2013, 4:46pm UTC](https://forum.ansible.com/t/using-setup-facts-in-the-following-callback/12467/2 "2013-03-07T16:46:27Z")

</div>

Here is a little diagram that I just drew to make you see how I want it to work (and maybe that you’ll say “OMG, there are so many better ways to do that!” 🙂 ).  
My problem is between the two callback executions in providing ansible setup facts in th second execution.

> **(attachments)**
>
> ![schema_callback_execution_anglais.png](https://us1.discourse-cdn.com/ansible/original/2X/c/cebbdbf79a43e699f828528f49d84331af30c11e.png)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [March 7, 2013, 4:49pm UTC](https://forum.ansible.com/t/using-setup-facts-in-the-following-callback/12467/3 "2013-03-07T16:49:37Z")

</div>

I'm going to be improving callback "context" very shortly so you'll be  
able to get at a very wide variety of things.

Presently there are some limitations about what sort of objects you get handed.

We'll be able to get at a 'context' object which will contain the  
runner and playbook objects, and you'll be able to get at hostvars  
from there to pull out what you want.

---

<div class="post-metadata">

**Author:** ![Tom\_Laredo](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@Tom\_Laredo](https://forum.ansible.com/u/Tom_Laredo)\
**Post date:** [March 7, 2013, 4:59pm UTC](https://forum.ansible.com/t/using-setup-facts-in-the-following-callback/12467/4 "2013-03-07T16:59:59Z")

</div>

Ok, that’s verry cool! So I’ll wait for that.  
\<hope=on\>Do you think that it will be available in the next version?  
Thanks for your awsome work!

2013/3/7 Michael DeHaan \<[michael.dehaan@gmail.com](mailto:michael.dehaan@gmail.com)\>

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [March 8, 2013, 4:48am UTC](https://forum.ansible.com/t/using-setup-facts-in-the-following-callback/12467/5 "2013-03-08T04:48:55Z")

</div>

> Ok, that's verry cool! So I'll wait for that.  
> \<hope=on\>Do you think that it will be available in the next version?\</hope\>

It's very very very likely. We are going to need it for some  
features in ansible-commander (REST layer/UI).

> Thanks for your awsome work!

You are welcome!

---

<div class="post-metadata">

**Author:** ![Tom\_Laredo](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@Tom\_Laredo](https://forum.ansible.com/u/Tom_Laredo)\
**Post date:** [April 4, 2013, 3:36pm UTC](https://forum.ansible.com/t/using-setup-facts-in-the-following-callback/12467/6 "2013-04-04T15:36:29Z")

</div>

Hi,

I just installed Ansible 1.1 and I am currently discovering the (amazing) new features.  
I still need to use setup infos in my custom “dpkg” playbook and I am trying to understand the new mecanism which allows us to do it.  
Unfortunately, I don’t manage to get those informations (hostname, architecture…) in my playbook.  
I already tried to use getattr(self, ‘playbook’, None) in my callback (where self = \<ansible.callback\_plugins.callback\_dpkg\_tom.CallbackModule object\>) but it gives me a object.  
I also don’t really understand from where I can take self.playbook, self.play etc…

Could you please help me understand that?

For now, I added this string at the end of the result of my “dpkg” custom module : “changed”: “False”, “invocation2”: {“host\_ip”: “192.168.93.37”, “hostname”: “eddv-fnpf-front1-ansible”, “module\_args”: “”, “module\_name”: “dpkg\_tom”}}

and I’m getting it back with this :

8\< --------------------------------------------------------------  
invocation2 = data.pop(‘invocation2’, None)

if invocation2 is None :  
return  
else:  
data = json.dumps(invocation2)  
if invocation2.get(‘module\_name’, None) != ‘dpkg\_tom’:  
return  
8\< --------------------------------------------------------------

but it is kind of a realy bad hack I think…  
Have you got any idea to do it properly?

Best regards,  
Tom

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 4, 2013, 6:26pm UTC](https://forum.ansible.com/t/using-setup-facts-in-the-following-callback/12467/7 "2013-04-04T18:26:01Z")

</div>

So if I understand, you are writing a custom callback and want to  
respond to your module named 'dpkg\_tom' differently. Got that.

The actual module used per task is not recorded in self.play.task  
because that's more about the task object. I can see us upgrading  
that.

The invocation hash should be fine to use. There is nothing in  
ansible that stores a hash entry called 'invocation2'

---

<div class="post-metadata">

**Author:** ![Tom\_Laredo](https://avatars.discourse-cdn.com/v4/letter/t/ecc23a/32.png) [@Tom\_Laredo](https://forum.ansible.com/u/Tom_Laredo)\
**Post date:** [April 4, 2013, 6:50pm UTC](https://forum.ansible.com/t/using-setup-facts-in-the-following-callback/12467/8 "2013-04-04T18:50:09Z")

</div>

Yes. I have :

- a custom module called “dpkg\_tom” which returns the liste of pakages installed (with version etc.) in JSON

- a callback “callback\_setup\_inventory.py” which runs only after to have received the “setup” module result

- another callback “callback\_dpkg\_tom.py” which runs only after to have received the “dpkg\_tom” module result

My final goal is to gather packages and hosts informations to put those informations into a database.

The execution of the “setup” module (and then of “callback\_setup\_inventory.py”) put the informations about the host into a table of my database.  
The execution of the “dpkg\_tom” module (and then of “callback\_dpkg\_tom.py”) put the informations about packages related to the host into another table of my database.

So, in “callback\_dpkg\_tom.py” I have to know some host informations (given by the “setup” module) like hostname, architecture etc.

I don’t know why but I just can’t use “invocation” in “callback\_dpkg\_tom.py” (wherease I do in “callback\_setup\_inventory.py”) so my workaround was to add an “invocation2” JSON hash appended to the “dpkg\_tom” module so I’m able to retrieve thos host informations.

Hope that is clearer.

If it’s not… well… I’ll take some english course =P

Tom

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 5, 2013, 9:02pm UTC](https://forum.ansible.com/t/using-setup-facts-in-the-following-callback/12467/9 "2013-04-05T21:02:40Z")

</div>

I don’t like your idea about adding an ‘invocation2’ but unfortuantely do not have bandwidth to help troubleshoot custom callback plugin Python code.

Someone else may.
