Run an Ansible task only when the variable contains a specific string
Asked Answered
L

10

106

I have multiple tasks depend from the value of variable1. I want to check if the value is in {{ variable1 }} but I get an error:

- name: do something when the value in variable1
  command: <command>
  when: "'value' in {{ variable1 }}"

I'm using ansible 2.0.2

Leotie answered 8/4, 2016 at 10:8 Comment(1)
Answer below works for me (well, I use it in assert: ... that: .... What error do you see?Delbert
C
117

If variable1 is a string, and you are searching for a substring in it, this should work:

when: '"value" in variable1'

if variable1 is an array or dict instead, in will search for the exact string as one of its items.

Codding answered 8/4, 2016 at 10:35 Comment(3)
this method works better, won't trigger warning as in result|searchBloodandthunder
This only works in later versions of Ansible, for alternatives for earlier versions see other answers.Epididymis
I was using that with ansible 1.9, if iI remember correctly; if I had to write it today I wouldn't use that.Codding
C
51

None of the above answers worked for me in ansible 2.3.0.0, but the following does:

when: variable1 | search("value")

In ansible 2.9 this is deprecated in favor of using ~ concatenation for variable replacement:

when: "variable1.find('v=' ~ value) == -1"

http://jinja.pocoo.org/docs/dev/templates/#other-operators

Other options:

when: "inventory_hostname in groups[sync_source]"
Cesarean answered 25/5, 2017 at 17:55 Comment(8)
+1 I prefer this answer. You can also add 'not' before 'variable1' if you want to check that variable1 does not contain 'value'. when: not variable1 | search("value")Hack
What if you want to negate / not this? Like if "value" is not in variable1?Chart
@SimonWoodside #32786622Cesarean
says this method to be deprecated in 2.9. Instead of using result|search use result is searchBloodandthunder
But the fact is I find result|search works better even with warning. Lot of discussion on this: github.com/ansible/ansible/issues/22397Bloodandthunder
Use '"value" in variable1' as suggested in another solution, won't trigger warning.Bloodandthunder
using variable1.find is atrocious and unreadable compared to variable1 | search("")Anglaangle
'"value" in var' doesnt work when your using a partial string, thats the only problem, and why search would be nice.Gabi
G
42

From Ansible 2.5

when: variable1 is search("value")

For negative scenarios

when: not variable1 is search("value")
Glorianna answered 31/8, 2018 at 9:31 Comment(5)
Great it works in ansible 2.9! If I need a condition that works only when the variable does not contain a specific string, what is the correct syntax? I was thinking to something like this: when: variable1 is not search("value") but is not working...Meter
Hi, it is when: not variable1 is search("value")Glorianna
@imjoseangel, Please add "not" case to the answer too...Alesiaalessandra
Does search supports more than 1 sub-string check in a variable? i.e. when: var is search("substring1|substring2") to search either "substring1" or "substring2" in variable var? Or we have to add that separately using and. If I have a condition when: var is search("substring1") or var is seach("substring2) and host is search("vm001") then, how can I club the first 2 conditions (with or) i.e. it becomes condition 1 and condition 2. Thanks. Use '(' ')'?Leifleifer
@Glorianna so figured out that, search("...") is cool enough to multi-search pattern like grep "substring[12]" can do. First I thought if it can do something like egrep can do i.e. egrep "substring1|substring2|coolsubstr3" but it didn't. Then idea came, what if it can do search("substring[12]" and that worked in my case in when condition.Leifleifer
G
18

Some of the answers no longer work as explained.

Currently here is something that works for me in ansible 2.6.x

 when: register_var.stdout is search('some_string')
Gahnite answered 11/2, 2019 at 19:10 Comment(0)
C
9

In Ansible version 2.9.2:

If your variable variable1 is declared:

when: "'value' in variable1"

If you registered variable1 then:

when: "'value' in variable1.stdout"

Catercornered answered 4/3, 2020 at 1:4 Comment(0)
E
9

This works for me in Ansible 2.9:

variable1 = www.example.com. 
variable2 = www.example.org. 

when: ".com" in variable1

and for not:

when: not ".com" in variable2
Electrothermal answered 12/5, 2020 at 17:32 Comment(0)
F
8

This example uses regex_search to perform a substring search.

- name: make conditional variable
  command: "file -s /dev/xvdf"
  register: fsm_out

- name: makefs
  command: touch "/tmp/condition_satisfied"
  when: fsm_out.stdout | regex_search(' data')

ansible version: 2.4.3.0

Fung answered 30/4, 2018 at 13:28 Comment(0)
S
6

use this

when: "{{ 'value' in variable1}}"

instead of

when: "'value' in {{variable1}}"

Also for string comparison you can use

when: "{{ variable1 == 'value' }}"

Strawboard answered 13/9, 2018 at 17:50 Comment(1)
when does not need curly braces: docs.ansible.com/ansible/latest/user_guide/… (today, no idea about 2013)Brooder
L
0

If one wants to club more than one similar looking sub-strings in just one search(), then a user can also do something like:

when: variable1 is search("value[12]")

which will be similar to:

when: variable1 is search("value1") or variable1 is search("value2")

:)

Leifleifer answered 12/9, 2023 at 17:1 Comment(0)
M
-2

I used

failed_when: not(promtool_version.stdout.find('1.5.2') != -1)

means - failed only when the previously registered variable "promtool_version" doesn't contains string '1.5.2'.

Mastiff answered 30/3, 2017 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.