# Callback functions and local facts.

**URL:** <https://forum.ansible.com/t/callback-functions-and-local-facts/15220>\
**Category:** Ansible Project\
**Created:** [May 13, 2014, 10:46am UTC](https://forum.ansible.com/t/callback-functions-and-local-facts/15220 "2014-05-13T10:46:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [May 13, 2014, 10:46am UTC](https://forum.ansible.com/t/callback-functions-and-local-facts/15220/1 "2014-05-13T10:46:04Z")

</div>

Hello,

I am trying to write my own callback function which logs information which I want to database, especially informations from local facts.  
We have localfacts in json mode and I can easly get access to them from Ansbile it self, however everything what I’ve tried in terms access them from callback function failed.

Details:

- My callback function script skeleton is based on this callback script: [Jan-Piet Mens :: Watching Ansible at work: Callbacks](http://jpmens.net/2012/09/11/watching-ansible-at-work-callbacks/)
- I’ve search ansible group and found something similar - however didn’t help solve my problem ([Redirecting to Google Groups](https://groups.google.com/forum/#!topic/ansible-project/3GvdIIWCdSI))
- Looking in source (ansible / plugins / callbacks /) is not so helpfull for me as well.

So question, how I can get access to fact stored in local facts in callback functions:

1. These is a file on remote host:  
/etc/ansible/facts.d/apache.fact

2. Return from this file (json format):  
{  
“name”: “apache”,  
“status”: “Installed”,  
“version”: “1.0.8”  
}

3. Return this fact in ansible-playbook  
“msg”: “work - variable: {u’apache’: {u’status’: u’Installed’, u’version’: u’1.0.8’, u’name’: u’apache’}}”

4. Access to it from my callback function (Based on this callback which I mentioned above).  
def log(host, data):  
if type(data) == dict:  
invocation = data.pop(‘invocation’, None)  
if invocation.get(‘module\_name’, None) != ‘setup’:  
return

facts = data.get(‘ansible\_facts’, None)

now = time.strftime(TIME\_FORMAT, time.localtime())

try:

# `host` is a unique index

cur.execute(“REPLACE INTO inventory (now, host, arch, dist, distvers, sys,kernel) VALUES(?,?,?,?,?,?,?);”,  
(  
now,  
facts.get(‘ansible\_hostname’, None),  
… # !!! Works well  
))  
con.commit()

cur.execute(“REPLACE INTO modules (host, name, status, version) VALUES(?,?,?,?);”,  
(  
**# !!! - Not working !!!**  
facts.get(‘ansible\_hostname’, None),  
facts.get(‘ansible\_local.apache.name’, None),  
facts.get(‘ansible\_local.apache.status’, None),  
facts.get(‘ansible\_local.apache.version’, None),  
))  
con.commit()

**Summary:**  
Any reference to nasible\_local.apache.name (etc) returns NULL in nasible callback function.

Please advice what I have to do, as I understand ansible\_local is DICT in python, do I have to run some special functions to get access for facts in format: Main-fact-name.some-subname.some… ???

Best regards,  
Marcin Praczko

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [May 13, 2014, 11:08am UTC](https://forum.ansible.com/t/callback-functions-and-local-facts/15220/2 "2014-05-13T11:08:51Z")

</div>

Hi,

I think I sorted this our, seems be working now as expected:

This is a snipped which must be used for local facts:

ansible\_local = facts.get(‘ansible\_local’, None)  
cur.execute(“REPLACE INTO modules (host, name, status, version) VALUES(?,?,?,?);”,  
(  
facts.get(‘ansible\_hostname’, None),  
ansible\_local[“apache”][“name”],  
ansible\_local[“apache”][“status”],  
ansible\_local[“apache”][“version”]  
))

Best regards,  
Marcin Praczko

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [May 13, 2014, 11:14am UTC](https://forum.ansible.com/t/callback-functions-and-local-facts/15220/3 "2014-05-13T11:14:51Z")

</div>

And still have question.

Is there any way to use facts\_get with DICT?

For example:  
facts.get('ansible\_local[“apache”]) ???

or

facts.get('ansible\_interfaces[“eth”].some\_argument) ???

Best regards,  
Marcin Praczko

---

<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:** [May 13, 2014, 3:52pm UTC](https://forum.ansible.com/t/callback-functions-and-local-facts/15220/4 "2014-05-13T15:52:29Z")

</div>

Hi!

This is really a question for ansible-devel, since this is about developing code that uses the API. Can you post there?

Thanks!
