# balanced configuration

**URL:** <https://forum.ansible.com/t/balanced-configuration/18267>\
**Category:** Ansible Project\
**Created:** [May 20, 2015, 3:11am UTC](https://forum.ansible.com/t/balanced-configuration/18267 "2015-05-20T03:11:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 20, 2015, 3:11am UTC](https://forum.ansible.com/t/balanced-configuration/18267/1 "2015-05-20T03:11:26Z")

</div>

Hi,

We have a 2 tiers environment where the front-end servers connects to the backend servers.

As an example we have 30 application servers and 3 backend servers, let’s say every backend server should support up to 10 frontend servers. We need Ansible to automatically setup the configuration so the load is shared :

servers 1-10 should connect to db1  
servers 11-20 should connect to db2  
servers 21-30 should connect to db3

The front-end servers doesn’t need to be sequential, the number of front-end can increase as the number of backends. The goal here is we want Ansible to be able to automatically balance the setup so the front-end servers have been evenly configured to use different backend servers.

Is this possible using Ansible ? I was trying to find a similar example using Jinja templates but maybe I was looking at the wrong place ?

Any help appreciated.

Regards,  
N.

---

<div class="post-metadata">

**Author:** ![Brent\_Langston1](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@Brent\_Langston1](https://forum.ansible.com/u/Brent_Langston1)\
**Post date:** [May 20, 2015, 4:03am UTC](https://forum.ansible.com/t/balanced-configuration/18267/2 "2015-05-20T04:03:15Z")

</div>

What you describe is similar to a setup I’ve used in the past. We broke up this part of our inventory into “pools” which was determined by a fact set for the host.

You could have poolA poolB and poolC, and db1, db2 and db3 respectively. poolA would connect to db1, poolB to db2, and poolC to db3.

Today, we’re using AWS, and the ec2.py inventory script. We carve up our inventory into (among other things) primary\_role, secondary\_role, and pool, so in your situation, unless db1, db2 and db3 are different enough that it makes sense to give them different secondary\_roles, then just isolationg by pool should be easy.

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 21, 2015, 8:16pm UTC](https://forum.ansible.com/t/balanced-configuration/18267/3 "2015-05-21T20:16:56Z")

</div>

Hi Brent , thanks for your reply. I was actually trying to avoid that as I was looking to achieve it in a more dynamic way so I don’t have to maintain a static inventory of pools..

This is what I have tried but still not working :

# inventory :

[pool-a:vars]

pool=a

[pool-a]  
server1  
server2  
server3

[pool-b:vars]

pool=b

[pool-b]  
server4  
server5  
server6

# template.j2

{% for servers in groups[‘pool-{{ pool }}’] %}  
server {{ hostvars[backend][‘ansible\_hostname’] }}  
{% endfor %}

# playbook

template: src=front\_config.j2 dest=/etc/front\_config

fatal: [[testing.example.net](http://testing.example.net)] =\> {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘dict object’ has no attribute ‘pool-{{ pool }}’”, ‘failed’: True}

Any help ?

---

<div class="post-metadata">

**Author:** ![Mike\_Herron](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@Mike\_Herron](https://forum.ansible.com/u/Mike_Herron)\
**Post date:** [May 21, 2015, 9:04pm UTC](https://forum.ansible.com/t/balanced-configuration/18267/4 "2015-05-21T21:04:53Z")

</div>

Assuming 'pool' is provided somewhere else, I don't think you want the curly braces?

{% for servers in groups['pool-' + pool] %}  
&nbsp;&nbsp;&nbsp;&nbsp;server {{ hostvars[backend]['ansible\_hostname'] }}  
{% endfor %}

Or are you looking to run that task for all your pools?

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 21, 2015, 11:35pm UTC](https://forum.ansible.com/t/balanced-configuration/18267/5 "2015-05-21T23:35:07Z")

</div>

Thanks Mike you are right, removing the curly braces and using + pool worked.
