Problem with variables

I am having trouble with variables in my playbook. I have included a sample playbook and the output. I am trying to define multiple variables based on distributions and major releases to restrict when the task will be performed. Simple variables work but I am not sure how to use conditional logic in when clauses. The test that work seem to generate a warning messages about not having to wrap variables in braces. In more complex cases I am not sure that expressions can be logically grouped. I have searched for answers and looked through the documentation and examples but have not been able to figure this out.

localhost is running ansible 1.4 just refreshed and the host os is CentOS 6

[patrickc@localhost ansible.test]$ more variables.yml

  • hosts: all
    gather_facts: yes
    vars:
    is_centos: “‘{{ ansible_distribution }}’ == ‘CentOS’”
    is_centos5: “‘{{ ansible_distribution }}’ == ‘CentOS’ and ‘{{ ansible_lsb[‘major_release’] }}’ == ‘5’”
    is_centos6: “‘{{ ansible_distribution }}’ == ‘CentOS’ and ‘{{ ansible_lsb[‘major_release’] }}’ == ‘6’”
    is_redhat: “‘{{ ansible_distribution }}’ == ‘RedHat’”
    is_suse: “‘{{ ansible_distribution }}’ == ‘Suse’”

tasks:

  • name: is_centos
    debug: msg=“SUCCESS is_centos”
    when: is_centos

  • name: is_redhat
    debug: msg=“FAIL is_redhat”
    when: is_redhat

  • name: is_suse
    debug: msg=“FAIL is_suse”
    when: is_suse

  • name: is_centos5
    debug: msg=“FAIL is_centos5”
    when: is_centos5

  • name: is_centos6
    debug: msg=“SUCCESS is_centos6”
    when: is_centos6

  • name: is_suse or is_centos5 t1
    debug: msg=“FAIL is_suse or is_centos5”
    when: is_suse or is_centos5

  • name: is_suse or is_centos5 t2
    debug: msg=“FAIL is_suse or is_centos5”
    when: “{{ is_suse or is_centos5 }}”

  • name: is_suse or is_centos5 t3
    debug: msg=“FAIL is_suse or is_centos5”
    when: “{{ is_suse }} or {{ is_centos5 }}”

  • name: is_suse or is_centos6 t1
    debug: msg=“SUCCESS is_suse or is_centos6”
    when: is_suse or is_centos6

  • name: is_suse or is_centos6 t2
    debug: msg=“SUCCESS is_suse or is_centos6”
    when: “{{ is_suse or is_centos6 }}”

  • name: is_suse or is_centos6 t3
    debug: msg=“SUCCESS is_suse or is_centos6”
    when: “{{ is_suse }} or {{ is_centos6 }}”

[patrickc@localhost ansible.test]$ ansible-playbook -i stage variables.yml
[WARNING]: It is unneccessary to use ‘{{’ in conditionals, leave variables in
loop expressions bare.

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [is_centos] *************************************************************
ok: [localhost] => {
“msg”: “SUCCESS is_centos”
}

TASK: [is_redhat] *************************************************************
skipping: [localhost]

TASK: [is_suse] ***************************************************************
skipping: [localhost]

TASK: [is_centos5] ************************************************************
skipping: [localhost]

TASK: [is_centos6] ************************************************************
ok: [localhost] => {
“msg”: “SUCCESS is_centos6”
}

TASK: [is_suse or is_centos5 t1] **********************************************
ok: [localhost] => {
“msg”: “FAIL is_suse or is_centos5”
}

TASK: [is_suse or is_centos5 t2] **********************************************
skipping: [localhost]

TASK: [is_suse or is_centos5 t3] **********************************************
skipping: [localhost]

TASK: [is_suse or is_centos6 t1] **********************************************
ok: [localhost] => {
“msg”: “SUCCESS is_suse or is_centos6”
}

TASK: [is_suse or is_centos6 t2] **********************************************
skipping: [localhost]

TASK: [is_suse or is_centos6 t3] **********************************************
ok: [localhost] => {
“msg”: “SUCCESS is_suse or is_centos6”
}

PLAY RECAP ********************************************************************
localhost : ok=6 changed=0 unreachable=0 failed=0

Ok, so a lot going on here so I’m going to answer the parts only about things that are valid and documented syntax:

(A) I don’t see why you say the CentOS 5 test failed because you said it was a CentOS 6 host and you skipped it. This works fine.

(B) So the problem or question is really about this one only:

TASK: [is_suse or is_centos5 t1] **********************************************
ok: [localhost] => {
“msg”: “FAIL is_suse or is_centos5”
}

In this case, if you were to define “is_suse_or_centos_5” it would work fine.

As a reminder, the syntax used was:

when: is_suse or is_centos5

This looks like a minor issue with variable recursion. Please file a github on this one and we’ll take a look. http://github.com/ansible/ansible – you can link to this ticket if you like.

The other forms about using “{{ }}” in the conditional are deprecated/troublesome syntax (and we’ve intentionally reduced the conditional system to not need these), so I’m not going to respond to those and concentrate on the problem that led to the experimentation if that’s ok!

Thanks!