I'm simply trying to check the version of ubuntu on all my servers. Based on this question I see that ansible has a ansible_distribution_version but this playbook does not show how I would simply just have it print out the ubuntu version, ie ubuntu 14.04, 16.04, etc
Asked
Active
Viewed 1e+01k times
3 Answers
40
You can do one at the time
---
- hosts: localhost
gather_facts: yes
become: false
tasks:
- name: Distribution
debug: msg="{{ ansible_distribution }}"
- name: Distribution version
debug: msg="{{ ansible_distribution_version}}"
- name: Distribution major version
debug: msg="{{ ansible_distribution_major_version }}"
See the results:
PLAY [localhost] ***********************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [localhost]
TASK [Distribution] ********************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Ubuntu"
}
TASK [Distribution version] ************************************************************************************************************************************************
ok: [localhost] => {
"msg": "18.04"
}
TASK [Distribution major version] ******************************************************************************************************************************************
ok: [localhost] => {
"msg": "18"
}
PLAY RECAP *****************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
Or you can use a more advance configuration iterating facts:
- hosts: localhost
gather_facts: yes
become: false
tasks:
- name: System details
debug: msg="{{ item }}"
with_items:
- "{{ ansible_distribution }}"
- "{{ ansible_distribution_version }}"
- "{{ ansible_distribution_major_version }}"
And a more compact results:
PLAY [localhost] ***********************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [localhost]
TASK [System details] ******************************************************************************************************************************************************
ok: [localhost] => (item=Ubuntu) => {
"msg": "Ubuntu"
}
ok: [localhost] => (item=18.04) => {
"msg": "18.04"
}
ok: [localhost] => (item=18) => {
"msg": "18"
}
PLAY RECAP *****************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
In both cases is a good practices to get the info using facts instead of shell or command modules.
Robert
- 606
- 7
- 9
-
response I get from ansible_distribution_major_version is "Hello world!" using ansible 2.6.0 – lobi Nov 07 '19 at 22:10
-
7just for completeness if someone wants to get the release name e.g. Bionic: `ansible_distribution_release` – Markus Nov 21 '19 at 09:07
1
After very research to find out Ubuntu 20.04 version then we have got released a version using ansible version-2.5.1
- hosts: localhost
become: true
gather_facts: yes
tasks:
- name: System details
debug:
msg: "{{ ansible_facts['lsb']['release'] }}"
- name: ubuntu 18
shell: echo "hello 18"
register: ub18
when: ansible_facts['lsb']['release'] == "18.04"
- debug:
msg: "{{ ub18 }}"
- name: ubuntu 20
shell: echo "hello 20"
register: ub20
when: ansible_facts['lsb']['release'] == "20.04"
- debug:
msg: "{{ ub20 }}"
user3710949
- 11
- 1
0
I got it, to check the OS version here is the playbook
---
- hosts: all
gather_facts: False
tasks:
- name: Check Dist Version
shell: cat /etc/os-release
register: response
- debug: msg="{{ response.stdout }}"
nadermx
- 667
- 1
- 10
- 32
-
7It is much better if you use the ```gather_facts: True``` instead of running shell commands. – Robert Jun 17 '19 at 17:33
-
3As advised by @Robert, this is not the ansible way of doing this kind of task. – Zeitounator Jun 17 '19 at 18:33
-
the downvoted answer (not by me) and, curiously, it is the only one that worked for me, the others fail, which seems to be related to an Ansible 2.9 bug... – xCovelus Feb 26 '21 at 12:38
-
They asked specifically for the ansible_distribution_version, not output from a shell command. – DevOpsSauce Apr 07 '21 at 13:57
-
Note that ansible already parses this file for you: `"distribution_file_path": "/etc/os-release",` – Clément Nov 14 '22 at 22:17