Match filter missing in Ansible version 2.9

2,630 views
Skip to first unread message

Devesh singh

unread,
Feb 5, 2020, 9:23:08 AM2/5/20
to Ansible Project
Hello All,

Anybody can  help for the below error. My system was patched recently and ansible version has been upgraded to 2.9.

After that my playbook started to throw  below error.:

=========================
fatal: [vlac475.netpost]: FAILED! => {"msg": "The conditional check 'ansible_default_ipv4.network|match( bpost_mgmt_network )' failed. The error was: template error while templating string: no filter named 'match'. String: {% if ansible_default_ipv4.network|match( bpost_mgmt_network ) %} True {% else %} False {% endif %}\n\nThe error appears to be in '/root/playbooks/roles/bpost_facts/tasks/main.yml': line 19, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# Server has multiple interface but primary is mgmt\n- name: set fact bpost_has_backup_nic to 'false' if no interface matching bpost mgmt subnet\n  ^ here\n"}

=========================
Task syntax:

# Server has multiple nic. primary is neither mgmt nor DMZ. Configured IP match backup network.
- name: set fact Detrio_has_backup_nic to 'true'
  set_fact:
    Detrio_has_backup_nic: true
  when:
    - ansible_{{ Detrio_backup_nic}}.ipv4.network|match( Detrio_backup_network )
    - _has_backup_nic is undefined
    - Detrio_backup_nic is defined

If any body can help to modify the syntax or what the new filter  for match in 2.9 will be appreciable.

Thanks & Regards,
Devesh Kumar SIngh

Matt Martz

unread,
Feb 5, 2020, 9:32:12 AM2/5/20
to ansible...@googlegroups.com
`match` is not a filter, it is what is called a `test`.  Ansible 2.5 deprecated the syntax to use a `test` like a `filter`. See https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_2.5.html#jinja-tests-used-as-filters

This should have been generating a deprecation warning since the 2.5 release.  In any case, the most simple fix is just to replace `|match` with `is match`.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d93b3cf3-f8f4-4168-b606-efdd4f3d4e90%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Vladimir Botka

unread,
Feb 5, 2020, 10:21:02 AM2/5/20
to Devesh singh, ansible...@googlegroups.com
On Wed, 5 Feb 2020 06:23:08 -0800 (PST)
Devesh singh <dev....@gmail.com> wrote:

> error was: template error while templating string: no filter named 'match'.
>
> when:
> - ansible_{{ Detrio_backup_nic }}.ipv4.network|*match*(Detrio_backup_network)

There are 2 problems

1) "match" is a test. See "Testing strings"
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings

2) Use "lookup" and "vars" plugin to get value of dynamically created
variable's name. See "vars – Lookup templated value of variables"
https://docs.ansible.com/ansible/latest/plugins/lookup/vars.html#vars-lookup-templated-value-of-variables

For example

- set_fact:
Detrio_has_backup_nic: true
vars:
my_nic: "{{ lookup('vars', 'ansible_' ~ Detrio_backup_nic) }}
when:
- my_nic.ipv4.network is match(Detrio_backup_network)
- _has_backup_nic is undefined
- Detrio_backup_nic is defined

HTH,

-vlado
Reply all
Reply to author
Forward
0 new messages