Design advice?

Hi,

First off I wanted to say that I’ve been using Ansible for a while now and so far I’m really pleased with it.
The product is powerful and flexible and the community is alive and helpful.

I was hoping that some of you might be able to share your insights on how to best approach the following:

I have ‘x’ backend hosts and ‘y’ frontend hosts. Each backend host runs 5 instances of a certain application, each with their own configuration.
Currently I have these defined through group_vars, such as:

[be-hosts]
my-backend-host[1:6]

[fe-hosts]
my-frontend-host[1:15]

This way I can easily apply a playbook against all of the backend or frontend hosts.
However, the challenge lies in defining the application instances, which I’d really like to be somewhat more dynamic.
Currently I drive this through host_vars such as:

my-backend-host1
instances: { start: 1, end: 5 }

my-backend-host2

instances: { start: 6, end: 10 }

etc.

One of my roles contains a directory called ‘instances’ with sub-directories called instanceN, the files from these directories are copied/templated using a ‘with_sequence’ loop.
However, I would really like to be able to specify certain variables on a host/group level for a specific instance which with this setup I can’t; well I can by defining instanceN.someVariable of course but it’s not really clean, nor is it easy to limit execution to a single instance this way.

In short, I would like to be able to treat those application instances like one can treat hosts using group_vars / limit etc.
What would also be nice is being able to intelligently allocate the instances based on the hostname (host1 = 1 - 5, host2 = 6 - 10, host3 = 11 - 15, so that would be something like start=(hostN - 1) * 5 end=(hostN * 5))

Any advice? Would you suggest a completely different approach?

Thanks for sharing your insights,
Nico.

I’ve also been playing with the idea of doing the following:

  • include: x.yml var={{item}}
    with_sequence: …

Sadly, includes are no longer allowed to be used in conjunction with ‘with_items’.
Same goes on playbook level, invoking a role multiple times in a loop isn’t allowed either.

Now I know that the best practice supposedly is to use loops within tasks, but for me that really doesn’t work since this role consists of like 30 tasks that would all require the same loop around them, to me it makes much more sense if I can define the loop before including the file instead of having to write the same loop 30 times.

Anyway, I’m still having the feeling that I should aim at a different approach; surely there must be other people who have servers with multiple instances of an application on it?

You can’t do what you ask above.

You should pass a variable into the role as a role parameter and have loops on the individual task steps.

OK, but any advice on my initial post in this thread?
My main problem there is that I have x hosts with y application instances on each; in it’s current incarnation there is no way to ‘limit’ on a set/single application instance(s). The only way I can think of doing this is by also writing it all out such as:

name: update application server 1 on host x
action: whatever
tags: update-application-server-1

Which in my case would result in more than 90 of these blocks.

“My main problem there is that I have x hosts with y application instances on each; in it’s current incarnation there is no way to ‘limit’ on a set/single application instance(s). The only way I can think of doing this is by also writing it all out such as:”

Can’t say I have time to review all your playbooks but --limit hostname exists and is a thing.