36

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

nadermx
  • 873
  • 2
  • 17
  • 41

3 Answers3

53

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

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
  • 873
  • 2
  • 17
  • 41