Callback functions and local facts.

205 views
Skip to first unread message

Marcin Prączko

unread,
May 13, 2014, 6:46:04 AM5/13/14
to ansible...@googlegroups.com
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: 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 (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


Marcin Prączko

unread,
May 13, 2014, 7:08:51 AM5/13/14
to ansible...@googlegroups.com
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

Marcin Prączko

unread,
May 13, 2014, 7:14:51 AM5/13/14
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
May 13, 2014, 11:52:29 AM5/13/14
to ansible...@googlegroups.com
Hi!

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

Thanks!


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f43d2c17-0899-42aa-8af9-a0d7ae6aa329%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages