# Running a playbook from Python with Inventory in Database

**URL:** <https://forum.ansible.com/t/running-a-playbook-from-python-with-inventory-in-database/22815>\
**Category:** Ansible Project\
**Created:** [October 18, 2016, 5:38am UTC](https://forum.ansible.com/t/running-a-playbook-from-python-with-inventory-in-database/22815 "2016-10-18T05:38:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hrishikesh\_Trivedi](https://avatars.discourse-cdn.com/v4/letter/h/edb3f5/32.png) [@Hrishikesh\_Trivedi](https://forum.ansible.com/u/Hrishikesh_Trivedi)\
**Post date:** [October 18, 2016, 5:38am UTC](https://forum.ansible.com/t/running-a-playbook-from-python-with-inventory-in-database/22815/1 "2016-10-18T05:38:28Z")

</div>

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
