Ansible playbook to determine OS release

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.

Tags:

Ansible