Reference: Jinja

Check if an object has the same value as another object.

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:

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