0

I am running a task which i only want to execute if the value of a variable that i have previously set does not exist in another variable. I have tried the following but it errors with a templating error:

name: get ip address
...
register: ipaddress

name: check cluster
....
register: topology

name: do my task
...
when: not topology is search(ipaddress)

Is there any way in ansible to accomplish what i am doing? I'm using version 2.6.

1 Answer 1

2

Use regex_filter.

Regular Expression Filters

To search a string with a regex, use the “regex_search” filter:

search for "foo" in "foobar"

{{ 'foobar' | regex_search('(foo)') }}

will return empty if it cannot find a match

{{ 'ansible' | regex_search('(foobar)') }}

case insensitive search in multiline mode

{{ 'foo\nBAR' | regex_search("^bar", multiline=True, ignorecase=True) }}

For your example, making a few assumptions -

when: not topology | regex_search(ipaddress, multiline=True)

(in a meeting, can't test this, please check it and let me know if it isn't quite right.)

Sign up to request clarification or add additional context in comments.

1 Comment

Didn't work for me. I got the following error: "The conditional check 'not topology | regex_search(ipaddress, multiline=True)' failed. The error was: Unexpected templating type error occurred on ({% if not topology | regex_search(ipaddress, multiline=True) %} True {% else %} False {% endif %}): expected string or buffer"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.