# group variables

**URL:** <https://forum.ansible.com/t/group-variables/12736>\
**Category:** Ansible Project\
**Created:** [April 29, 2013, 2:25pm UTC](https://forum.ansible.com/t/group-variables/12736 "2013-04-29T14:25:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [April 29, 2013, 2:25pm UTC](https://forum.ansible.com/t/group-variables/12736/1 "2013-04-29T14:25:14Z")

</div>

If I have a separate host file for production and testing, can I do something like:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[all:vars]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dbpassword=blah

Right now we set an environment variable with our env (prod/testing), then import a var file with vars\_file based on that variable.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [April 29, 2013, 2:38pm UTC](https://forum.ansible.com/t/group-variables/12736/2 "2013-04-29T14:38:55Z")

</div>

you can just have a group\_vars/all

that has global variables

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [April 29, 2013, 2:40pm UTC](https://forum.ansible.com/t/group-variables/12736/3 "2013-04-29T14:40:54Z")

</div>

sorry, hit enter too early.

you can just have a group\_vars/all

that has global variables, and have a inventory file per enviornment so:

-i /etc/ansible/prod/inventory (with /etc/ansible/prod/group\_vars/all)  
vs  
-i /etc/ansible/staging/inventory (with /etc/ansible/staging/group\_vars/all)

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [April 29, 2013, 3:06pm UTC](https://forum.ansible.com/t/group-variables/12736/4 "2013-04-29T15:06:30Z")

</div>

So in this case, will /etc/ansible/group\_vars/all still be loaded for both prod and staging?

What about roles, can I keep my roles directory in /etc/ansible, or I have to have a different one prod and staging?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [April 29, 2013, 3:40pm UTC](https://forum.ansible.com/t/group-variables/12736/5 "2013-04-29T15:40:44Z")

</div>

no, if you specify different inventory paths you won't also get the  
'default' inventory.

Another option is to have a unified inventory /etc/ansible/hosts and  
group\_vars/all for common variables, group\_vars/production for produciton  
globals, group\_vars/staging ... etc.

Ansible is very flexible and doesn't really force you to 1 way of setting  
up your environment, but this can sometimes be "too much choice",

I just started using roles and I keep them in my playbook repo, not sure  
how they would work from /etc/ansible.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 29, 2013, 7:32pm UTC](https://forum.ansible.com/t/group-variables/12736/6 "2013-04-29T19:32:48Z")

</div>

You can fully path almost everything if you want to instead of assuming the relative roles paths.

“/etc/roles/foo” as a roles path, though I didn’t plan for it, will work.

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [April 30, 2013, 5:15am UTC](https://forum.ansible.com/t/group-variables/12736/7 "2013-04-30T05:15:39Z")

</div>

Thanks this was helpful. I had never really realised the total separation between inventory and playbooks. We keep both in the same area and under revision control.

I worked out a way to have a set of common variables for all environments by adding a "common" role with just a vars/main.yml, while I keep my environment specific variables under the inventory subdirs (prod/group\_vars/all, test/group\_vars/all, etc...).

By the way, it took me a while to get my head around roles vs. host groups, but I found a scheme that works now, and makes for very clean playbooks.Thanks!

One question: would a patch for accepting both main.yml and main.yaml acceptable (we've got all .yaml files, and have the .yml ones in there, which leads to typos)?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 1, 2013, 3:06am UTC](https://forum.ansible.com/t/group-variables/12736/8 "2013-05-01T03:06:42Z")

</div>

“One question: would a patch for accepting both main.yml and main.yaml acceptable (we’ve got all .yaml files, and have the .yml ones in there, which leads to typos)?”

Yes, there were some earlier attempts at this that we didn’t take because they duplicated some code, I dont’ think we ever got that back into something we merged in.

Perfectly fine and would be welcome, thanks!
