Skip to content

Instantly share code, notes, and snippets.

@towo
Last active May 16, 2019 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save towo/a02aaf0234332c38136f42f94663b911 to your computer and use it in GitHub Desktop.
Save towo/a02aaf0234332c38136f42f94663b911 to your computer and use it in GitHub Desktop.
OS-dependent default-precedence variables for ansible roles
# There's another method I spotted in trombik/ansible-role-dovecot. In the playbook, do this:
- name: Include OS-specific variables
include_vars: "{{ ansible_os_family }}.yml"
# In {{ ansible_os_family }}.yml:
__rolename_variable: some_value
# In defaults/main.yml:
rolename_variable: "{{ __rolename_variable }}"
# This also works and might be your preferred choice if you don't like overly long lines.
# Credit goes to mgedmin on freenode/#ansible for the idea.
# We use the base variable with a fixed suffix to denote the case switch.
# You should probably use something universal so that you can easily regexp
# away existing configuration.
some_variable_os:
Debian: foo
Redhat: bar
default: baz
# Pull the value of some_variable_os for the current os family, with a defined fallback.
# Technically, you could also write a jinja filter that does the grunt work for you, but
# depending on what readability/maintaniblity requirements you assume, that might be extra
# work.
some_variable: "{{ some_variable_os[ansible_os_family] | default(some_variable_os['default']) }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment