how to organize hosts

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?

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...

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:

`

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).

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.

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!