# Best Practice Question: Multiple non-overlapping groups for one host - best alternative?

**URL:** <https://forum.ansible.com/t/best-practice-question-multiple-non-overlapping-groups-for-one-host-best-alternative/16832>\
**Category:** Ansible Project\
**Created:** [November 20, 2014, 11:30am UTC](https://forum.ansible.com/t/best-practice-question-multiple-non-overlapping-groups-for-one-host-best-alternative/16832 "2014-11-20T11:30:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Fritzsche](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@David\_Fritzsche](https://forum.ansible.com/u/David_Fritzsche)\
**Post date:** [November 20, 2014, 11:30am UTC](https://forum.ansible.com/t/best-practice-question-multiple-non-overlapping-groups-for-one-host-best-alternative/16832/1 "2014-11-20T11:30:40Z")

</div>

Hi all,

in the setup I would like to automate with ansible I have mostly servers with  
distinct purposes and on each server they may be several distinct  
services/components. The plan is to use ansible to have the configuration of  
all these servers in one place and to enable us to setup staging environments  
for the new software we develop and for new releases of the software stack we  
use (mariadb, nginx, ...)

Since we want to be able to rearrange the "services" (i.e., a database or a  
FastCGI application) independently to different hosts I started to organize  
everything into ansible groups, i.e., I have groups like

- database\_01  
- database\_02  
- ...  
- fcgi\_01  
- fcgi\_02  
- ...

for each group I set variables in group\_vars/\<group-name\>, which looks a  
little bit like the following:

group\_vars/database\_01::

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [November 20, 2014, 4:18pm UTC](https://forum.ansible.com/t/best-practice-question-multiple-non-overlapping-groups-for-one-host-best-alternative/16832/2 "2014-11-20T16:18:30Z")

</div>

Hi David, the best way to do this would be to make the role aware of the fact that there may be more than one database installed on it, so that the variables in the group would then become an array:

# group\_vars/foo.yml:

databases:

- name: db01  
user: db\_user1  
password: xxxxxx
- name: db02  
user: db\_user2  
password: xxxxxx  
…

And then you would loop over this list in your role tasks.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![David\_Fritzsche](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@David\_Fritzsche](https://forum.ansible.com/u/David_Fritzsche)\
**Post date:** [November 20, 2014, 5:11pm UTC](https://forum.ansible.com/t/best-practice-question-multiple-non-overlapping-groups-for-one-host-best-alternative/16832/3 "2014-11-20T17:11:30Z")

</div>

Hi James,

thanks for the tip. I think the approach is not totally suitable for my  
situation. I would rather not define all the databases for one server in one  
place for two reasons:

I would like to have it very easy to "move" the database to a different server  
(only in the sense that it gets installed on a different server if I run  
everything from scratch to install a test/staging environment)

Also quite important is that I want to be able to run a playbook that installs  
just db01 and not db02 to save development time (e.g., when I change the  
database schema of db01, but do not want to run the tasks that check if there  
needs to be updates to db02 or when I want to setup a new test environment  
where I know that I do not need db02...).

Maybe there is a way to setup things that I have to compromise at most on one  
of these things?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 20, 2014, 5:48pm UTC](https://forum.ansible.com/t/best-practice-question-multiple-non-overlapping-groups-for-one-host-best-alternative/16832/4 "2014-11-20T17:48:22Z")

</div>

define all DBs in group\_vars/all

then have 3 groups:

[singledbA]  
mydbs = dbA

[singledbB]  
mydbs = dbA, dbB

[bothddbs]  
mydbs = dbA,dbB

then have the task that installs them use use the mydbs  
you can even define mydbs by host
