Ansible playbook to determine OS release

3

1

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

Posted 2019-01-18T23:27:16.577

Reputation: 387

Answers

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

Posted 2019-01-18T23:27:16.577

Reputation: 387

2It is much better if you use the gather_facts: True instead of running shell commands. – Robert – 2019-06-17T17:33:22.773

As advised by @Robert, this is not the ansible way of doing this kind of task. – Zeitounator – 2019-06-17T18:33:50.230

3

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

Posted 2019-01-18T23:27:16.577

Reputation: 181

response I get from ansible_distribution_major_version is "Hello world!" using ansible 2.6.0 – lobi – 2019-11-07T22:10:08.243

just for completeness if someone wants to get the release name e.g. Bionic: ansible_distribution_release – Markus – 2019-11-21T09:07:01.740