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 ?
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.
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 %}
fatal: [testing.example.net] => {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘dict object’ has no attribute ‘pool-{{ pool }}’”, ‘failed’: True}