Last active
May 16, 2019 09:42
-
-
Save towo/a02aaf0234332c38136f42f94663b911 to your computer and use it in GitHub Desktop.
OS-dependent default-precedence variables for ansible roles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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