dynamic inventory creation for complex json file

49 views
Skip to first unread message

Sakshi Rathore

unread,
Mar 24, 2021, 7:58:51 AM3/24/21
to Ansible Project
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.

Abhijeet Kasurde

unread,
Mar 24, 2021, 8:04:06 AM3/24/21
to ansible...@googlegroups.com
Please provide a sample complex json file.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/848dadea-ba06-4019-872c-7d07c5b7c845n%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Sakshi Rathore

unread,
Mar 24, 2021, 8:59:32 AM3/24/21
to ansible...@googlegroups.com
please find attached json file.

cluster.json

Abhijeet Kasurde

unread,
Mar 24, 2021, 9:07:16 AM3/24/21
to ansible...@googlegroups.com
What have you tried so far and what is required output?



--
Thanks,
Abhijeet Kasurde

Sakshi Rathore

unread,
Mar 24, 2021, 9:21:09 AM3/24/21
to ansible...@googlegroups.com
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"

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/IOyUATc2x1g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAFwWkHrfe%2B24qwQZ5KqdNdehF4KNPZKnT_erfhWhWR_V6rG25g%40mail.gmail.com.

Sakshi Rathore

unread,
Mar 25, 2021, 6:47:42 AM3/25/21
to ansible...@googlegroups.com
Is there any other approach to achieve this?
Reply all
Reply to author
Forward
0 new messages