I’m writing an ansible playbook for installing jdk.
The logic is to use version 8 if the system is "Ubuntu", but 1.8.0 if it is "CentOS".
following is my code:
- hosts: all
vars:
- java_open_jdk_version_major: 8
when: ansible_distribution == 'Ubuntu'
- java_open_jdk_version_major: 1.8.0
when: ansible_distribution == 'CentOS'
roles:
- name: jdk
In this way, the “java_open_jdk_version_major” always becomes 1.8.0.
How to define variables in this case?
2
Answers
you do something like this:
The reason why you always end up with version as "1.8.0", is because your variables definition is a list and
when
conditionals are not doing anything there.When
java_open_jdk_version_major
is evaluated first, its value is "8", but another definition is found below, which is "1.8.0", and lastly evaluated value wins.Another way to achieve this, and ensure that appropriate versions are set for the respective Distribution (
CentOS
andUbuntu
) is to use apre_tasks
section, like so: