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