# Pass dynamic inventory as json not as file

**URL:** <https://forum.ansible.com/t/pass-dynamic-inventory-as-json-not-as-file/34510>\
**Category:** Ansible Project\
**Created:** [January 26, 2021, 10:16am UTC](https://forum.ansible.com/t/pass-dynamic-inventory-as-json-not-as-file/34510 "2021-01-26T10:16:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Filip\_Rucinski](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@Filip\_Rucinski](https://forum.ansible.com/u/Filip_Rucinski)\
**Post date:** [January 26, 2021, 10:16am UTC](https://forum.ansible.com/t/pass-dynamic-inventory-as-json-not-as-file/34510/1 "2021-01-26T10:16:49Z")

</div>

Hello

First question:

Is there any possibility to pass dynamic inventory as a pure json structure?

I have got a small app that is written in python and flask, that listen for some data (finally it is in json format) from other system. Based on this data I prepare \_meta structure like below:

‘hosts’: {

‘hosts’: [‘host001’, ‘host001’],

‘vars’: {

‘var01’: “value01”,  
}

},  
‘\_meta’: {

‘hostvars’: {

‘host001’: { ‘var001’ : “value02” },  
‘host002’: { ‘var002’: “value03” }  
}

}

Using this structure I’m creating [inventory script](https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#developing-inventory-scripts) that returns above code. Than based on [ansible Python API](https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html#python-api) documentation I run ansible task using some playbook and inventory script.

In above documentation you can find: “# create inventory, use path to host config file as source or hosts in a comma separated string inventory = InventoryManager(loader=loader, sources=sources)”

And now is my question: is there any easy way to pass above inventory not as a “/path/to/inventory\_script” but as a pure json structure?

I have read about [ansible-runner](https://github.com/ansible/ansible-runner) project which can be simpler implementation of creating and runnning ansible task (instead of pure API) but there I still have to pass “/path/to/file” as inventory.

Now I’m reading about writing my own plugin for ansible but maybe there is an easier way to do what I mentioned above. Maybe you have some other suggestions.

Best Regards  
Filip

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 27, 2021, 3:58pm UTC](https://forum.ansible.com/t/pass-dynamic-inventory-as-json-not-as-file/34510/2 "2021-01-27T15:58:37Z")

</div>

You can feed JSON to the YAML inventory plugin (since JSON is mostly  
a subset of YAML), but it must follow the structure it uses, which is  
different than the inventory script.

---

<div class="post-metadata">

**Author:** ![Filip\_Rucinski](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@Filip\_Rucinski](https://forum.ansible.com/u/Filip_Rucinski)\
**Post date:** [January 29, 2021, 1:03pm UTC](https://forum.ansible.com/t/pass-dynamic-inventory-as-json-not-as-file/34510/3 "2021-01-29T13:03:25Z")

</div>

Hi,

Thank you for your reply.

I can create this structure in YAML format, but what i really want to achieve is pass this structure as json/yaml variable. As I read this YAML plugin still require to save structure to file and than pass it to ansible API (if I’m wrong please correct me).  
To be more precise (using example from:[ansible Python API](https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html#python-api)) I would like to create inventory variable like below;

# create inventory, use path to host config file as source or hosts in a comma separated string

inventory = InventoryManager(loader=loader, sources=sources)

where source would be like:  
source = {

‘hosts’: {

‘hosts’: [‘host001’, ‘host001’],

‘vars’: {

‘var01’: “value01”,  
}

},  
‘\_meta’: {

‘hostvars’: {

‘host001’: { ‘var001’ : “value02” },  
‘host002’: { ‘var002’: “value03” }  
}

}  
}

Here I use json format, but it can be any YAML formatted variable.

Best regards  
Filip

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 29, 2021, 2:38pm UTC](https://forum.ansible.com/t/pass-dynamic-inventory-as-json-not-as-file/34510/4 "2021-01-29T14:38:44Z")

</div>

You need create an inventory plugin that handles it, as of now i don't  
know of any that exist that do what you want.

---

<div class="post-metadata">

**Author:** ![Filip\_Rucinski](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@Filip\_Rucinski](https://forum.ansible.com/u/Filip_Rucinski)\
**Post date:** [February 1, 2021, 6:08pm UTC](https://forum.ansible.com/t/pass-dynamic-inventory-as-json-not-as-file/34510/5 "2021-02-01T18:08:34Z")

</div>

Thanks for your answer. I wanted to avoid writing my own plugin, but if there is no other options I will try this.

Best Regards  
Filip
