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

LinuxConditionalAnsibleAnsible PlaybookAnsible 2.x

Linux Problem Overview


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

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

I'm using ansible 2.0.2

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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]"

Solution 3 - Linux

From Ansible 2.5

when: variable1 is search("value")

Solution 4 - Linux

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')

Solution 5 - Linux

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

Solution 6 - Linux

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

Solution 7 - Linux

use this

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

instead of

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

Also for string comparison you can use

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

Solution 8 - Linux

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"

Solution 9 - Linux

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'.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmndhrView Question on Stackoverflow
Solution 1 - LinuxguidoView Answer on Stackoverflow
Solution 2 - LinuxDenise MauldinView Answer on Stackoverflow
Solution 3 - LinuximjoseangelView Answer on Stackoverflow
Solution 4 - LinuxDrewView Answer on Stackoverflow
Solution 5 - LinuxobjectView Answer on Stackoverflow
Solution 6 - LinuxChirag VyasView Answer on Stackoverflow
Solution 7 - LinuxsdinView Answer on Stackoverflow
Solution 8 - LinuxmudricdView Answer on Stackoverflow
Solution 9 - LinuxWojciech SciesinskiView Answer on Stackoverflow