# Problem with variables

**URL:** <https://forum.ansible.com/t/problem-with-variables/13828>\
**Category:** Ansible Project\
**Created:** [November 2, 2013, 2:00am UTC](https://forum.ansible.com/t/problem-with-variables/13828 "2013-11-02T02:00:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Callahan](https://avatars.discourse-cdn.com/v4/letter/p/9e8a1a/32.png) [@Patrick\_Callahan](https://forum.ansible.com/u/Patrick_Callahan)\
**Post date:** [November 2, 2013, 2:00am UTC](https://forum.ansible.com/t/problem-with-variables/13828/1 "2013-11-02T02:00:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 2, 2013, 3:03am UTC](https://forum.ansible.com/t/problem-with-variables/13828/2 "2013-11-02T03:03:24Z")

</div>

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](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!
