dynamic inventory creation for complex json file

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.

Please provide a sample complex json file.

please find attached json file.

(attachments)

cluster.json (191 KB)

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

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”

Is there any other approach to achieve this?