Thanks to Hagai I have a good solution worth sharing.

* add common_vars.sh bash script to each of the inventories I wish to share 
variables. Make it executable. Point it where your common Ansible YAML 
variables are (in my case inventory/common_vars.yml).
* add common_vars.yml and populate it

Run your playbooks with the specified inventory path as normal 
(inventory/aws-dev, inventory/aws-prod etc)

Benefits
* inventory files are all kept in the inventory folder
* normal Ansible group_vars are left alone
* only need one extra file per inventory, and no copy paste
* if you need a few top level common_vars, you can just point your 
common_vars to onprem_common.yml and cloudstack_common.yml, or as you like.
* feels like a proper Ansible way to do things without hacks

New dir structure below (compared to original). Bash script code after.

.
├── inventory
│   ├── common_vars.yml
│   ├── aws-dev
│   │   ├── aws-dev
│   │   ├── common_vars.sh
│   │   ├── ec2.ini
│   │   ├── ec2.py
│   │   └── group_vars
│   │       ├── all
│   │       │   ├── all.yml
│   │       │   └── secrets.yml
│   │       ├── security_group_app.yml
│   │       └── security_group_util
│   │           ├── secrets.yml
│   │           └── security_group_util.yml
│   ├── aws-prod
│   │   ├── aws-prod
│   │   ├── common_vars.sh
│   │   ├── ec2.ini
│   │   ├── ec2.py
│   │   └── group_vars
│   │       └── all
│   │           ├── all.yml
│   │           └── secrets.yml
│   └── vmware-dev
│       ├── group_vars
│       │   ├── all
│       │   │   ├── all.yml
│       │   │   └── secrets.yml
│       │   └── tag_role_app.yml
│       ├── common_vars.sh
│       ├── vmware-dev
│       ├── vmware.ini
│       └── vmware.py
├── roles
├── aws-configure.yml
├── aws-provision.yml
├── vmware-configure.yml
└── vmware-provision.yml

common_vars.sh
#!/usr/bin/env bash

# 
http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# echo $DIR
# 
http://stackoverflow.com/questions/27382552/converting-yaml-to-json-with-python-block-end-found
python -c 'import sys, yaml, json; json.dump({"all": {"vars": 
yaml.load(sys.stdin) } }, sys.stdout, indent=4)' < $DIR/../common_vars.yml 
# > all.json

This accomplishes everything required without complexity.

Thanks Hagai!

On Friday, June 26, 2015 at 7:52:28 PM UTC+1, Hagai Kariti wrote:
>
> You need to wrap it in an 'all' group, yes. The output should look like 
> this:
>
>    {
>       "all": {
>          "vars": {
>             ..your vars here..
>          }
>       }
>    }
>
> And templates are evaluated when used, so the vars can be templated.
>

-- 
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/c21d0da1-f461-4ff6-8a5a-dd49f87b17de%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to