# Breaking up hosts file

**URL:** <https://forum.ansible.com/t/breaking-up-hosts-file/21137>\
**Category:** Ansible Project\
**Created:** [April 12, 2016, 1:26pm UTC](https://forum.ansible.com/t/breaking-up-hosts-file/21137 "2016-04-12T13:26:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Hanstra](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@Tom\_Hanstra](https://forum.ansible.com/u/Tom_Hanstra)\
**Post date:** [April 12, 2016, 1:26pm UTC](https://forum.ansible.com/t/breaking-up-hosts-file/21137/1 "2016-04-12T13:26:42Z")

</div>

I currently have been putting both hosts and host related variables into the /etc/ansible/hosts file with variables such as this:

[myhosts]  
host1  
host2

[host1]  
host1.domain

[host1:vars]  
var1=“Some data”  
var2=“Some other data”  
tag1=myTag

[host2]  
host2.domain

[host2:vars]  
…

etc. This all works but is not overly scalable as the number of servers grows.

How can I break this up into a more directory based structure so that things can scale better with many servers. I’ve tried to read through information on host\_vars and group\_vars but have not succeeded in getting the syntax set up properly to mimic what I have working with the hosts file.

Can someone help me figure out directory structure, naming, and setup to get that working?

Thanks!

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [April 12, 2016, 1:40pm UTC](https://forum.ansible.com/t/breaking-up-hosts-file/21137/2 "2016-04-12T13:40:20Z")

</div>

Create /etc/ansible/host\_vars and /etc/ansible/group\_vars

Create group vars files like  
/etc/ansible/group\_vars/myhosts.yml

and enter variables for your ‘myhosts’ group in the myhosts.yml file. Remember that these are YAML files, so vars need to be in the form

key: value

NOT

key=value

Hope this helps,  
Jon

---

<div class="post-metadata">

**Author:** ![ac427](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@ac427](https://forum.ansible.com/u/ac427)\
**Post date:** [April 12, 2016, 1:42pm UTC](https://forum.ansible.com/t/breaking-up-hosts-file/21137/3 "2016-04-12T13:42:14Z")

</div>

You can create group\_vars directory

[http://docs.ansible.com/ansible/intro\_inventory.html#group-variables](http://docs.ansible.com/ansible/intro_inventory.html#group-variables)

---

<div class="post-metadata">

**Author:** ![Tom\_Hanstra](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@Tom\_Hanstra](https://forum.ansible.com/u/Tom_Hanstra)\
**Post date:** [April 12, 2016, 3:39pm UTC](https://forum.ansible.com/t/breaking-up-hosts-file/21137/4 "2016-04-12T15:39:29Z")

</div>

I’m still missing the syntax and file naming for this.

First off, can I create multiple files in group\_vars and have them all read? If so, what are the best naming conventions. Can I have names:

host1.yml  
host2.yml

etc. with a separate file for each host?

And, if so, what does the content of the file look like? I’ve tried this in my host1.yml file:

[host1]  
host.domain

[host1:vars]  
tag: myTag

but that does not work. What combination will actually work right?

---

<div class="post-metadata">

**Author:** ![ananta.c](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@ananta.c](https://forum.ansible.com/u/ananta.c)\
**Post date:** [April 12, 2016, 3:52pm UTC](https://forum.ansible.com/t/breaking-up-hosts-file/21137/5 "2016-04-12T15:52:38Z")

</div>

for host related stuff you can put in host\_vars

check this out [https://github.com/ac427/my\_cm/tree/master/ansible](https://github.com/ac427/my_cm/tree/master/ansible) for reference to host\_vars and group\_vars

---

<div class="post-metadata">

**Author:** ![Tom\_Hanstra](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@Tom\_Hanstra](https://forum.ansible.com/u/Tom_Hanstra)\
**Post date:** [April 12, 2016, 4:58pm UTC](https://forum.ansible.com/t/breaking-up-hosts-file/21137/6 "2016-04-12T16:58:24Z")

</div>

I’m afraid I’m still a YAML newbie and that syntax still has me baffled.

I see the files in your hosts\_var directory, but your variables are not simply:

label: var\_value

There seem to be a couple of extra layers and I don’t follow why they are there. Why this:

interfaces:  
auto:  
ip: ip\_number

Why the multiple layering?

And then how are these found for use by the playbook? When I try something similar, ansible complains that I don’t have an inventory file.

Sorry to be obtuse!

---

<div class="post-metadata">

**Author:** ![ananta.c](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@ananta.c](https://forum.ansible.com/u/ananta.c)\
**Post date:** [April 12, 2016, 5:05pm UTC](https://forum.ansible.com/t/breaking-up-hosts-file/21137/7 "2016-04-12T17:05:02Z")

</div>

Every system can have multiple interfaces ( so, multiple mac,ip address etc ) . That is the reason

I have values of mac,ip, etc in the host\_vars referenced in below playbook.  
[https://github.com/ac427/my\_cm/blob/master/ansible/playbooks/add\_node](https://github.com/ac427/my_cm/blob/master/ansible/playbooks/add_node)

---

<div class="post-metadata">

**Author:** ![Tom\_Hanstra](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@Tom\_Hanstra](https://forum.ansible.com/u/Tom_Hanstra)\
**Post date:** [April 12, 2016, 5:34pm UTC](https://forum.ansible.com/t/breaking-up-hosts-file/21137/8 "2016-04-12T17:34:24Z")

</div>

Perhaps I’m inching closer. Can I pick up the target from somewhere else? Or have target in the hosts file in some way that ansible will know to use it instead of having to add it to the command line?

Thanks!
