# Questions regarding "when: condition"

**URL:** <https://forum.ansible.com/t/questions-regarding-when-condition/26536>\
**Category:** Ansible Project\
**Created:** [January 22, 2018, 10:55pm UTC](https://forum.ansible.com/t/questions-regarding-when-condition/26536 "2018-01-22T22:55:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 22, 2018, 10:55pm UTC](https://forum.ansible.com/t/questions-regarding-when-condition/26536/1 "2018-01-22T22:55:22Z")

</div>

The commented section of the following code works. I was trying to simplify it by adding with\_items; however, it seems to change the way it is evaluated?.. at least that is my guess based on the verbosity:

`  
skipping: [ansible-oel6] =\> (item=ansible) =\> {  
“changed”: false,  
“item”: “ansible”,  
“skip\_reason”: “Conditional result was False”  
}

`

Playbook.

`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 22, 2018, 10:57pm UTC](https://forum.ansible.com/t/questions-regarding-when-condition/26536/2 "2018-01-22T22:57:58Z")

</div>

Using `with_items` like you are, you would need to use `search(item)` without the quotes around item. Otherwise “item” is treated as the string item, instead of the variable called item.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 22, 2018, 11:02pm UTC](https://forum.ansible.com/t/questions-regarding-when-condition/26536/3 "2018-01-22T23:02:14Z")

</div>

That is good to know, but I made the change and still experience the issue; however, with that info I changed it to the following which works for the first section:

`  
when: item in ansible\_hostname

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 23, 2018, 7:34pm UTC](https://forum.ansible.com/t/questions-regarding-when-condition/26536/4 "2018-01-23T19:34:45Z")

</div>

> &nbsp;&nbsp;when: ansible\_hostname | lower | search("item")  
> # when: ansible\_hostname | lower | search("prod") or  
> # ansible\_hostname | lower | search("backup") or  
> # ansible\_hostname | lower | search("nfs") or  
> # ansible\_hostname | lower | search("ansible")  
> &nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;- prod  
> &nbsp;&nbsp;&nbsp;&nbsp;- backup  
> &nbsp;&nbsp;&nbsp;&nbsp;- nfs  
> &nbsp;&nbsp;&nbsp;&nbsp;- ansible  
> &nbsp;&nbsp;failed\_when: false
> 
> So, I have a couple of questions.  
> 1. What would be the proper way to see if the hostname contains "splat"  
> using with\_items?

You got the answer from Matt, and that should work.

An alternative is to not use with\_items but just use regex or.

&nbsp;&nbsp;when: ansible\_hostname | lower is search("prod|backup|nfs|ansible")

> 2. What would be the reverse of this? (ie not in, or not contained, etc)

&nbsp;&nbsp;when: not (ansible\_hostname | lower is search("prod|backup|nfs|ansible"))

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 23, 2018, 7:36pm UTC](https://forum.ansible.com/t/questions-regarding-when-condition/26536/5 "2018-01-23T19:36:56Z")

</div>

Oh, I like that. Thank you.
