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

mndhr picture mndhr · Apr 8, 2016 · Viewed 150.6k times · Source

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

Answer

guido picture guido · Apr 8, 2016

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.