0

I have the below json that has a range. I am trying to get values from json for a specific entry from the range to be used as an ansible variable .

for instance i would like to get the folder value of of server002 from below json to be used as an ansible variable using JSON Query Filter. Please help.

[
{"hosts": "server001:060",
"values": {
    "folder": "/my_folder1/",
    "pool": "pool1",
    "dsname": "DS1",
    "network": "nw_1"
}},
{"hosts": "server061:080",
"values": {
    "folder": "/my_folder2/",
    "pool": "pool2",
    "dsname": "DS2",
    "network": "nw_2"
}}
]
Vijay Vat
  • 317
  • 1
  • 3
  • 9

2 Answers2

0

I don't see a server002 in your example, but below is an example search for the second server in your list. (Change 'json_file_path' to the path where your JSON file is located.)

- name: Set search facts
  set_fact:
     host_to_find: 'server061:080'
     json_file_path: <path to json file>
- name: Get data for host
  vars:
    hosts_data: "{{ lookup('file', json_file_path) | from_json }}"
  set_fact:
    host: "{{ hosts_data | selectattr('hosts', 'match', host_to_find) | list | first }}"
- name: Display value of folder var
  debug:
    var: host['values']['folder']
clockworknet
  • 1,838
  • 1
  • 12
  • 16
  • Thank you for answering. server002 falls inside the first range server001:060. I was hoping to somehow find a way to find server002 from this range and get the values. Also I am getting the error "msg": "The task includes an option with an undefined variable. The error was: No first item, sequence was empty. – Vijay Vat Dec 04 '18 at 03:11
0

Below is a working play which should satisfy your use-case:

---
- name: JSON range extraction
  hosts: 127.0.0.1
  connection: local
  gather_facts: no
  tasks:
    - name: Set facts for search
      set_fact:
        host_to_find: '002'
        hosts_json_string: '[{"hosts":"server001:060","values":{"folder":"/my_folder1/","pool":"pool1","dsname":"DS1","network":"nw_1"}},{"hosts":"server061:080","values":{"folder":"/my_folder2/","pool":"pool2","dsname":"DS2","network":"nw_2"}}]'

    - name: Convert json string to facts
      set_fact:
        hosts_data: "{{ hosts_json_string | from_json }}"

    - name: Sort json by hosts and replace the value of hosts to make range extraction easier
      set_fact:
        sorted_hosts: "{{hosts_data|sort(attribute='hosts')|regex_replace('(server(\\d+):(\\d+))','\\2-\\3')}}"

    - name: Find index of host_to_find in sorted_hosts and set_fact
      vars:
        hosts_range: "{{sorted_hosts| json_query('[*].hosts')}}"
      set_fact:
        host_index: "{% for range in hosts_range %}{% set range_split = range.split('-') %}{% if ((host_to_find|int >= range_split[0]|int) and (host_to_find|int <= range_split[1]|int)) %}{{ loop.index0 }}{% endif %}{% endfor %}"

    - name: Get the folder location
      set_fact:
        folder_location: "{{ sorted_hosts[host_index|int].get('values').folder }}"
      when: not host_index == ""
...
Anant Naugai
  • 530
  • 4
  • 14