# Best way to test for a substring in a variable

**URL:** <https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947>\
**Category:** Ansible Project\
**Created:** [November 20, 2013, 7:08pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947 "2013-11-20T19:08:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@Ryan\_Mitchell](https://forum.ansible.com/u/Ryan_Mitchell)\
**Post date:** [November 20, 2013, 7:08pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947/1 "2013-11-20T19:08:07Z")

</div>

Sorry if this has been answered somewhere but I couldn’t find anything that really addressed this, or I’m really dense about how best to solve this problem.

I have a variable that should be either a fully-qualified domain name or an IP address. I want to throw a warning or error if the variable is not set to one of the two, or even optionally just use the ansible\_ssh\_host or ansible\_inventory\_hostname if there is no match. I’m not sure how to actually do the conditional check, though. The “when:” conditional statement doesn’t seem to give me the options I want natively, so should I be dropping into only\_if and jinja2/python syntax instead? Any other suggestions?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 20, 2013, 7:13pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947/2 "2013-11-20T19:13:18Z")

</div>

“I want to throw a warning or error if the variable is not set to one of the two,”

- debug: msg=“Hey, this might sound crazy, but this variable is not set to duck or goose”  
when: “var\_name != 'duck and variable != ‘goose’”

"or even optionally just use the ansible\_ssh\_host or ansible\_inventory\_hostname if there is no match. "

{{ var\_name | default(ansible\_ssh\_host) }}

only\_if has been a deprecated feature for ages, and when absolutely does everything you want.

---

<div class="post-metadata">

**Author:** ![Ryan\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@Ryan\_Mitchell](https://forum.ansible.com/u/Ryan_Mitchell)\
**Post date:** [November 20, 2013, 7:18pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947/3 "2013-11-20T19:18:28Z")

</div>

Sorry, I guess I wasn’t clear in my original post. I need to test for a FQDN vs. just a machine name. So if the variable is appended with “.[domain.com](http://domain.com)” it’s okay. I won’t be matching a specific string, so it’s more of a “contains” type thing. If it was regexp I’d be writing something like “._.domain.com". If it’s an IP address (not a specific one, mind you, just a valid IP) - meaning if I was using regexp I’d do something like (might not compile, but you probably know what I mean) "\d_.\d\*.\d\*.\d\*”. If it doesn’t match those two situations I’ll go with the default - although I like your default statement - I didn’t know about that.

I know only\_if has been deprecated (and I’m glad!) - all my scripts use “when” now, just not sure how to do a conditional substring search.

Sorry for the confusion.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [November 20, 2013, 7:26pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947/4 "2013-11-20T19:26:16Z")

</div>

when: ​variable.find('somestring') != -1

---

<div class="post-metadata">

**Author:** ![Ryan\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@Ryan\_Mitchell](https://forum.ansible.com/u/Ryan_Mitchell)\
**Post date:** [November 20, 2013, 7:39pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947/5 "2013-11-20T19:39:49Z")

</div>

Ahh, perfect. Thanks Serge, I’ll give that a go.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 23, 2013, 4:15pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947/6 "2013-11-23T16:15:34Z")

</div>

also:

when: “‘something’ in variable”

you have to quote it because you’ll have non-matching string quotes otherwise, but it at least doesn’t look like Python (for those that want to avoid it)

---

<div class="post-metadata">

**Author:** ![Ryan\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@Ryan\_Mitchell](https://forum.ansible.com/u/Ryan_Mitchell)\
**Post date:** [November 23, 2013, 5:25pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947/7 "2013-11-23T17:25:20Z")

</div>

Thanks Michael. It appears that a “not in” isn’t an option?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 23, 2013, 5:51pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947/8 "2013-11-23T17:51:14Z")

</div>

Why not?

– Michael

---

<div class="post-metadata">

**Author:** ![Ryan\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@Ryan\_Mitchell](https://forum.ansible.com/u/Ryan_Mitchell)\
**Post date:** [November 23, 2013, 5:57pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947/9 "2013-11-23T17:57:50Z")

</div>

Wouldn’t take it - syntax error. Was that perhaps added in 1.4? I’m still on 1.3.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 23, 2013, 6:54pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947/10 "2013-11-23T18:54:12Z")

</div>

Not added in 1.4, just Jinja

Probably wants the not before the statement

– Michael

---

<div class="post-metadata">

**Author:** ![Ryan\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@Ryan\_Mitchell](https://forum.ansible.com/u/Ryan_Mitchell)\
**Post date:** [November 23, 2013, 7:39pm UTC](https://forum.ansible.com/t/best-way-to-test-for-a-substring-in-a-variable/13947/11 "2013-11-23T19:39:48Z")

</div>

I’ll try that. I didn’t see the “not” in jinja docs but I might have missed it. Thanks.
