Checking for multiple conditions using "when"

9,978 views
Skip to first unread message

Samnang Sen

unread,
May 5, 2014, 4:57:30 PM5/5/14
to ansible...@googlegroups.com
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"

James Cammarata

unread,
May 5, 2014, 9:18:28 PM5/5/14
to ansible...@googlegroups.com
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'




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/11fc9d73-a04b-42c3-955b-d555015253b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Samnang Sen

unread,
May 7, 2014, 4:27:36 PM5/7/14
to ansible...@googlegroups.com
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"'

Strahinja Kustudić

unread,
May 7, 2014, 4:39:12 PM5/7/14
to ansible...@googlegroups.com
How and where did you define deploy_modules?

Samnang Sen

unread,
May 7, 2014, 4:42:37 PM5/7/14
to ansible...@googlegroups.com
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"'

Matt Martz

unread,
May 7, 2014, 4:52:34 PM5/7/14
to ansible...@googlegroups.com, Samnang Sen
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:

when: item.key in ["php", "perl"]
-- 
Matt Martz
ma...@sivel.net

Samnang Sen

unread,
May 7, 2014, 4:57:46 PM5/7/14
to ansible...@googlegroups.com, Samnang Sen
Wow ... thanks a lot. That was it. Also, great tip, I was hoping ansible would allow me to use the "in" command.
Reply all
Reply to author
Forward
0 new messages