23

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

3 Answers 3

34

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.

2
  • response I get from ansible_distribution_major_version is "Hello world!" using ansible 2.6.0
    – lobi
    Nov 7, 2019 at 22:10
  • 7
    just for completeness if someone wants to get the release name e.g. Bionic: ansible_distribution_release
    – Markus
    Nov 21, 2019 at 9:07
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 }}"
4
  • 6
    It is much better if you use the gather_facts: True instead of running shell commands.
    – Robert
    Jun 17, 2019 at 17:33
  • 3
    As advised by @Robert, this is not the ansible way of doing this kind of task. Jun 17, 2019 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, 2021 at 12:38
  • They asked specifically for the ansible_distribution_version, not output from a shell command. Apr 7, 2021 at 13:57
0

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 }}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.