Callback functions and local facts.

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:

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

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

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

Hi!

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

Thanks!