# how to organize hosts

**URL:** <https://forum.ansible.com/t/how-to-organize-hosts/23807>\
**Category:** Ansible Project\
**Created:** [February 24, 2017, 3:29pm UTC](https://forum.ansible.com/t/how-to-organize-hosts/23807 "2017-02-24T15:29:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [February 24, 2017, 3:29pm UTC](https://forum.ansible.com/t/how-to-organize-hosts/23807/1 "2017-02-24T15:29:45Z")

</div>

I’m struggling with this problem, hoping to get some feedback on how others solve it:  
The situation is this:  
I have two apps, App1 and App2. Each app is configured using a collection of roles and variables

In UAT, both apps run on the same server  
in Prod, there’s two servers; 1 run both apps, the other runs only App2.

I’m trying to come up with a solution that lets me reflect this in Ansible as easily as possible, and with the smallest amount of duplicate code.

Thinks I’m considering:

1. use hostvars combined with the “group by” functionality and separate things. Problem with this is that my two “generated” groups would be “app1+app2” and “app2”. This seems counter-intuitive to me, not to mention what if I get an App3, then the complexities suddenly explode.
2. Use hostvars combined with the “split” functionality, and then do a conditional loop to iterate each server’s apps and do includes from there.

Both of these seem like overly complicated solutions to a (in theory) simple problem. How are others solving this?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 24, 2017, 4:08pm UTC](https://forum.ansible.com/t/how-to-organize-hosts/23807/2 "2017-02-24T16:08:48Z")

</div>

Probably not what you want to hear - but have you considered setting  
up your UAT environment using two servers as well?  
That way your PROD and UAT environments resemble each other more  
closely - which is good.  
Your ansible structure would get less complex.  
Since you're using config management anyway, it wouldn't be much more work.  
Only thing could be resources...

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [February 24, 2017, 4:42pm UTC](https://forum.ansible.com/t/how-to-organize-hosts/23807/3 "2017-02-24T16:42:39Z")

</div>

I completely agree with you sir - and it’s definetely something we’re looking to address.

Still, the need to have some apps more “compacted” in dev/test while they’re more spread out in uat/prod is probably still relevant.

So, here’s what I’ve come up with this far:

`

---

<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:** [February 25, 2017, 5:15pm UTC](https://forum.ansible.com/t/how-to-organize-hosts/23807/4 "2017-02-25T17:15:33Z")

</div>

Is deploying both apps to all UAT and production servers an option? Then the only thing that would need to be different is which machines the apps run on, which could be set in host vars.

I guess if there is a lot of data that might be a problem, but disks cheaper than your time (hopefully).

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [February 26, 2017, 9:13am UTC](https://forum.ansible.com/t/how-to-organize-hosts/23807/5 "2017-02-26T09:13:26Z")

</div>

Would separate inventories for UAT and Prod work?

If you set the play to deploy each app to a group,  
you can put the same server into multiple inventory groups.

so UAT inventory has the same server in both the app1 and app2  
groups, but the Prod inventory has 2 servers in app1 but only  
one server in app2.

---

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [March 26, 2017, 9:16pm UTC](https://forum.ansible.com/t/how-to-organize-hosts/23807/6 "2017-03-26T21:16:33Z")

</div>

Late reply, but anyways.

We’re already using separate inventories per stage (uat/prod etc), and yes - using inventory hostgroups to solve this is a good idea. It’s definetely the most “ansibly” way of doing it I think. Thanks!
