# Help with conditionals syntax

**URL:** <https://forum.ansible.com/t/help-with-conditionals-syntax/25805>\
**Category:** Ansible Project\
**Created:** [October 23, 2017, 11:01pm UTC](https://forum.ansible.com/t/help-with-conditionals-syntax/25805 "2017-10-23T23:01:57Z")\
**Posts on this page:** 7\
**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:** [October 23, 2017, 11:01pm UTC](https://forum.ansible.com/t/help-with-conditionals-syntax/25805/1 "2017-10-23T23:01:57Z")

</div>

I know my syntax is wrong but you can get the gist with the code below. Can someone correct my invalid syntax?

Works:

`when: ansible_hostname | search("apps")`

Fails:

`  
when: ansible\_hostname | search(“apps”) and  
ansible\_hostname != bad\_server|bad\_server2

`

---

<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:** [October 24, 2017, 2:57pm UTC](https://forum.ansible.com/t/help-with-conditionals-syntax/25805/2 "2017-10-24T14:57:23Z")

</div>

anyone?

---

<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:** [October 24, 2017, 2:59pm UTC](https://forum.ansible.com/t/help-with-conditionals-syntax/25805/3 "2017-10-24T14:59:28Z")

</div>

when:

- ansible\_hostname | search(“apps”)
- ansible\_hostname not in [“bad\_server”, “bad\_server2”]

Note: using a list format for `when` is an implicit AND

---

<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:** [October 24, 2017, 3:03pm UTC](https://forum.ansible.com/t/help-with-conditionals-syntax/25805/4 "2017-10-24T15:03:15Z")

</div>

Thank you! I didn’t know a list was an implicit AND. I tested it and it works. I appreciate you taking the time to help me out (and teach me something too)!

---

<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:** [October 24, 2017, 3:18pm UTC](https://forum.ansible.com/t/help-with-conditionals-syntax/25805/5 "2017-10-24T15:18:37Z")

</div>

One follow-up question… how could I do this with a wildcard? Also, are these jinja2? I am trying to find documentation to guide me, but don’t know what to look for.

`

when:

- ansible\_hostname | search(“apps”)
- ansible\_hostname not in [“\*\_server”]

`

---

<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:** [October 24, 2017, 3:22pm UTC](https://forum.ansible.com/t/help-with-conditionals-syntax/25805/6 "2017-10-24T15:22:12Z")

</div>

They are jinja. You cannot do a wildcard like that. You would instead want to use search again, but negate the evaluation:

- not ansible\_hostname | search(‘.\*\_server’)

---

<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:** [October 24, 2017, 3:29pm UTC](https://forum.ansible.com/t/help-with-conditionals-syntax/25805/7 "2017-10-24T15:29:43Z")

</div>

Thanks again Matt.
