# Use regex and variables in a when statement

**URL:** <https://forum.ansible.com/t/use-regex-and-variables-in-a-when-statement/28563>\
**Category:** Ansible Project\
**Created:** [September 13, 2018, 11:48am UTC](https://forum.ansible.com/t/use-regex-and-variables-in-a-when-statement/28563 "2018-09-13T11:48:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [September 13, 2018, 11:48am UTC](https://forum.ansible.com/t/use-regex-and-variables-in-a-when-statement/28563/1 "2018-09-13T11:48:12Z")

</div>

Hello!

I need to run a task based on the stderr output of a previous task:

My stderr output is as follows:

“stderr”: “Failed to enable application app2\_20180601.05 : Application app1\_20180501.01 has not been disabled yet.”

**Defined Variable:**

newApp: app2\_20180601.05

The value below, can be any value, and it could change from time-to-time (and is not defined as a variable, it’s just something the stderr reports). So essentially I am planning to make that a regex or somehow use a wild card so that my when statement always picks it up.  
app1\_20180501.01

**Question: How can I use regex in the when statement? Along with some variables?**

For example if I set it persistently, it always works:

`

- debug:  
msg: The old application has not been disabled yet  
when: ‘app\_enabled\_status.stderr is search(“Failed to enable application " ~ newApp ~ " : Application app1\_20180501.01 has not been disabled yet.”)’  
`

However, I would like to set it to:

`

- debug:  
msg: The old application has not been disabled yet  
when: ‘app\_enabled\_status.stderr is search(“Failed to enable application " ~ newApp ~ " : Application \* has not been disabled yet.”)’  
`

or

- debug:  
msg: The old application has not been disabled yet  
when: ‘app\_enabled\_status.stderr is search("Failed to enable application " ~ newApp ~ " : Application (’\w+\d\_\d{8}.\d{2}‘) has not been disabled yet.")’

Many thanks for your assistance in advance!

Regards  
JS
