When Or statement

34 views
Skip to first unread message

colli...@gmail.com

unread,
Nov 25, 2020, 10:44:42 AM11/25/20
to Ansible Project
In an ansible play, is "or" not supported with conditional statements? I have a task that is searching a variable for 2 values. The when statements work alone, but with "or" it runs but doesn't do what I am expecting. Here is the task, is my syntax wrong or is it just not supported? - 


    - name: Apply policy to the interfaces
      cisco.ios.ios_config:
        parents: interface {{ item.key }}
        lines:
          - device-tracking attach-policy IPDT_POLICY
      loop: "{{ netbrain_interface_attributes|dict2items }}"
      when: >
        item.key is not search("Port")
        or item.key is not search("Vlan")


Stefan Hornburg (Racke)

unread,
Nov 25, 2020, 11:08:13 AM11/25/20
to ansible...@googlegroups.com
Maybe you need ( ) around both conditions, but you can also use (untested)

item.key is not regex("(Port|Vlan)")

Regards
Racke

>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/a0649fd5-6757-4e91-9424-904629aad1fdn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/a0649fd5-6757-4e91-9424-904629aad1fdn%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

J C

unread,
Nov 25, 2020, 12:43:04 PM11/25/20
to ansible...@googlegroups.com
I apologize I did miss a line in my task when I copied.  So I used the (Port|Vlan).  Which again works as a single statement.  Also tried putting the statements in ()

    - name: Apply policy to the interfaces
      cisco.ios.ios_config:
        parents: interface {{ item.key }}
        lines:
          - device-tracking attach-policy IPDT_POLICY
      loop: "{{ netbrain_interface_attributes|dict2items }}"
      when: >
        item.key is not regex("(Port|Vlan)")
        or item.key not in po_ints

    - name: Apply policy to the interfaces
      cisco.ios.ios_config:
        parents: interface {{ item.key }}
        lines:
          - device-tracking attach-policy IPDT_POLICY
      loop: "{{ netbrain_interface_attributes|dict2items }}"
      when: >
        (item.key is not regex("(Port|Vlan)"))
        or (item.key not in po_ints)

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/81625ee8-3b31-a6b7-dc30-50b4db9b5d72%40linuxia.de.

J C

unread,
Nov 30, 2020, 10:15:02 AM11/30/20
to ansible...@googlegroups.com
Hello, 

Just thought I would uypdate.  I was not able to find the correct syntax for an or in a when statement.   What I end up doing, and I am sure there is a better way to do it, was create separate lists then adding them together.

    - name: Create a list without Gigabit interfaces
      set_fact:
        exclude1: "{{ ex | reject('search', 'Giga') | list }}"
    - name: Create a list of GigabitEthernet0/0
      set_fact:
        exclude2: "{{ ex | select('search', 'GigabitEthernet0/0') | list }}"

    - name: Gather the Port Channel Summary
      ansible.netcommon.cli_parse:
        command: "show etherchannel summary"
        parser:
          name: ansible.netcommon.pyats
        set_fact: eth_sum

    - name: Get the PO Channel Interfaces
      set_fact:
        po_ints: "{{ eth_sum.interfaces|json_query(query)|
                    flatten }}"
        cacheable: yes
      vars:
        query: '*.port_channel.port_channel_member_intfs'

    - name: Create one list to be used in a future play
      set_fact:
        exclude_interfaces: "{{ exclude1 + exclude2 + po_ints }}"
        cacheable: yes

James Lagermann

unread,
Dec 1, 2020, 11:17:40 AM12/1/20
to Ansible Project
Have you tried to put the line in double quotes?  I'm not sure if it's always required, however, some of my 'or' statements will not work without them so I just put all of them in double quotes.

      when:  "(item.key is not regex("(Port|Vlan)")) or (item.key not in po_ints)"

Stefan Hornburg (Racke)

unread,
Dec 1, 2020, 11:37:17 AM12/1/20
to ansible...@googlegroups.com
On 12/1/20 5:17 PM, James Lagermann wrote:
> Have you tried to put the line in double quotes?  I'm not sure if it's always required, however, some of my 'or'
> statements will not work without them so I just put all of them in double quotes.
>
>       when:  "(item.key is not regex("(Port|Vlan)")) or (item.key not in po_ints)"

That might work, but in this case you need to use single quotes for the regex:

when: "(item.key is not regex('(Port|Vlan)')) or (item.key not in po_ints)"

Regards
Racke
> <https://groups.google.com/d/msgid/ansible-project/a0649fd5-6757-4e91-9424-904629aad1fdn%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/a0649fd5-6757-4e91-9424-904629aad1fdn%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> 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/81625ee8-3b31-a6b7-dc30-50b4db9b5d72%40linuxia.de
> <https://groups.google.com/d/msgid/ansible-project/81625ee8-3b31-a6b7-dc30-50b4db9b5d72%40linuxia.de>.
>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/36fbffed-c622-4565-b513-455dc1034e97n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/36fbffed-c622-4565-b513-455dc1034e97n%40googlegroups.com?utm_medium=email&utm_source=footer>.
OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages