Ansible 2.0 variable precedence using an inventory file

55 views
Skip to first unread message

Joe Jasinski

unread,
Aug 15, 2016, 10:19:27 AM8/15/16
to Ansible Project
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


James Cammarata

unread,
Aug 17, 2016, 11:12:28 AM8/17/16
to ansible...@googlegroups.com
Generally the best place to put global vars you want to override is in role defaults. These have the lowest priority and can be very easily overridden by just about any other variable source, including inventory variables (so I'm not sure what problem you're running into there). You should be able to specify the `db_user` var in either group or host vars, or in params set on the role as it's being run (or even --extra-vars, in the extreme case).

James Cammarata

Ansible Lead/Sr. Principal Software Engineer
Ansible by Red Hat
twitter: @thejimic, github: jimi-c

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/29b46d5d-c57d-4482-a711-61d5928c96e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages