# extending conditional task execution

**URL:** <https://forum.ansible.com/t/extending-conditional-task-execution/11713>\
**Category:** Ansible Project\
**Created:** [July 23, 2012, 10:37pm UTC](https://forum.ansible.com/t/extending-conditional-task-execution/11713 "2012-07-23T22:37:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timothy\_Appnel](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Timothy\_Appnel](https://forum.ansible.com/u/Timothy_Appnel)\
**Post date:** [July 23, 2012, 10:37pm UTC](https://forum.ansible.com/t/extending-conditional-task-execution/11713/1 "2012-07-23T22:37:38Z")

</div>

Before I just code something up and submit it I wanted to float this one by the list to see if anyone had a different idea or opposed it entirely.

I hit a situation today where I needed to conditional run a task based on a pattern as opposed to an absolute value.

Ansible supports conditionals but it uses a simple eval statement so I can't (to my knowledge) import re and then do a match. That's understandable and probably a good idea. That still doesn't help me with my problem though.

I'm thinking a specialized task conditional, like only\_if\_match, would be best where its value would be used to perform a re.search() and return a boolean result. I don't like the idea of adding another key, but determining if a string is something to eval or a regex to compile and match would be worse IMO.

Thoughts?

\<tim/\>

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 23, 2012, 10:44pm UTC](https://forum.ansible.com/t/extending-conditional-task-execution/11713/2 "2012-07-23T22:44:21Z")

</div>

> I'm thinking a specialized task conditional, like only\_if\_match, would be best where its value would be used to perform a re.search() and return a boolean result. I don't like the idea of adding another key, but determining if a string is something to eval or a regex to compile and match would be worse IMO.

I am against adding new syntax here.

It seems easiest if runner/\_\_init\_\_.py just imports "re" such that this will work for you.

Then you should be able to do all the string methods that take regexes just fine.

--Michael

---

<div class="post-metadata">

**Author:** ![Timothy\_Appnel](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Timothy\_Appnel](https://forum.ansible.com/u/Timothy_Appnel)\
**Post date:** [July 23, 2012, 11:42pm UTC](https://forum.ansible.com/t/extending-conditional-task-execution/11713/3 "2012-07-23T23:42:43Z")

</div>

OK. That works for me.

I was thinking about avoiding an unnecessary import (re is almost definitely being loaded elsewhere tho) and the ability to compile & cache a pattern. Nice to have but not necessary.

\<tim/\>
