Hi Patrick,
I'm using
the
https://github.com/lukaspustina/dynamic-inventory-for-ansible-with-openstack
dynamic inventory script, specifically for obtaining inventory from C3. You
can use it as a reference if you like.
I'm still using the directory structure I posted above, with group_vars
inside each env. The "defaults" group is the special one that I use to
replace Ansible's normal "all" group, except that I get expected override
precedence. I modified the dynamic inventory script to load the
non-env-specific defaults file and apply them like this:
def read_default_vars(self):
c = ConfigParser.ConfigParser()
c.read("env/defaults")
section = "all"
result = {}
for key in c.options(section):
result[key] = c.get(section, key)
return result
def build_defaults_parent_group(self):
groups = list(self.inventory().iterkeys())
groups.remove("_meta")
default_vars = self.read_default_vars()
self.inventory()['defaults'] = {
'hosts': [],
'children': groups,
'vars': default_vars}
That way the env-specific defaults file (e.g. env/dev/group_vars/defaults)
will override any vars set at env/defaults.
It is still awkward. I wish Ansible had a built-in solution.
On Wednesday, March 25, 2015 at 10:13:44 AM UTC-6, Patrick Martini wrote:
>
> Hi Trevor,
>
> can you share your dynamic inventory script with us ? it looks like the
> only way to have site-wide defaults.
>
> Thanks
> Patrick
>
> On Monday, December 22, 2014 at 7:18:27 PM UTC+1, Trevor Hartman wrote:
>>
>> Thanks for posting Azul. It's interesting to see another way of handling
>> it. I ended up with this structure, which I'm liking a lot:
>>
>> env
>> ├── defaults
>> ├── dev
>> │ ├── group_vars
>> │ │ ├── backend
>> │ │ ├── defaults
>> │ │ ├── ingest
>> │ │ ├── monitoring
>> │ │ └── zookeeper
>> │ └── inventory.py
>> └── production
>> ├── group_vars
>> │ ├── backend
>> │ ├── defaults
>> │ ├── ingest
>> │ ├── monitoring
>> │ └── zookeeper
>> └── inventory.py
>>
>> 1. I completely eschew Ansible's "all" supergroup because precedence was
>> not behaving as I expected.
>> 2. I use a dynamic inventory script which adds all my groups to a
>> "defaults" supergroup. This is my "all" replacement, so env/defaults is my
>> new group_vars/all. The script also manually reads in env/defaults and sets
>> the vars in the JSON output.
>> 3. env/defaults can specify site-wide defaults which can be overridden by
>> env/dev/group_vars/defaults. There's a third level of specificity at
>> env/dev/group_vars/$group, e.g. env/dev/group_vars/zookeeper, which would
>> override anything in env/dev/group_vars/defaults.
>>
>> I'd really like to see Ansible create an official solution to this common
>> problem. It'd take the burden off devs and increase uniformity in Ansible
>> automation.
>>
>> One awkward piece of my setup is having to set all my vars at the
>> inventory level, when much of it belongs at the playbook level.
>>
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/58749303-a004-4ede-bf8d-6407b6a4b9a5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.