# when condition on a variable starting with a string value

**URL:** <https://forum.ansible.com/t/when-condition-on-a-variable-starting-with-a-string-value/23663>\
**Category:** Ansible Project\
**Created:** [February 7, 2017, 1:45pm UTC](https://forum.ansible.com/t/when-condition-on-a-variable-starting-with-a-string-value/23663 "2017-02-07T13:45:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [February 7, 2017, 1:45pm UTC](https://forum.ansible.com/t/when-condition-on-a-variable-starting-with-a-string-value/23663/1 "2017-02-07T13:45:44Z")

</div>

Hello,

Actually I wrote my playbook this way to set values id the dual variable is the string value SRV:

- set\_fact:  
distrib: “{{ distribsrv }}”  
Directoryname: “{{ Directorysrv }}”  
when: dual == ‘SRV’

I would like to modify the when condition to run it when dual == ‘SRV1’, "SRV2’ … etc, is there a way to do a when condition : when : dual like ‘SRV\*’ ?

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [February 7, 2017, 2:53pm UTC](https://forum.ansible.com/t/when-condition-on-a-variable-starting-with-a-string-value/23663/2 "2017-02-07T14:53:30Z")

</div>

I wrote this:

vars:  
services:

- SRV

- SRV[1:99]

- set\_fact:  
distrib: “{{ distribsrv }}”  
when: dual in services

but dual is SRV2 and the set\_fact module has been ignored

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [February 7, 2017, 3:32pm UTC](https://forum.ansible.com/t/when-condition-on-a-variable-starting-with-a-string-value/23663/3 "2017-02-07T15:32:17Z")

</div>

if I write this, i do not get a variable list:

- set\_fact:  
services: “{{ services + item }}”  
with\_sequence: count=99
- debug: msg=“sequence {{ services }}”

ok: [SRV1] =\> {  
“msg”: “sequence SRV123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899”  
}

the result expected is SRV1, SRV2, SRV3 …

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [February 7, 2017, 3:34pm UTC](https://forum.ansible.com/t/when-condition-on-a-variable-starting-with-a-string-value/23663/4 "2017-02-07T15:34:39Z")

</div>

My issue is: I would like to run a task when a variable start with ‘SRV’ (srv1, SRV2 …) ad I don’t know how to write the condition: when: dual like ‘SRV\*’

---

<div class="post-metadata">

**Author:** ![Stankovic\_Marko](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stankovic\_Marko](https://forum.ansible.com/u/Stankovic_Marko)\
**Post date:** [February 7, 2017, 3:48pm UTC](https://forum.ansible.com/t/when-condition-on-a-variable-starting-with-a-string-value/23663/5 "2017-02-07T15:48:32Z")

</div>

I know this is weird, but can't think of something better:

&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;distrib: "{{ distribsrv }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Directoryname: "{{ Directorysrv }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: dual | regex\_replace( '^SRV.\*', 'True') | bool

Cheers,  
Marko  
CONFIDENTIALITY NOTICE: This message is the property of International Game Technology PLC and/or its subsidiaries and may contain proprietary, confidential or trade secret information. This message is intended solely for the use of the addressee. If you are not the intended recipient and have received this message in error, please delete this message from your system. Any unauthorized reading, distribution, copying, or other use of this message or its attachments is strictly prohibited.

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [February 7, 2017, 3:52pm UTC](https://forum.ansible.com/t/when-condition-on-a-variable-starting-with-a-string-value/23663/6 "2017-02-07T15:52:33Z")

</div>

thank you I will have a look at your solution, I finally found this way to proceed otherwise:

- set\_fact:  
services: “{{ services }} + SRV{{item }}”

with\_sequence: count=5

- set\_fact:  
distrib: “{{ distribsrv }}”  
when: dual in services

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [SRV1] =\> {  
“msg”: “sequence SRV + SRV1 + SRV2 + SRV3 + SRV4 + SRV5”  
}

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [February 7, 2017, 4:07pm UTC](https://forum.ansible.com/t/when-condition-on-a-variable-starting-with-a-string-value/23663/7 "2017-02-07T16:07:41Z")

</div>

Thanks a lot Marko, I used your syntax:  
dual\>regex\_replace(‘^SRV(.\*)’, ‘True’)

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [February 8, 2017, 8:14am UTC](https://forum.ansible.com/t/when-condition-on-a-variable-starting-with-a-string-value/23663/8 "2017-02-08T08:14:29Z")

</div>

> Thanks a lot Marko, I used your syntax:  
> dual\>regex\_replace('^SRV(.\*)', 'True')

Did you try the following?

> No need to use regex for pattern searching. You can use search like  
> this:
> 
> when: name\_prefix | search("stage-dbs")

from  
[https://stackoverflow.com/questions/34429842/how-to-evaluate-a-when-condition-for-ansible-task](https://stackoverflow.com/questions/34429842/how-to-evaluate-a-when-condition-for-ansible-task)

In your case this should be:

when: dual | search("SRV")

Johannes
