In an existing playbook of mine, so far I have the following:
vars:
is_ubuntu: “‘$ansible_distribution’ == ‘Ubuntu’”
is_rh: “‘$ansible_distribution’ == ‘Fedora’ or ‘$ansible_distribution’ == ‘Scientific’ or ‘$ansible_distribution’ == ‘CentOS’”
earlier_than_precise: “‘$ansible_distribution’ == ‘Ubuntu’ and ‘$ansible_distribution_version’ < ‘12.04’”
precise_or_later: “‘$ansible_distribution’ == ‘Ubuntu’ and ‘$ansible_distribution_version’ >= ‘12.04’”
Here is a task using the “old” only_if:
- name: Ubuntu - Create an account for Test User
user: name=tset comment=‘Test User’ shell=/bin/bash createhome=yes password=$tset_password
only_if: ‘$is_ubuntu’
- name: Ubuntu older than precise- Add Test User to the admin group
user: name=tset groups=admin append=yes
only_if: ‘$earlier_than_precise’
- name: Ubuntu precise and later - Add Test User to the sudo group
user: name=tset groups=sudo append=yes
only_if: ‘$precise_or_later’
To update my playbook, I changed the vars section to the following:
vars:
Of course, if we use the real Debian, we can expand the following.
is_ubuntu: “{{ ansible_distribution }}’ == ‘Ubuntu’”
is_rh: “‘{{ ansible_distribution }}’ == ‘Fedora’ or ‘{{ ansible_distribution }}’ == ‘Scientific’ or ‘{{ ansible_distribution }}’ == ‘CentOS’”
earlier_than_precise: “‘{{ ansible_distribution }}’ == ‘Ubuntu’ and ‘{{ ansible_distribution_version }}’ < ‘12.04’”
precise_or_later: “‘{{ ansible_distribution }}’ == ‘Ubuntu’ and ‘{{ ansible_distribution_version }}’ >= ‘12.04’”
and the task to the following:
- name: Ubuntu - Create an account for Test User
user: name=tset comment=‘tset’ shell=/bin/bash createhome=yes password={{ tset_password }}
when: is_ubuntu
- name: Ubuntu older than precise - Add tset to the admin group
user: name=tset groups=admin append=yes
when: earlier_than_precise
- name: Ubuntu precise or later - Add tset to the sudo group
user: name=tset groups=sudo append=yes
when: precise_or_later
Testing the above, even on a Ubuntu box running 11.04, the last task still got executed. I must have misunderstood the documentation somehow. Any hints are appreciated.
– Zack
" precise_or_later: “‘{{ ansible_distribution }}’ == ‘Ubuntu’ and ‘{{ ansible_distribution_version }}’ >= ‘12.04’”
Are you intending to do a string comparison with a greater than sign?
The when statement you want is:
when: ansible_distribution == ‘Ubuntu’ and ansible_distribution_version >= ‘12.04’
Thus, if you want to save that statement, I believe you don’t want to put the handlebars in the saved expression.
Also, “when: precise_or_later” is a string.
You would want:
when: “{{ precise_or_later }}”
but should of course first fix the “precise_or_later” expression you are saving, as it doesn’t need the handle bars in it.
Hi Michael,
Also, “when: precise_or_later” is a string.
You would want:
when: “{{ precise_or_later }}”
but should of course first fix the “precise_or_later” expression you are saving, as it doesn’t need the handle bars in it.
Thanks for your tips. Below is a simple playbook that I created:
Hello,
- name: RH - announce the hostname
shell: /bin/hostname
when: "{{ is_rh }"
You have a typo here. It should be:
when: "{{ is_rh }}"
- name: Ubuntu < 12.04 - announce the hostname
shell: /bin/hostname
when: "{{ earlier_than_precise }"
And here too.
BTW, the following works. But this doesn’t help my case since I would like to re-use variables defined in the vars section rather than repeat these verbose logic conditions for each and every task:
Actually I got the above wrong, you need both:
when: “{{ saved_expression }}”
AND the {{ }} around the variables in the saved expression.
Hi Krzysztof,
Thanks for chipping in.
Hello,
- name: RH - announce the hostname
shell: /bin/hostname
when: “{{ is_rh }”
You have a typo here. It should be:
when: “{{ is_rh }}”
- name: Ubuntu < 12.04 - announce the hostname
shell: /bin/hostname
when: “{{ earlier_than_precise }”
And here too.
Fixing these doesn’t seem to be sufficient. The following is the fixed playbook:
Actually I got the above wrong, you need both:
when: “{{ saved_expression }}”
AND the {{ }} around the variables in the saved expression.
I took the above tips, and came up the following (back to square one?)
I’ll see about testing this and giving you an answer later.
Until then, I highly recommend just using group_by, as it produces superior output for slicing up host by OS type, etc.
OK, I think you have found a bug. Please consider this playbook:
This isn’t a bug.
When you have a “when” statement, it should look like this:
“when: expression”
Not "when: {{ expression }} "
The whole point was to eliminate the need of the template expressions inside the conditional.
To restate a bit, there may of course be a problem referencing previous conditionals as variables.
Which I’ve already mentioned to Zack I’m going to look into it