# Get data to MySQL database with callback

**URL:** <https://forum.ansible.com/t/get-data-to-mysql-database-with-callback/12431>\
**Category:** Ansible Project\
**Created:** [February 28, 2013, 11:54am UTC](https://forum.ansible.com/t/get-data-to-mysql-database-with-callback/12431 "2013-02-28T11:54:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Acyl](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Acyl](https://forum.ansible.com/u/Acyl)\
**Post date:** [February 28, 2013, 11:54am UTC](https://forum.ansible.com/t/get-data-to-mysql-database-with-callback/12431/1 "2013-02-28T11:54:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 28, 2013, 1:47pm UTC](https://forum.ansible.com/t/get-data-to-mysql-database-with-callback/12431/2 "2013-02-28T13:47:22Z")

</div>

Ok so you already have:

&nbsp;&nbsp;&nbsp;&nbsp;facts = data.get('ansible\_facts', None)

Sounds like you just want an inner for loop like so:

&nbsp;&nbsp;&nbsp;&nbsp;packages = facts.get('packages',)  
&nbsp;&nbsp;&nbsp;&nbsp;for p in packages:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do database stuff here

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Acyl](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Acyl](https://forum.ansible.com/u/Acyl)\
**Post date:** [February 28, 2013, 4:25pm UTC](https://forum.ansible.com/t/get-data-to-mysql-database-with-callback/12431/3 "2013-02-28T16:25:27Z")

</div>

I modified my callback with added the new code lines. So, when I 'm launching my playbook I can’t enter into this loop. In verbose mode playbook, the result of my module dpkg\_custom was called without result of the function print().

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)  
packages = facts.get(‘packages’,)

try:

for p in packages:  
with connection:  
print(‘###################enter loop######################’)  
cursor = connection.cursor()  
sqlquery = “INSERT INTO packages\_available (package\_name, architecture, distribution, available\_version) VALUES('” + str(facts.get(p, None)) + “‘,’” + str(facts.get(‘ansible\_architecture’, None)) + “‘,’” + str(facts.get(‘ansible\_distribution’, None)) + “‘,’” + str(facts.get(‘available\_version’, None)) + “')”  
cursor.execute(sqlquery)  
connection.commit()

8\< ---------------------------------------------------------------------------------------------------------------------

If I forget anything please let me now.

Regards.

2013/2/28 Michael DeHaan \<[michael.dehaan@gmail.com](mailto:michael.dehaan@gmail.com)\>

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 28, 2013, 4:34pm UTC](https://forum.ansible.com/t/get-data-to-mysql-database-with-callback/12431/4 "2013-02-28T16:34:56Z")

</div>

> I modified my callback with added the new code lines. So, when I 'm  
> launching my playbook I can't enter into this loop. In verbose mode  
> playbook, the result of my module dpkg\_custom was called without result of  
> the function print().

I am sorry, it is out of the scope of this mailing list to help with  
non-ansible-specific python questions.

Wish I could help, but it seems the basic concept is already correct  
and it's just a matter of you hammering out the python code for what  
you want.

Sorry!

---

<div class="post-metadata">

**Author:** ![Acyl](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@Acyl](https://forum.ansible.com/u/Acyl)\
**Post date:** [February 28, 2013, 4:56pm UTC](https://forum.ansible.com/t/get-data-to-mysql-database-with-callback/12431/5 "2013-02-28T16:56:29Z")

</div>

Ok, thank you very much for your help!  
I’ll try to find a solution and then post it there (if I find it).  
Thanks again.  
Best regards

2013/2/28 Michael DeHaan \<[michael.dehaan@gmail.com](mailto:michael.dehaan@gmail.com)\>

---

<div class="post-metadata">

**Author:** ![Dusan\_Sovic](https://avatars.discourse-cdn.com/v4/letter/d/4491bb/32.png) [@Dusan\_Sovic](https://forum.ansible.com/u/Dusan_Sovic)\
**Post date:** [February 28, 2013, 6:43pm UTC](https://forum.ansible.com/t/get-data-to-mysql-database-with-callback/12431/6 "2013-02-28T18:43:39Z")

</div>

Hi Acyl,

I think the problem is that your SQL part is called only for ‘ **setup** ’ module and not for ‘ **dpkg\_custom** ’ module.

if invocation.get(‘module\_name’, None) != ‘setup’ :

Try to replace setup with dpkg\_custom but I’m not sure if facts from setup module will be available.

Best Regards,

Dusan

Dňa štvrtok, 28. februára 2013 17:56:29 UTC+1 Acyl napísal(-a):
