102

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

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
mndhr
  • 1,409
  • 5
  • 13
  • 17
  • Answer below works for me (well, I use it in `assert: ... that: ...`. What error do you see? – jhutar Oct 01 '16 at 20:07

9 Answers9

111

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.

guido
  • 18,864
  • 6
  • 70
  • 95
52

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]"
Denise Mauldin
  • 5,397
  • 5
  • 32
  • 51
38

From Ansible 2.5

when: variable1 is search("value")

For negative scenarios

when: not variable1 is search("value")
imjoseangel
  • 3,543
  • 3
  • 22
  • 30
  • 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... – gaetano Mar 19 '20 at 14:43
  • 7
    Hi, it is `when: not variable1 is search("value")` – imjoseangel Mar 19 '20 at 14:52
  • 1
    @imjoseangel, Please add "not" case to the answer too... – Sriman Aug 18 '22 at 12:00
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')
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Drew
  • 491
  • 5
  • 9
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
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Chirag Vyas
  • 101
  • 1
  • 3
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

object
  • 796
  • 1
  • 12
  • 21
8

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"

mudricd
  • 133
  • 1
  • 9
6

use this

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

instead of

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

Also for string comparison you can use

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

sdin
  • 119
  • 2
  • 4
  • 2
    when does not need curly braces: https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#id2 (today, no idea about 2013) – simohe Dec 01 '20 at 20:04
-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'.