Using group_vars alongside inventory directories

86 views
Skip to first unread message

Niko Felger

unread,
Jan 22, 2015, 10:34:41 AM1/22/15
to ansible...@googlegroups.com
Hi,

Is it possible to use a group_vars/ directory alongside inventory directories (as opposed to files)?

Take this simplified example, which has an inventory file (staging) and an inventory directory (production):

$ tree
.
└── inventories
    ├── group_vars
    │   └── all.yml
    ├── production
    │   └── hosts
    └── staging

staging and production/hosts both contain a single host:

[localhost]
127.0.0.1

group_vars/all.yml defines a variable example_var:

---
example_var: present

Running the staging inventory file picks up group_vars:

$ ansible -i inventories/staging all -m debug -a 'msg="{{hostvars}}"'
127.0.0.1 | success >> {
    "msg": "{'inventory_hostname': '127.0.0.1', 'example_var': 'present', 'inventory_hostname_short': '127', 'group_names': ['localhost']}"
}

But running the production inventory directory doesn't pick them up (no example_var in the output):

$ ansible -i inventories/production/ all -m debug -a 'msg="{{hostvars}}"'
127.0.0.1 | success >> {
    "msg": "{'inventory_hostname': '127.0.0.1', 'inventory_hostname_short': '127', 'group_names': ['localhost']}"
}

How can I have the group_vars be picked up without having to duplicate them under inventories/production/group_vars?

I want to use an inventory directory to mix static and dynamic inventory, with one inventory directory per environment (dev/stage/prod), and some shared vars. Maybe there's a better way to do this though?

Thanks!
Niko


PS: I asked a similar (and quite long-winded) question yesterday. Since I didn't get an answer, I thought I'd come up with a more specific formulation, I hope that's fine! I'm new to this list -- if there's anything I can do to make answering my questions easier, please let me know!

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

Tomasz Kontusz

unread,
Jan 22, 2015, 11:28:10 AM1/22/15
to ansible...@googlegroups.com
group_vars/ should live inside production/. I don't know any simple way to share those vars (you can always symlink some of them)

Niko Felger <ni...@eyequant.com> napisał:

--
Wysłane za pomocą K-9 Mail.

Niko Felger

unread,
Jan 22, 2015, 11:43:37 AM1/22/15
to ansible...@googlegroups.com
Perfect, thanks Tomasz! This did the trick for me:

$ tree
.
└── inventories
    ├── group_vars
    │   └── all.yml
    ├── production
    │   ├── group_vars -> ../group_vars
    │   └── hosts
    └── staging
Reply all
Reply to author
Forward
0 new messages