# Structuring inventory

**URL:** <https://forum.ansible.com/t/structuring-inventory/14713>\
**Category:** Ansible Project\
**Created:** [March 6, 2014, 4:53am UTC](https://forum.ansible.com/t/structuring-inventory/14713 "2014-03-06T04:53:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aleksey\_Maksimov](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Aleksey\_Maksimov](https://forum.ansible.com/u/Aleksey_Maksimov)\
**Post date:** [March 6, 2014, 4:53am UTC](https://forum.ansible.com/t/structuring-inventory/14713/1 "2014-03-06T04:53:01Z")

</div>

Hi,

I am just starting with Ansible and have questions on how to better structure my inventory file(s).

Here is what I have:

Multiple hosts, assigned to different environments.

test1 → host1, host2, host3  
test2 → host4, host5, host6

On each of those hosts I have multiple users, that used to run various software components and such users have to be configured differently.

I’ve put this to inventory like this:

[test1-host1]  
test1-host1-user1 ansible\_ssh\_user=user1  
test1-host1-user2 ansible\_ssh\_user=user2

[test1-host1:vars]  
ansible\_ssh\_host=host1\_name

[test1-host2]  
test1-host2-user3 ansible\_ssh\_user=user3  
test1-host2-user4 ansible\_ssh\_user=user4

[test1-host2]  
ansible\_ssh\_host=host2\_name

Am I doing my inventory right?

Regards,  
Aleksey Maksimov

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 6, 2014, 5:13pm UTC](https://forum.ansible.com/t/structuring-inventory/14713/2 "2014-03-06T17:13:54Z")

</div>

Tyipcially you wouldn’t create different host records just to connect to them as different users, but would specify this as “-u” to the playbook execution.

And while you can keep variables inline with “:vars”, I would generally recommend group\_vars/ and host\_vars/ files for nearly everyone, they are cleaner  
and can also help represent structured data.

It’s definitely fine to set ansible\_ssh\_user there, but having hte “-user” as part of the host name is a bit unusual.

Also if you have different environments that are not managed together, I usually recommend seperate inventory files for them, keeps it simpler in most cases.

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [March 6, 2014, 5:47pm UTC](https://forum.ansible.com/t/structuring-inventory/14713/3 "2014-03-06T17:47:19Z")

</div>

I don’t know that there is a right or wrong way to do inventory, but I wouldn’t do it like that. 🙂

Given that you have two environments that might be two groups… But if there is nothing in common between the hosts in an environment then that may not be the case.

I would take your hosts and see what they have in common…

If for example you have two web servers, four application servers and two of your application servers also run databases then that looks like three clear groups…

[Web]  
host1  
host2

[App]  
host3  
host4  
host5  
host6

[DB]  
host5  
host6

If the environments share some things but are different for some things then you might also want environment groups

[test1]  
host1  
host3  
host5

[test2]  
host2  
host4  
host6

… Then use the group\_vars files to set the variables that you want ansible to apply.

I hope that this helps,

Adam

---

<div class="post-metadata">

**Author:** ![Aleksey\_Maksimov](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Aleksey\_Maksimov](https://forum.ansible.com/u/Aleksey_Maksimov)\
**Post date:** [March 7, 2014, 5:49am UTC](https://forum.ansible.com/t/structuring-inventory/14713/4 "2014-03-07T05:49:26Z")

</div>

> Tyipcially you wouldn’t create different host records just to connect to them as different users, but would specify this as “-u” to the playbook execution.

That way if I need to do something for all users in all hosts in the environments, I would have to execute playbooks manually per each user. This kind of defeats the purpose of automation in my mind.

> And while you can keep variables inline with “:vars”, I would generally recommend group\_vars/ and host\_vars/ files for nearly everyone, they are cleaner  
> and can also help represent structured data.

I understand that and just find it easier to have vars in the same file and format as inventory. Need to get used to switching between yaml and property files. 🙂

> It’s definitely fine to set ansible\_ssh\_user there, but having hte “-user” as part of the host name is a bit unusual.
> 
> Also if you have different environments that are not managed together, I usually recommend seperate inventory files for them, keeps it simpler in most cases.

Yes, I already do that, not pointed out for the sake of simpicity.

Regards,  
Aleksey Maksimov
