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

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

This question is related to linux conditional ansible ansible-playbook ansible-2.x

The answer is


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.


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"


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


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

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

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


use this

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

instead of

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

Also for string comparison you can use

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


From Ansible 2.5

when: variable1 is search("value")

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

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to conditional

Pandas/Python: Set value of one column based on value in another column Run an Ansible task only when the variable contains a specific string (Excel) Conditional Formatting based on Adjacent Cell Value Laravel Checking If a Record Exists Multiple conditions in if statement shell script The condition has length > 1 and only the first element will be used Creating a new column based on if-elif-else condition How to conditional format based on multiple specific text in Excel Using SUMIFS with multiple AND OR conditions Replacing Numpy elements if condition is met

Examples related to ansible

Specifying ssh key in ansible playbook file Ansible: how to get output to display How to do multiline shell script in Ansible Accessing inventory host variable in Ansible playbook Ansible: get current target host's IP address Ansible Ignore errors in tasks and fail at end of the playbook if any tasks had errors Ansible: How to delete files and folders inside a directory? How to pass a user / password in ansible command How to test that a registered variable is not empty? Copy multiple files with Ansible

Examples related to ansible-playbook

Ansible Ignore errors in tasks and fail at end of the playbook if any tasks had errors Ansible: create a user with sudo privileges Run an Ansible task only when the variable contains a specific string Ansible: Store command's stdout in new variable? Checking for multiple conditions using "when" on single task in ansible how to define ssh private key for servers fetched by dynamic inventory in files Override hosts variable of Ansible playbook from the command line Proper way to concatenate variable strings How can I pass variable to ansible playbook in the command line? How to run a task when variable is undefined in ansible?

Examples related to ansible-2.x

Accessing inventory host variable in Ansible playbook Run an Ansible task only when the variable contains a specific string Ansible: copy a directory content to another directory Ansible - Use default if a variable is not defined