Muller
Muller

Reputation: 57

ansible json_query merge 2 results

Im have a result from a Netbox server:

I want to find the next awaible vlan with the state Deprecated.

- name: Create Fact 
  set_fact:
    netbox_json_filter:  "{{ netbox_json.json.results | json_query('[*].vid') }}"

- debug:
    var: netbox_json_filter


- name: Create Fact2
  set_fact:
    netbox_json_filter2:  "{{ netbox_json.json.results | json_query('[*].status.value') }}" 

- debug:
    var: netbox_json_filter2

Is it possible to combine the 2 result or use a new query ?

{ "count": 2, "next": null, "previous": null, "results": [ { "id": 4, "url": "http://192.168.209.230:8000/api/ipam/vlans/4/", "display": "Test VLAN (400)", "site": null, "group": null, "vid": 400, "name": "Test VLAN", "tenant": null, "status": { "value": "deprecated", "label": "Deprecated" }, "role": null, "description": "", "tags": [], "display_name": "Test VLAN (400)", "custom_fields": { "Jack": null }, "created": "2021-06-19", "last_updated": "2021-06-19T17:51:14.262534Z", "prefix_count": 0 }, { "id": 5, "url": "http://192.168.209.230:8000/api/ipam/vlans/5/", "display": "Test VLAN (401)", "site": null, "group": null, "vid": 401, "name": "Test VLAN", "tenant": null, "status": { "value": "reserved", "label": "Reserved" }, "role": null, "description": "", "tags": [], "display_name": "Test VLAN (401)", "custom_fields": { "Jack": null }, "created": "2021-06-19", "last_updated": "2021-06-19T17:59:40.343774Z", "prefix_count": 0 } ] }

Upvotes: 1

Views: 291

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 67959

Given the simplified data

    resutls:
      - vid: 1
        status:
          value: A
      - vid: 2
        status:
          value: B
      - vid: 3
        status:
          value: C

It's possible to create lists. See MultiSelect List, e.g.

    - set_fact:
        filter: "{{ resutls|json_query('[].[vid, status.value]') }}"

gives

  filter:
  - - 1
    - A
  - - 2
    - B
  - - 3
    - C

The next option is to create dictionaries. See MultiSelect Hash, e.g.

    - set_fact:
        filter: "{{ resutls|json_query('[].{vid: vid, value: status.value}') }}"

gives

  filter:
  - value: A
    vid: 1
  - value: B
    vid: 2
  - value: C
    vid: 3

Upvotes: 2

Related Questions