How can I call list as a conditional for ansible_hostname

Hi team,

I am calling a list that contains multiple strings, How to put condition in when statement to match list of strings that matches ansible_hostname

below playbook does not work. please help

ansible_net_hostname: testalpcsr029

vars:

northregion: [ ‘alp’, alt’ ]

playbook:-
name: Configure
ios_config:
commands:

  • “{{ item }}”
    with_items: “{{ snmp_commands_ap }}”
    when:
  • “‘csr’ in ansible_net_hostname or ‘asw’ in ansible_net_hostname”
  • northregion in ansible_net_hostname

thanks

Use the search string test.

when: ansible_net_hostname is search(‘(csr|asw)’)

You could store the regexp in a variable:

vars:
regexp: ‘csr|asw’

when: ansible_net_hostname is search(regexp)

Or you can construct the regexp from a list in the task.

  • ios_config:

    when: ansible_net_hostname is search(regexp)

vars:
regexp: “{{ ‘|’.join(northregion) }}”

Great Sam!