Best way to test for a substring in a variable

17,389 views
Skip to first unread message

Ryan Mitchell

unread,
Nov 20, 2013, 2:08:07 PM11/20/13
to ansible...@googlegroups.com
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?

Michael DeHaan

unread,
Nov 20, 2013, 2:13:18 PM11/20/13
to ansible...@googlegroups.com
"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.




On Wed, Nov 20, 2013 at 2:08 PM, Ryan Mitchell <mi...@threeweekhobby.com> wrote:
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?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Ryan Mitchell

unread,
Nov 20, 2013, 2:18:28 PM11/20/13
to ansible...@googlegroups.com
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" 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.


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/D2hQzZ_jNuM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

Serge van Ginderachter

unread,
Nov 20, 2013, 2:26:16 PM11/20/13
to ansible...@googlegroups.com

On 20 November 2013 20:18, Ryan Mitchell <mi...@threeweekhobby.com> wrote:
so it's more of a "contains" type thing.

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

Ryan Mitchell

unread,
Nov 20, 2013, 2:39:49 PM11/20/13
to ansible...@googlegroups.com
Ahh, perfect.  Thanks Serge, I'll give that a go.


Michael DeHaan

unread,
Nov 23, 2013, 11:15:34 AM11/23/13
to ansible...@googlegroups.com
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)




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

Ryan Mitchell

unread,
Nov 23, 2013, 12:25:20 PM11/23/13
to ansible...@googlegroups.com
Thanks Michael. It appears that a "not in" isn't an option?


Michael DeHaan

unread,
Nov 23, 2013, 12:51:14 PM11/23/13
to ansible...@googlegroups.com
Why not? 

-- Michael

Ryan Mitchell

unread,
Nov 23, 2013, 12:57:50 PM11/23/13
to ansible...@googlegroups.com
Wouldn't take it - syntax error. Was that perhaps added in 1.4? I'm still on 1.3. 


Michael DeHaan

unread,
Nov 23, 2013, 1:54:12 PM11/23/13
to ansible...@googlegroups.com
Not added in 1.4, just Jinja

Probably wants the not before the statement

-- Michael

Ryan Mitchell

unread,
Nov 23, 2013, 2:39:48 PM11/23/13
to ansible...@googlegroups.com
I'll try that. I didn't see the "not" in jinja docs but I might have missed it. Thanks. 
Reply all
Reply to author
Forward
0 new messages