Hi,
I’m kinda new to Ansible, love the program so far.
I want two facts, hostname and serial number,in a Sqlite database. I’m trying to achieve it with the callback plugin, which is written in Python.
I came across this website and applied his Python code on my machine.
http://jpmens.net/2012/09/11/watching-ansible-at-work-callbacks/
The problem is that the script made by jpmens is for Ansible 1. I’m working with Ansible 2.0.2.0
The code I have for inv.py
import os
import time
import sqlite3
dbname = '/etc/ansible/test.db'
TIME_FORMAT='%Y-%m-%d %H:%M:%S'
try:
con = sqlite3.connect(test1)
cur = con.cursor()
except:
pass
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 test2 (now, host, serial) VALUES(?,?,?);",
(
now,
facts.get('ansible_hostname', None),
facts.get('ansible_product_serial', None)
))
con.commit()
except:
pass
class CallbackModule(object):
def runner_on_ok(self, host, serial):
log(host, serial)
I’m getting the following error message:
" [WARNING]: Failure when attempting to use callback plugin (</etc/ansible/callback_plugin/inv.CallbackModule object at 0x16c5a90>):
‘TaskResult’ object has no attribute ‘get’"
I don’t understand Python quite well. Can anybody pin-point me to my mistake?
Or is there a more simple method to put the values in a sqlite database?
Thanks