Trying to put output in Sqlite3

75 views
Skip to first unread message

Kevin Csuka

unread,
Jun 7, 2016, 9:05:36 AM6/7/16
to Ansible Project
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.

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
[code]
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)
[/code]

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



Kevin Csuka

unread,
Jun 9, 2016, 3:06:33 AM6/9/16
to Ansible Project
Anyone?

Op dinsdag 7 juni 2016 15:05:36 UTC+2 schreef Kevin Csuka:

Kevin Csuka

unread,
Jun 9, 2016, 1:36:44 PM6/9/16
to Ansible Project
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.

Trond Hindenes

unread,
Jun 9, 2016, 6:58:00 PM6/9/16
to Ansible Project
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.
Reply all
Reply to author
Forward
0 new messages