Running a playbook from Python with Inventory in Database

Hi Team,

I need a help for running a playbook from a python program. Following is the description of the setup.

I have a configuration database that contains following information

  • Hosts

  • Variables for each host by role

  • Instance and role mapping

I am creating dynamic inventory JSON from database using a python (inventory.py). JSON sample output shown below.

{

“_meta”: {

“hostvars”: {

“dba”: {

“db-system” : {

“asm_diskgroup_diskDiscoveryString”: “/dev/mapper/*”,

“asm_diskgroup_disks”: “/dev/mapper/ocrvote1p1,/dev/mapper/ocrvote2p1,/dev/mapper/ocrvote3p1”,

“asmca_disk_db1”: “/dev/mapper/db1p1”,

“asmca_disk_db2”: “/dev/mapper/db2p1”,

“asmca_disk_fra”: “/dev/mapper/fra1”,

“asmca_disk_redo”: “/dev/mapper/redo1”,

}

}

}

},

“instances”: {

“hosts”: [

“dba”

]

}

}

I have the root playbook (dba.yml) for application installation as shown below that I run to start installation on dba.

- hosts: dba

gather_facts: False

roles:

- ssh

- ntp

- db-system

I execute following command to run the playbook.

ansible-playbook -i inventory.py dba.yml

I want to write a python program to run the playbook with the inventory that I have in the database.

I have following questions

Can I load the inventory object from database in python ??

If yes, how do I do that ??

If no, can I use JSON output to load the inventory object ?? and how do I do that ??

Any help would be greatly appreciated.

  • Hrishikesh