# How can I call list as a conditional for ansible\_hostname

**URL:** <https://forum.ansible.com/t/how-can-i-call-list-as-a-conditional-for-ansible-hostname/31591>\
**Category:** Ansible Project\
**Created:** [November 4, 2019, 6:21am UTC](https://forum.ansible.com/t/how-can-i-call-list-as-a-conditional-for-ansible-hostname/31591 "2019-11-04T06:21:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pushparaj\_Gothandam](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pushparaj_gothandam/32/5538_2.png) [@Pushparaj\_Gothandam](https://forum.ansible.com/u/Pushparaj_Gothandam)\
**Post date:** [November 4, 2019, 6:21am UTC](https://forum.ansible.com/t/how-can-i-call-list-as-a-conditional-for-ansible-hostname/31591/1 "2019-11-04T06:21:39Z")

</div>

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”
- north **region in ansible\_net\_hostname**

thanks

---

<div class="post-metadata">

**Author:** ![sdoran](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@sdoran](https://forum.ansible.com/u/sdoran)\
**Post date:** [November 4, 2019, 3:15pm UTC](https://forum.ansible.com/t/how-can-i-call-list-as-a-conditional-for-ansible-hostname/31591/2 "2019-11-04T15:15:29Z")

</div>

Use the search [string test](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings).

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

---

<div class="post-metadata">

**Author:** ![Pushparaj\_Gothandam](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pushparaj_gothandam/32/5538_2.png) [@Pushparaj\_Gothandam](https://forum.ansible.com/u/Pushparaj_Gothandam)\
**Post date:** [December 5, 2019, 4:17pm UTC](https://forum.ansible.com/t/how-can-i-call-list-as-a-conditional-for-ansible-hostname/31591/3 "2019-12-05T16:17:53Z")

</div>

Great Sam!
