# callback and facts

**URL:** <https://forum.ansible.com/t/callback-and-facts/16430>\
**Category:** Ansible Developer\
**Created:** [October 4, 2014, 3:10pm UTC](https://forum.ansible.com/t/callback-and-facts/16430 "2014-10-04T15:10:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxime.gerges31](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@maxime.gerges31](https://forum.ansible.com/u/maxime.gerges31)\
**Post date:** [October 4, 2014, 3:10pm UTC](https://forum.ansible.com/t/callback-and-facts/16430/1 "2014-10-04T15:10:07Z")

</div>

Hello,

I’m trying to tune ansible in order to :

1. Connect to a list of Cisco devices (switches + router) and pull facts from the devices (S/N, interfaces,…)

2. Create a report with each fact’s devices in a CSV file  
For the 1st point I’m currently writing a new module based on Exscript that connects to the device, issue some _show_ commands, parse the output and create some facts based on those outputs.

For the 2nd point I was thinking of a callback plugin using the on\_stats methods. Indeed the on\_stats method is called at the end of the playbook.

**Unfortunately, I don’t understand from the ansible code how facts are created and where in the playbook they are avalaible.**

Is it possible to retrieve facts at the end of the playbook for all hosts reachable ?

Thanks for you help 🙂

---

<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:** [October 4, 2014, 3:44pm UTC](https://forum.ansible.com/t/callback-and-facts/16430/2 "2014-10-04T15:44:08Z")

</div>

You should be able to do something like:

for host in self.playbook.inventory.get\_hosts():  
facts = host.get\_variables()

# Do something with facts

---

<div class="post-metadata">

**Author:** ![maxime.gerges31](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@maxime.gerges31](https://forum.ansible.com/u/maxime.gerges31)\
**Post date:** [October 4, 2014, 9:04pm UTC](https://forum.ansible.com/t/callback-and-facts/16430/3 "2014-10-04T21:04:32Z")

</div>

Thanks for your answer ! It works great for variables but as far as I tested it doesn’t get facts out.  
I ran a setup module which pull down a lot of fact from a linux box but those facts are not in get\_variables().

Nevertheless I will investigate on this little issue.

---

<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:** [October 4, 2014, 10:39pm UTC](https://forum.ansible.com/t/callback-and-facts/16430/4 "2014-10-04T22:39:50Z")

</div>

Ah, in that case, something like this may be what you want:

hostvars = utils.merge\_hash(self.playbook.SETUP\_CACHE.copy(), self.playbook.VARS\_CACHE)

That will also require import ansible.utils:

from ansible import utils

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 5, 2014, 6:07pm UTC](https://forum.ansible.com/t/callback-and-facts/16430/5 "2014-10-05T18:07:25Z")

</div>

I’d strongly suggest a template instead of a callback, it would be a lot cleaner.

---

<div class="post-metadata">

**Author:** ![maxime.gerges31](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@maxime.gerges31](https://forum.ansible.com/u/maxime.gerges31)\
**Post date:** [October 19, 2014, 2:09pm UTC](https://forum.ansible.com/t/callback-and-facts/16430/6 "2014-10-19T14:09:44Z")

</div>

Hello michael,

What do you mean about a template ? what is it really and how to use it ?

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [October 20, 2014, 9:38pm UTC](https://forum.ansible.com/t/callback-and-facts/16430/7 "2014-10-20T21:38:58Z")

</div>

Just dumping a templated file to the local filesystem that contains what you want to see, and saying “go read output.txt” for the results, versus writing a custom callback plugin.

---

<div class="post-metadata">

**Author:** ![maxime.gerges31](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@maxime.gerges31](https://forum.ansible.com/u/maxime.gerges31)\
**Post date:** [November 5, 2014, 5:47pm UTC](https://forum.ansible.com/t/callback-and-facts/16430/8 "2014-11-05T17:47:20Z")

</div>

Thanks for the answer michael.

One more question regarding callback. We are currently looking to developp different callback in order to stock facts/vars in CSV & in database. This means we will create 2 callbacks.  
I’d like to know if there is any way to enable one/several callback from a playbook variable ? Does Ansible ship with such feature ?

An example will be that all callbacks will have their disabled attribute set to False, then the playbook could read a specific attribute and activate only specific callback based on this attribute.

Thanks for you support.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [November 7, 2014, 2:27pm UTC](https://forum.ansible.com/t/callback-and-facts/16430/9 "2014-11-07T14:27:11Z")

</div>

Pretty much what you said…

Your callback could decide to set a “self.active = True/False” in the constructor, and each method could return “if not self.active” to basically no-op.

---

<div class="post-metadata">

**Author:** ![maxime.gerges31](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@maxime.gerges31](https://forum.ansible.com/u/maxime.gerges31)\
**Post date:** [November 8, 2014, 9:22am UTC](https://forum.ansible.com/t/callback-and-facts/16430/10 "2014-11-08T09:22:06Z")

</div>

Thank fort the answer. My question was how to set the active variable to true/false based on a variable define in the yaml playbook?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [November 17, 2014, 9:46pm UTC](https://forum.ansible.com/t/callback-and-facts/16430/11 "2014-11-17T21:46:37Z")

</div>

The callback interface isn’t really going to see much of those variables directly, unless you jump through some hoops that may not be part of a stable API.

You might want to consider making it learn from an environment variable, depending on the use case.
