Reference: Ansible

match  requires a complete match in the string, while search  only requires matching a subset of the string.

Example

As per the selectattr( )  page, the Jinja equalto( )  Test, as well as the Ansible match( )  and search( )  Tests all work in a similar fashion.

Using this dictionary:

- hosts: localhost
  connection: local
  gather_facts: no
  vars:
    network:
      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

We can obtain the “floating” value using any one of following filters:

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

The above lines result in the following output:

```yaml TASK [debug] *********************** ok: [localhost] => { “msg”: [ “10.90.80.10” ] }

TASK [debug] *********************** ok: [localhost] => { “msg”: [ “10.90.80.10” ] }

TASK [debug] *********************** ok: [localhost] => { “msg”: [ “10.90.80.10” ] }```


As always, if you have any questions or have a topic that you would like me to discuss, please feel free to post a comment at the bottom of this blog entry, e-mail at will@oznetnerd.com, or drop me a message on Reddit (OzNetNerd).

Note: The opinions expressed in this blog are my own and not those of my employer.

Leave a comment