When Or statement

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”)

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"\)

Maybe you need ( ) around both conditions, but you can also use (untested)

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

Regards
       Racke

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)

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

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)”

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