# Trying to put output in Sqlite3

**URL:** <https://forum.ansible.com/t/trying-to-put-output-in-sqlite3/21685>\
**Category:** Ansible Project\
**Created:** [June 7, 2016, 10:44am UTC](https://forum.ansible.com/t/trying-to-put-output-in-sqlite3/21685 "2016-06-07T10:44:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Csuka](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@Kevin\_Csuka](https://forum.ansible.com/u/Kevin_Csuka)\
**Post date:** [June 7, 2016, 10:44am UTC](https://forum.ansible.com/t/trying-to-put-output-in-sqlite3/21685/1 "2016-06-07T10:44:13Z")

</div>

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/](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

```auto
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

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@Kevin\_Csuka](https://forum.ansible.com/u/Kevin_Csuka)\
**Post date:** [June 9, 2016, 7:06am UTC](https://forum.ansible.com/t/trying-to-put-output-in-sqlite3/21685/2 "2016-06-09T07:06:33Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![Kevin\_Csuka1](https://avatars.discourse-cdn.com/v4/letter/k/ecae2f/32.png) [@Kevin\_Csuka1](https://forum.ansible.com/u/Kevin_Csuka1)\
**Post date:** [June 9, 2016, 11:37am UTC](https://forum.ansible.com/t/trying-to-put-output-in-sqlite3/21685/3 "2016-06-09T11:37:41Z")

</div>

> Found the answer:

from ansible.plugins.callback import CallbackBase  
import os  
import time  
import sqlite3

dbname = ‘./test.db’  
TIME\_FORMAT=‘%Y-%m-%d %H:%M:%S’

try:  
con = sqlite3.connect(dbname)  
cur = con.cursor()  
cur.execute(‘CREATE TABLE `test3` (`now` TEXT, `host` TEXT UNIQUE, `serial` TEXT)’)  
con.commit()  
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 test3 (now, host, serial) VALUES(?,?,?);”,  
(  
now,  
facts.get(‘ansible\_hostname’, None),  
facts.get(‘ansible\_product\_serial’, None)  
))  
con.commit()  
except:  
pass

Can close.

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [June 9, 2016, 10:58pm UTC](https://forum.ansible.com/t/trying-to-put-output-in-sqlite3/21685/4 "2016-06-09T22:58:00Z")

</div>

Callbacks seem a bit overkill for something like this. I would just get the stuff yuu need into variables and then use “shell” or similar to post the data to your datastore, but glad you got it working.
