0
votes

I'm having a problem to filter a list by it's attribute using variable.

Let's say I have variable "network" register in ansible.

{
    "addresses": {
    "private_ext": [
        {
            "type": "fixed",
            "addr": "172.16.2.100"
        }
    ],
    "private_man": [
        {
            "type": "fixed",
            "addr": "172.16.1.100"
        },
        {
            "type": "floating",
            "addr": "10.90.80.10"
        }
        {
            "type": "floatingXYZ",
            "addr": "10.20.30.10"
        }
    ]
  }
}

and I want to use something like this

- debug: msg="{{ network.addresses | json_query(\"private_man[?type=='^${PATTERN}$'].addr\") }}"

or this

- debug: msg={{ network.addresses.private_man | selectattr("type", "match", "^${PATTERN}$") | map(attribute='addr') | join(',') }}

but the search pattern is stored in variable PATTERN with value "floating" and I want to find the exact value (not with value floatingXYZ).

Almost the same problem was described here, but without the variable part.

1

1 Answers

0
votes

Use string concatenation:

- debug:
    msg: "{{ network.addresses.private_man | selectattr('type', 'match', '^'+PATTERN+'$') | map(attribute='addr') | join(',') }}"
  vars:
    PATTERN: floating