Specifying the OS - Ansible
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'
)
![cybertextron](https://i.stack.imgur.com/u0SVy.jpg?s=256&g=1)
Comments
-
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
andMac OS X
. The current way this playbook is written it will only work inUbuntu (Debian)
. How can I have that part of the code be executed according to the OS? ForUbuntu
it'sapt
, forRed Hat
it'syum
and forMac OS X
brew
. -
ydaetskcoR over 8 yearsI 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 over 7 yearsOn
OS X
theansible_os_family
isDarwin
-
pentavalentcarbon over 3 yearsAn updated mapping of family name to distributions is here.
-
ydaetskcoR over 3 years@pentavalentcarbon feel free to suggest an edit to the answer with the updated OS_FAMILY dict.