# Host file variables - correct pattern?

**URL:** <https://forum.ansible.com/t/host-file-variables-correct-pattern/11797>\
**Category:** Ansible Project\
**Created:** [August 14, 2012, 10:24am UTC](https://forum.ansible.com/t/host-file-variables-correct-pattern/11797 "2012-08-14T10:24:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Palley](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jonathan\_Palley](https://forum.ansible.com/u/Jonathan_Palley)\
**Post date:** [August 14, 2012, 10:24am UTC](https://forum.ansible.com/t/host-file-variables-correct-pattern/11797/1 "2012-08-14T10:24:08Z")

</div>

Curious the best pattern to do the following. I _want_ to add the feature to allow host files to have variables, but it seems there should be a better way:

We have a few identical clusters of machines in different physical locations. So we have all our playbooks and then define the cluster in different hosts files. For example:

ansible-playbooks -i dev.vagrant some\_playbook.yml  
ansible-playbooks -i beijing\_idc some\_playbook.yml  
ansible-playbooks -i ec2-west some\_playbook.yml

Now, there are some variables that need to be specific to the host file or cluster. For example, the location of the yum repository to use.

Am I missing something obvious?

Thanks -

P.S.  
Some basic ansible tasks to get a centos server online: [https://github.com/brainpage/ansible-common](https://github.com/brainpage/ansible-common)

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [August 14, 2012, 11:06am UTC](https://forum.ansible.com/t/host-file-variables-correct-pattern/11797/2 "2012-08-14T11:06:37Z")

</div>

Jonathan Palley wrote:

> Curious the best pattern to do the following. I \*want\* to add the feature  
> to allow host files to have variables, but it seems there should be a  
> better way:
> 
> We have a few identical clusters of machines in different physical  
> locations. So we have all our playbooks and then define the cluster in  
> different hosts files. For example:
> 
> ansible-playbooks -i dev.vagrant some\_playbook.yml  
> ansible-playbooks -i beijing\_idc some\_playbook.yml  
> ansible-playbooks -i ec2-west some\_playbook.yml
> 
> Now, there are some variables that need to be specific to the host file or  
> cluster. For example, the location of the yum repository to use.
> 
> Am I missing something obvious?

Does [all:vars] not work for you?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [August 14, 2012, 12:10pm UTC](https://forum.ansible.com/t/host-file-variables-correct-pattern/11797/3 "2012-08-14T12:10:02Z")

</div>

Plenty of options here.

There's definitely [groupname:vars] as documented in the host file.

Also, you can store files in  
(directory\_inventory\_file\_is\_in)/group\_vars/$groupname.yml

If you do the latter, I'd recommend a latest pull of 0.7, as  
previously there was a slight bug where if your vars\_files section  
was empty it wouldn't load.

There's also vars\_files, itself, where you can load in a set of  
variables for a particular play only.

All of this is covered in the various pages of the docs, so I'd just  
pick which you like best. group\_vars/$groupname.yml or vars\_files  
are what you'll need for structured data.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Palley](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jonathan\_Palley](https://forum.ansible.com/u/Jonathan_Palley)\
**Post date:** [August 14, 2012, 5:03pm UTC](https://forum.ansible.com/t/host-file-variables-correct-pattern/11797/4 "2012-08-14T17:03:20Z")

</div>

> Jonathan Palley wrote:
> 
> > Curious the best pattern to do the following. I _want_ to add the feature  
> > to allow host files to have variables, but it seems there should be a  
> > better way:
> > 
> > We have a few identical clusters of machines in different physical  
> > locations. So we have all our playbooks and then define the cluster in  
> > different hosts files. For example:
> > 
> > ansible-playbooks -i dev.vagrant some\_playbook.yml  
> > ansible-playbooks -i beijing\_idc some\_playbook.yml  
> > ansible-playbooks -i ec2-west some\_playbook.yml
> > 
> > Now, there are some variables that need to be specific to the host file or  
> > cluster. For example, the location of the yum repository to use.
> > 
> > Am I missing something obvious?
> 
> Does [all:vars] not work for you?

That is the obvious way that I just wasn’t seeing. Thanks.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Palley](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@Jonathan\_Palley](https://forum.ansible.com/u/Jonathan_Palley)\
**Post date:** [August 14, 2012, 5:05pm UTC](https://forum.ansible.com/t/host-file-variables-correct-pattern/11797/5 "2012-08-14T17:05:59Z")

</div>

I think why the groupname variations on loading vars doesn’t work is because I’m using the same group names in different inventory files.

Maybe this is where I’ve gone off track in terms of the standard pattern; but the host files just became to long and messy which is why I put each “cluster” in its own host files.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [August 14, 2012, 5:09pm UTC](https://forum.ansible.com/t/host-file-variables-correct-pattern/11797/6 "2012-08-14T17:09:33Z")

</div>

> I think why the groupname variations on loading vars doesn’t work is because I’m using the same group names in different inventory files.

ansible looks at one and only one inventory file, so I can close this. ok.

> Maybe this is where I’ve gone off track in terms of the standard pattern; but the host files just became to long and messy which is why I put each “cluster” in its own host files.

I suspect as we grow there will be alternative external inventory scripts that support more of a “.d” directory structure. I’d suggest someone write one for their own use and release it, and  
I’ll gladly mention it on the “ansible-resources” page.

(As a refresher, if you have a script that behaves correctly, you can use it with “-i” or even place it in default the inventory file location.)
