# dynamic inventory creation for complex json file

**URL:** <https://forum.ansible.com/t/dynamic-inventory-creation-for-complex-json-file/34843>\
**Category:** Ansible Project\
**Created:** [March 24, 2021, 11:58am UTC](https://forum.ansible.com/t/dynamic-inventory-creation-for-complex-json-file/34843 "2021-03-24T11:58:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sakshi\_Rathore](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@Sakshi\_Rathore](https://forum.ansible.com/u/Sakshi_Rathore)\
**Post date:** [March 24, 2021, 11:58am UTC](https://forum.ansible.com/t/dynamic-inventory-creation-for-complex-json-file/34843/1 "2021-03-24T11:58:51Z")

</div>

hi all,

i am having a complex json file with nested json structure. how can i create dynamic inventory using json file into ansible.

i have tried many tutorials but due to compley json structure i am not getting it completely.

please advise.

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [March 24, 2021, 12:03pm UTC](https://forum.ansible.com/t/dynamic-inventory-creation-for-complex-json-file/34843/2 "2021-03-24T12:03:36Z")

</div>

Please provide a sample complex json file.

---

<div class="post-metadata">

**Author:** ![Sakshi\_Rathore](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@Sakshi\_Rathore](https://forum.ansible.com/u/Sakshi_Rathore)\
**Post date:** [March 24, 2021, 12:59pm UTC](https://forum.ansible.com/t/dynamic-inventory-creation-for-complex-json-file/34843/3 "2021-03-24T12:59:07Z")

</div>

please find attached json file.

> **(attachments)**
>
> [cluster.json](https://forum.ansible.com/uploads/short-url/hzwut6GdaExAYj0YfF3mAJZ3jdf.json) (191 KB)

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [March 24, 2021, 1:06pm UTC](https://forum.ansible.com/t/dynamic-inventory-creation-for-complex-json-file/34843/4 "2021-03-24T13:06:45Z")

</div>

What have you tried so far and what is required output?

---

<div class="post-metadata">

**Author:** ![Sakshi\_Rathore](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@Sakshi\_Rathore](https://forum.ansible.com/u/Sakshi_Rathore)\
**Post date:** [March 24, 2021, 1:20pm UTC](https://forum.ansible.com/t/dynamic-inventory-creation-for-complex-json-file/34843/5 "2021-03-24T13:20:47Z")

</div>

i have written a python script to list all the ips for master nodes and worker nodes with respect to cluster name.  
but i am not sure how to do it for nested ip addresses.

i have also tried converting json to csv and then fetch the ip addresses from this using this approach i am getting csv file but not getting the required data (ip addresses for master and worker nodes ) in dynamic inventory.

on other hand i am also trying using jsonquery as follows but it gives me 2 set of lists and from this my requirement is to get only one list for master and worker nodes:

- name: get auth-token from ccp  
uri:  
url: ‘{{ ccp\_url }}/system/login’  
return\_content: yes  
body:  
username: ‘{{ ccp\_user }}’  
password: ‘{{ ccp\_pass }}’  
body\_format: form-urlencoded  
validate\_certs: no  
method: POST  
headers:  
Content-Type: application/x-www-form-urlencoded  
register: token

- name: printtoken  
debug:  
msg: “{{ token.x\_auth\_token }}”

- name: create list of all CCP-Nodes  
uri:  
url: ‘{{ ccp\_url }}/clusters’  
validate\_certs: no  
method: GET  
dest: …/cluster.txt  
headers:  
Content-Type: application/x-www-form-urlencoded  
x-auth-token: ‘{{ token.x\_auth\_token }}’  
register: jsoncontent

- name: master ip list from json content  
debug: msg=“{{ input | json\_query(jmesquery) }}”  
#debug: msg=“{{ input1 | json\_query(jmesquery1) }}”  
#copy: content=“{{ input | json\_query(jmesquery) || input1 | json\_query(jmesquery1) }}” dest=…/ip\_address.txt  
vars:  
input : “{{ lookup(‘template’, ‘…/cluster.txt’) }}”  
target: “{{ input | json\_query(jmesquery) }}”  
jmesquery: “[_].master\_group[].nodes[].private\_ip"  
input1 : “{{ lookup(‘template’, ‘…/cluster.txt’) }}”  
target1: “{{ input1 | json\_query(jmesquery1) }}”  
jmesquery1: "[_].node\_groups.nodes.private\_ip”

- name: worker ip list from json content  
debug: msg=“{{ input1 | json\_query(jmesquery1) }}”  
#debug: msg=“{{ input1 | json\_query (” [_].node\_groups[].nodes[].private\_ip || [_].master\_group.nodes.private\_ip “) }}”  
#copy: content=“{{ input1 | json\_query(jmesquery1) }}” dest=…/ip\_address1.txt  
vars:  
input1 : “{{ lookup(‘template’, ‘…/cluster.txt’) }}”  
target1: "{{ input1 | json\_query(jmesquery1) }}

jmesquery1: “[\*].node\_groups.nodes.private\_ip”

---

<div class="post-metadata">

**Author:** ![Sakshi\_Rathore](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@Sakshi\_Rathore](https://forum.ansible.com/u/Sakshi_Rathore)\
**Post date:** [March 25, 2021, 10:47am UTC](https://forum.ansible.com/t/dynamic-inventory-creation-for-complex-json-file/34843/6 "2021-03-25T10:47:14Z")

</div>

Is there any other approach to achieve this?
