Checking for multiple conditions using "when"

I have a list of modules I would like to deploy. With certain modules, I want to run a specific command against it. For example, if the module is a PHP or PERL module, I want to modify the ownership of the folder. If it’s anything else, ignore the task.

The task below fails

  • name: Modify ownership
    shell: chown www:www {{ module_path }}
    with_dict: deploy_modules
    when: ‘"{{ item.key }} == “php” or {{ item.key }} == “perl”’

TASK: [web | Modify ownership] *****************************************************
fatal: [web01] => error while evaluating conditional: "php == “perl” or php == “php”
fatal: [web02] => error while evaluating conditional: "php == “perl” or php == “php”

When using “when:”, you don’t have to wrap variables in braces. Just do this:

when: item.key == “php” or item.key == “perl”

For example, the following works for me:

$ cat test_with_dict.yml

  • hosts: localhost
    connection: local
    gather_facts: no
    vars:
  • values:
    foo:
    val1: “this is foo value 1”
    val2: “this is foo value 2”
    bar:

val1: “this is bar value 1”
val2: “this is bar value 2”
baz:

val1: “this is baz value 1”
val2: “this is baz value 2”
bam:

val1: “this is bam value 1”
val2: “this is bam value 2”
tasks:

  • name: do loop over values dictionary
    debug: var=item.value.val1
    with_dict: values
    when: item.key == ‘foo’ or item.key == ‘bar’

Still not working.

TASK: [web | Modify ownership] *****************************************************
fatal: [oqn-qc-web01] => error while evaluating conditional: item.key == “php” or item.key == “perl”’
fatal: [oqn-qc-web02] => error while evaluating conditional: item.key == “php” or item.key == “perl”’

How and where did you define deploy_modules?

Here’s the full thing,

This is from my group var file.

cat groups_vars/deploy_list

deploy_modules:

php:
tag: ‘abc123’
svn_path: ‘path_to_svn’
document_root: ‘/tmp’

perl:
tag: ‘abc987’
svn_path: ‘path_to_svn’
document_root: ‘/tmp’

end file

  • name: Deploying Modules
    action: shell svn export --username myuser --password mypassword {{ repository }}/{{ item.value.svn_path }}/tags/{{ item.value.tag }} /tmp/{{ item.key }}.{{revision_no}}
    with_dict: deploy_modules

  • name: Modify ownership
    shell: chown www:www {{ module_path }}
    with_dict: deploy_modules
    when: item.key == “php” or item.key == “perl”’

You have an errant trailing ’ at the end of your ‘when’ line:

when: item.key == “php” or item.key == “perl”’

Should instead be:

when: item.key == “php” or item.key == “perl”

I tested with that change and everything works as expected.

One potential improvement:

Wow … thanks a lot. That was it. Also, great tip, I was hoping ansible would allow me to use the “in” command.