Ansible Determine Operating System

12

2

As part of my deploy script I wanna check which operating system I am deploying to. I used ansible localhost -m setup and as they say in the documentation this outputs a lot. Is there a way I can just access the ubuntu distro I am using? Ideally I want to find if the box is running Trusty or Precise

ford prefect

Posted 2015-12-08T22:58:46.053

Reputation: 227

What about lsb_release? – Xen2050 – 2015-12-17T13:58:24.703

cat /etc/*release is what I use, for any Linux system. – qasdfdsaq – 2015-12-17T15:34:44.290

@Xen2050 In this case I think it makes more sense to use ansible's built in functionality which probably calls what you both mention to set the fact – ford prefect – 2015-12-17T16:06:54.923

Answers

22

ansible_distribution_release

The fact is called ansible_distribution_release. If you are running Ubuntu 14.04, the fact would read "trusty".

Two other example values: ansible_distribution_release would be "xenial" for Ubuntu 16.04 and "precise" for Ubuntu 12.04.

ansible_distribution_version

You can also look at the fact ansible_distribution_version. For Ubuntu 14.04, you would see "14.04".

Two other example values: ansible_distribution_version would be "16.04" for Ubuntu 16.04 and "12.04" for Ubuntu 12.04.

Here is an example task that you could put into a playbook to install the build-essential package only on Ubuntu 14.04:

- name: Install build-essential for Ubuntu 14.04 only
  apt: name=build-essential state=present
  when: ansible_distribution_version == "14.04"

Deltik

Posted 2015-12-08T22:58:46.053

Reputation: 16 807