Hi all,
I have a set of Ansible configuration that I intend to use as a Generic template for setting up a Django webserver and related services. This project contains a multitude of roles for configuring various aspects optional services (Nginx, Django, Redis, Postgres, etc.).
I'm trying to find a good way to specify good global default, overridable variables that can be used throughout all of my roles. For example, I'd like to define variables like "app_user" or "app_name" (and many others) that I can use in all of my roles.
Right now, these variables are placed at the top level of my project inside of env_vars/base.yml. I'm thinking that this would be my global default configuration file that would define all of the defaults throughout the project and inform users of what settings are available to override.
Also, the role defaults that I have defined in each role read in variables set in the env_vars/base.yml (my global defaults file), and set local copies of those variables for use in that role's tasks.
# contents of roles/postgres/defaults/main.yml
---
postgres__db_user: "{{ db_user }}"
In that example, that global "db_user" might be used in several roles. I like this approach because it's very DRY and modular; the global 'db_user' variable sets the namespaced local 'postgres__db_user' variable inside of the postgres role, but can be namespaced differently in other roles.
What I'd love to be able to do is use the inventory file to override the global defaults. The inventory file seems like a good place to put variables that would customize the provisioning of specific hosts.
However, the problem is that the inventory file is one of the things that takes least precedence in the variable precedence hierarchy.
(I'm a bit confused by this because. I'd think the inventory file, being the thing that is most likely be used to create host-specific configurations, would take a high precedence.)
I'm wondering if there is a better way to organize my project such that I can set some global defaults that can be the defaults for all roles, retain modular roles with the namespaced variables, but be able to cleanly configure any non-defaults in the inventory.
Does anyone know how I might achieve this or know of any other best practices for situations like this?
Note: I'm currently using the --extra-vars command line switch to pass in variables that I want to override, but this makes for really long command line executions.
Here is my project thus far to see a working example:
Thanks for any advice,
Joe