Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: filter a list by its attributes

Tags:

ansible

jinja2

I have variable named "network" registered 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"
            }
        ]
    }
}

Is it possible to get the IP address ("addr") with type="floating" doing something like this?

- debug: var={{ network.addresses.private_man | filter type="fixed" | get "addr" }}

I know the syntax is wrong but you get the idea.

like image 307
Guido Avatar asked Aug 08 '15 15:08

Guido


2 Answers

To filter a list of dicts you can use the selectattr filter together with the equalto test:

network.addresses.private_man | selectattr("type", "equalto", "fixed")

The above requires Jinja2 v2.8 or later (regardless of Ansible version).


Ansible also has the tests match and search, which take regular expressions:

match will require a complete match in the string, while search will require a match inside of the string.

network.addresses.private_man | selectattr("type", "match", "^fixed$")

To reduce the list of dicts to a list of strings, so you only get a list of the addr fields, you can use the map filter:

... | map(attribute='addr') | list

Or if you want a comma separated string:

... | map(attribute='addr') | join(',')

Combined, it would look like this.

- debug: msg={{ network.addresses.private_man | selectattr("type", "equalto", "fixed") | map(attribute='addr') | join(',') }}
like image 171
udondan Avatar answered Dec 22 '22 22:12

udondan


I've submitted a pull request (available in Ansible 2.2+) that will make this kinds of situations easier by adding jmespath query support on Ansible. In your case it would work like:

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

would return:

ok: [localhost] => {
    "msg": [
        "172.16.1.100"
    ]
}
like image 30
Filipe Avatar answered Dec 23 '22 00:12

Filipe