As I have separate “staging” and “production” inventory files, I end up duplicating some group compositions (e.g. databases:children is mysql:children and redis:children, that kind of thing). I’m wondering if there’s a way to consolidate that logic, such as a directive to include a file. Or maybe some trick I’m missing here.
If you’re dealing with any significant number of hosts (ie. more than a hundred), we generally recommend using a dynamic inventory script of some kind. Beyond that, the INI syntax doesn’t really allow for any includes unfortunately.
I do the per environment inventories too (partly because they're
datacenter/vagrant)
and had a few groups just to target one-off plays.
My per-environment settings usually tuck into
[all:vars]
in the inventory - I've no idea how I'm going to map that to a dynamic inventory
yet, probably a lot of set_fact roles to set everything up.
I keep them in step by diffing
Re: the group composition - I needed that for some one-off plays, it
turns out you can target multiple groups in a play with something like
.....
- hosts: mysql:redis
sudo: yes
tasks:
.....
Don’t do “all:vars”, use a “group_vars/all” file instead.
Not only is it cleaner for a static inventory, but it’s usable with dynamic ones as well.
Yeah we're about to try some of the cloud providers, I know this is
going to bite us.
problem with group_vars/all is that it's 'in the tree' - I'm trying to
find a way to
have preprod_hosts and production_hosts contain everything environment-specific,
so we can ensure roles/ group_vars/ etc. don't vary across different
environments.
“problem with group_vars/all is that it’s ‘in the tree’ - I’m trying to
find a way to”
You can keep this alongside your inventory (which most people do) rather than alongside your playbook, if you like.
Ah, mine are all lumped in together, like this:
ansible-mystack:$ tree -L 1
.
├── Vagrantfile
├── ansible.cfg
├── group_vars
├── preprod_hosts
├── prod_hosts
├── roles
├── site.yml
└── vagrant_hosts
Are you talking about having a group_vars per inventory then?
Trying to figure out how that would look.