Created
November 10, 2016 22:18
-
-
Save rmetzler/112fcfff90b674a63259c4114ef2884e to your computer and use it in GitHub Desktop.
Ansible Callback Plugin for SQLite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# original code stolen from https://stackoverflow.com/questions/37721314/ansible-putting-facts-in-sqlite-via-callback | |
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 `test` (`now` TEXT, `host` TEXT UNIQUE)') | |
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 test (now, host) VALUES(?,?);", | |
( | |
now, | |
facts.get('ansible_hostname', None) | |
)) | |
con.commit() | |
except: | |
pass | |
class CallbackModule(CallbackBase): | |
def runner_on_ok(self, host, res): | |
log(host, res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment