Specifying the OS - Ansible

23,572

The normal approach to this is to have an OS family specific task file that is conditionally included by checking the ansible_os_family fact.

So you may have a main.yml task file in your role that looks something like:

# Arbitrary task here, not needed but the point is you can have any generic tasks directly in main.yml
- name: get the date
  shell: `date`
  register: date

- include: debian.yml
  when: ansible_os_family == 'Debian'

- include: redhat.yml
  when: ansible_os_family == 'RedHat'

And then in debian.yml we have:

- name: install requirements
  apt: name={{item}} state=latest update_cache=true
  with_items:
      - gcc
      - python-dev
      - python-setuptools
      - python-software-properties

and in redhat.yml we have:

- name: install requirements
  yum: name={{item}} state=latest update_cache=true
  with_items:
      - gcc
      - python-dev
      - python-setuptools
      - python-software-properties

Obviously this allows you to set different dependency lists depending on the OS family as well.

If you wanted to you could also conditionally include OS family (or really anything you can check a fact for) specific vars like this:

- name: Include OS-specific variables.
  include_vars: "{{ item }}"
  with_first_found:
    - ../vars/{{ ansible_distribution | lower }}.yml
    - ../vars/{{ ansible_os_family | lower }}.yml

And then set your dependency lists in say vars/debian.yml like this:

python_dependencies:
  - gcc
  - python-dev
  - python-setuptools
  - python-software-properties

so now your tasks/debian.yml looks like:

- name: install requirements
  apt: name={{item}} state=latest update_cache=true
  with_items: python_dependencies

You can see a list of the OS's and their families by checking the source code here which has this dict of all the OS families:

# A list with OS Family members
OS_FAMILY = dict(
    RedHat = 'RedHat', Fedora = 'RedHat', CentOS = 'RedHat', Scientific = 'RedHat',
    SLC = 'RedHat', Ascendos = 'RedHat', CloudLinux = 'RedHat', PSBM = 'RedHat',
    OracleLinux = 'RedHat', OVS = 'RedHat', OEL = 'RedHat', Amazon = 'RedHat',
    XenServer = 'RedHat', Ubuntu = 'Debian', Debian = 'Debian', Raspbian = 'Debian', Slackware = 'Slackware', SLES = 'Suse',
    SLED = 'Suse', openSUSE = 'Suse', SuSE = 'Suse', SLES_SAP = 'Suse', Gentoo = 'Gentoo', Funtoo = 'Gentoo',
    Archlinux = 'Archlinux', Manjaro = 'Archlinux', Mandriva = 'Mandrake', Mandrake = 'Mandrake',
    Solaris = 'Solaris', Nexenta = 'Solaris', OmniOS = 'Solaris', OpenIndiana = 'Solaris',
    SmartOS = 'Solaris', AIX = 'AIX', Alpine = 'Alpine', MacOSX = 'Darwin',
    FreeBSD = 'FreeBSD', HPUX = 'HP-UX'
)
Share:
23,572
cybertextron
Author by

cybertextron

just me.

Updated on November 18, 2020

Comments

  • cybertextron
    cybertextron over 3 years

    I'm a newbie in Ansible, so I wrote a little ansible utility to install some package dependencies for a system I'm writing:

    ---
    
    - hosts: all
      user: root
      tasks:
          - name: install requirements
            apt: name={{item}} state=latest update_cache=true
            with_items:
                - gcc
                - python-dev
                - python-setuptools
                - python-software-properties
    

    The current supported environments are Ubuntu, Red Hat and Mac OS X. The current way this playbook is written it will only work in Ubuntu (Debian). How can I have that part of the code be executed according to the OS? For Ubuntu it's apt, for Red Hat it's yum and for Mac OS X brew.

  • ydaetskcoR
    ydaetskcoR over 8 years
    I don't have a mac so can't test it but if you run ansible -i "mac-host," all -m setup (where "mac-host" is the name of your OS X box) this will give all the facts about your "mac-host" machine that will hopefully give you some fact you can use to discern between it and another OS.
  • Syed Danish Ali
    Syed Danish Ali over 7 years
    On OS X the ansible_os_family is Darwin
  • pentavalentcarbon
    pentavalentcarbon over 3 years
    An updated mapping of family name to distributions is here.
  • ydaetskcoR
    ydaetskcoR over 3 years
    @pentavalentcarbon feel free to suggest an edit to the answer with the updated OS_FAMILY dict.