# Dynamic inventory and group vars

**URL:** <https://forum.ansible.com/t/dynamic-inventory-and-group-vars/17340>\
**Category:** Ansible Project\
**Created:** [January 21, 2015, 4:20pm UTC](https://forum.ansible.com/t/dynamic-inventory-and-group-vars/17340 "2015-01-21T16:20:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niko\_Felger](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Niko\_Felger](https://forum.ansible.com/u/Niko_Felger)\
**Post date:** [January 21, 2015, 4:20pm UTC](https://forum.ansible.com/t/dynamic-inventory-and-group-vars/17340/1 "2015-01-21T16:20:05Z")

</div>

Hi,

I’ve been using ansible for half a year and have been really happy with it so far, so first of all: thanks for all the great work on this project!

We’re moving from static to dynamic inventory, but I’m having trouble with loading group\_vars and haven’t been able to find out from the docs how these are looked up, so I hope someone can help me out here.

In the static setup we had one inventory file per environment (dev/staging/prod), and a group\_vars directory where we set variables per role as well as per environment based on their group membership.

The static directory structure looks like this:

`

inventories  
├── development  
├── group\_vars  
│ ├── all.yml  
│ ├── web.yml  
│ ├── worker.yml  
│ ├── development  
│ ├── secrets.yml  
│ └── vars.yml  
│ ├── production  
│ ├── secrets.yml  
│ └── vars.yml  
│ └── staging  
│ ├── secrets.yml  
│ └── vars.yml  
├── production  
└── staging

`

When I run e.g.:

`ansible -i inventories/staging web -m debug -a 'msg="{{hostvars}}"'`

the results contain all the relevant vars for the groups ‘all’, ‘web’ and ‘staging’.

Things I like about this setup:

- you have to explicitly pick an environment to target, which makes accidents more difficult;
- production and staging inventories are completely separate, again making mistakes harder;
- having a single place where we can set some variables for all hosts and override them and add more, by role (web, worker, etc) and by environment.

The new mixed inventory setup looks like this (staging and production are now directories, maybe that’s part of the issue?):

`

inventories  
├── development  
├── group\_vars  
│ ├── all.yml  
│ ├── web.yml  
│ ├── worker.yml  
│ ├── development  
│ │ ├── secrets.yml  
│ │ └── vars.yml  
│ ├── grabber.yml  
│ ├── production  
│ │ ├── secrets.yml  
│ │ └── vars.yml  
│ └── staging  
│ ├── secrets.yml  
│ └── vars.yml  
├── production  
│ ├── base  
│ ├── ec2.ini  
│ └── ec2.py  
└── staging  
├── base  
├── ec2.ini  
└── ec2.py

`

The output of  
`ansible -i inventories/staging/ web -m debug -a 'msg="{{hostvars}}"'`  
now doesn’t include any of the group vars. The group\_vars directory doesn’t seem to be loaded or applied at all.

Any ideas how I can structure this to achieve what we had previously but with mixed inventory?

Thanks!  
Niko

PS: In case you’re wondering, the two ec2.py scripts are identical and the ec2.ini’s only differ in their cache\_path and instance\_filters (selecting instances tagged with env:production/staging, respectively).

Our mailing address is: WhiteMatter Labs GmbH | Reichenberger Str. 124, 10999 Berlin  
Handelsregister: Amtsgericht Charlottenburg, HRB 162605 B | Geschäftsführer: Fabian Stelzer
