# Conditional wild card usage

**URL:** <https://forum.ansible.com/t/conditional-wild-card-usage/33341>\
**Category:** Ansible Project\
**Created:** [July 7, 2020, 7:46pm UTC](https://forum.ansible.com/t/conditional-wild-card-usage/33341 "2020-07-07T19:46:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandeep\_athoti](https://avatars.discourse-cdn.com/v4/letter/s/74df32/32.png) [@sandeep\_athoti](https://forum.ansible.com/u/sandeep_athoti)\
**Post date:** [July 7, 2020, 7:46pm UTC](https://forum.ansible.com/t/conditional-wild-card-usage/33341/1 "2020-07-07T19:46:07Z")

</div>

Is there any way I can use the wild cards or regex in when statements,

when: ansible\_distribution\_major\_version|int == 6 or ansible\_fqdn == “test[0-1][0-9].example.com”

I actually do not want to pass hostname of my server in vars section. Will this regex work with in conjunction with Ansible facts ?

During my playbook execution time, it need to interpret my hostname of the server from fact ansible\_fqdn.

Samole playbook,

hosts: localhost  
tasks:  
name: debug  
debug:  
msg: “passed”  
when: “ansible\_distribution\_major\_version|int == 6 or ansible\_fqdn | regex\_search(‘test[0-9][0-1]-[0-9][0-9].example.com’)”

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 7, 2020, 9:35pm UTC](https://forum.ansible.com/t/conditional-wild-card-usage/33341/2 "2020-07-07T21:35:37Z")

</div>

See "Testing strings"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_tests.html#testing-strings](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings)

For example

&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostnames:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- test56.example.com  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- testXY.example.com  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_regex: '^test(\d\d)\.example\.com'  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: OK  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ hostnames }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item is match(my\_regex)

gives

&nbsp;&nbsp;ok: [localhost] =\> (item=test56.example.com) =\>  
&nbsp;&nbsp;&nbsp;&nbsp;msg: OK  
&nbsp;&nbsp;skipping: [localhost] =\> (item=testXY.example.com)
