-2

What the syntax would be to pull the "members" value out of this JSON where the name value is "PROD_poolgroup".

I think it should be [?config.name == "PROD_poolgroup"].config.members I've tried other variations without success also.

"all_members": [
{
 "config": {
    "_last_modified": "1594434546441212",
    "cloud_ref": "https://192.168.86.20/api/cloud/cloud-5a7a41cb-156e-43f4-ad28-031b27dac813",
    "implicit_priority_labels": false,
    "members": [{
        "pool_ref": "https://192.168.86.20/api/pool/pool-cb436bba-000a-4d72-8ff5-7d6cdf1ac2fa",
        "ratio": 1
      }],
    "min_servers": 0,
    "name": "PROD_poolgroup",
    "tenant_ref": "https://192.168.86.20/api/tenant/admin",
    "url": "https://192.168.86.20/api/poolgroup/poolgroup-e08933c0-d142-4787-9990-45f94dfc6b89",
    "uuid": "poolgroup-e08933c0-d142-4787-9990-45f94dfc6b89"
  },
  "uuid": "poolgroup-e08933c0-d142-4787-9990-45f94dfc6b89"},
{
 "config": {
   "_last_modified": "1594404797173635",
   "cloud_ref": "https://192.168.86.20/api/cloud/cloud-5a7a41cb-156e-43f4-ad28-031b27dac813",
   "implicit_priority_labels": false,
   "members": [{
       "pool_ref": "https://192.168.86.20/api/pool/pool-f98ed65c-00b2-4638-83df-8b89df79deec",
       "ratio": 1
              }],
   "min_servers": 0,
   "name": "QA_poolgroup",
   "tenant_ref": "https://192.168.86.20/api/tenant/admin",
   "url": "https://192.168.86.20/api/poolgroup/poolgroup-0b19b515-9ae6-41ef-9111-00e61470e615",
   "uuid": "poolgroup-0b19b515-9ae6-41ef-9111-00e61470e615"},
   "uuid": "poolgroup-0b19b515-9ae6-41ef-9111-00e61470e615"
    }
    ]
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
ScottO
  • 27
  • 1

1 Answers1

1

Use either single quote or backquote for using literal expression to compare value of name. Below are two working examples:

Using single quote:

- debug:
    msg: "{{ all_members | json_query(query) | list }}"
  vars:
    query: "[?config.name == 'PROD_poolgroup'].config.members"

Using literal expression:

- debug:
    msg: "{{ all_members | json_query(query) | list }}"
  vars:
    query: '[?config.name == `"PROD_poolgroup"`].config.members'
Moon
  • 2,837
  • 1
  • 20
  • 34
  • I've attempted this and the ansible playbook errors with fatal: [localhost]: FAILED! => { "msg": "Error in jmespath.search in json_query filter plugin:\n'instancemethod' object is not iterable" } Seems like all_members might not be rendered correctly. all_members is the result of a lookup plugin. I'll start looking there. – ScottO Jul 12 '20 at 14:51