Hello,
I recently developed a module called « dpkg_custom » which return JSON result.
This module allows me to list, on a particular node, the installed packages, the version which is currently installed and the available version on the Debian repository.
Here is a (shortened) result of this module:
8< ---------------------------------------------------------------------------------------------------------------------
$ ansible 127.0.0.1 -u root -m dpkg_custom
127.0.0.1 | success >> {
“ansible_facts”: {
“packages”: [
{
“acpi”: {
“available_version”: “”,
“distribution”: “squeeze”,
“installed_version”: “1.5-2”,
“status”: “uptodate”
}
},
{
“acpi-support-base”: {
“available_version”: “”,
“distribution”: “squeeze”,
“installed_version”: “0.137-5”,
“status”: “uptodate”
}
},
…
]
},
“changed”: “False”
}
8< ---------------------------------------------------------------------------------------------------------------------
I also have a MySQL database that I want to be filled by my callback. Here are the first lines of my callback:
8< ---------------------------------------------------------------------------------------------------------------------
import os
import time
import MySQLdb as mdb
import sys
import json
dbname = ‘CMDB’
try:
connection = mdb.connect(‘localhost’, ‘ansible’, ‘***********’, dbname)
except:
pass
def log(host, data):
if type(data) == dict:
invocation = data.pop(‘invocation’, None)
if invocation is None:
return
if invocation.get(‘module_name’, None) != ‘setup’ :
return
facts = data.get(‘ansible_facts’, None)
try:
with connection:
cursor = connection.cursor()
sqlquery = “INSERT INTO packages_available (package_name, architecture, distribution, available_version) VALUES('” + str(facts.get(‘packages’, None)) + “‘,’” + str(facts.get(‘ansible_architecture’, None)) + “‘,’” + str(facts.get(‘ansible_distribution’, None)) + “‘,’” + str(facts.get(‘available_version’, None)) + “')”
cursor.execute(sqlquery)
connection.commit()
sqlquery = “SELECT id FROM packages_available WHERE package_name='” + str(facts.get(‘package’, None)) + “’ AND architecture='” + str(facts.get(‘ansible_architecture’, None)) + “’ AND distribution='” + str(facts.get(‘distribution’, None)) + “'”
cursor.execute(sqlquery)
connection.commit()
…
8< ---------------------------------------------------------------------------------------------------------------------
I’m a noob in Python development (oops ) so please by kind
I’m looking for a method to get all my facts called “packages” in this callback to fill my database.
I think that it is possible with a “foreach” loop but I don’t really how to implement it.
Otherwise, in this callback I’m using both facts of the “setup” module and of my “dpkg_custom”. Is it possible?
Thank for your answers
Regards