compute variables inside playbooks

Hi,

I’m wondering how to compute variables inside the playbook.
Maybe I’m just trying to do something wrong, please let me know…

Let’s say I have 2 frontend servers running NGINX as a proxy to two backend servers.
I want my nginx proxy on frontend1 to send requests to backend1 OR to backend2 as a backup server.
Of course, I want the opposit on frontend2.

My idea was to “compute” the upstream and backup-upstream servers inside the playbook… and fill a jinja template with these variables.
To do so, my playbook will have to know or guess a lot of information…
I don’t have a clear idea yet … But is that only possible ?
What is the other solution ? define the information hardcoded in a group-vars file ?

What would be the best way to do this ?
Idealy, the upstream and backup-upstream variables should be lists as I may have many more servers in the future…

Many thanks.

My idea was to “compute” the upstream and backup-upstream servers inside the playbook… and fill a jinja template with these variables.

Yes, there are some examples in the FAQ about how to iterate over hosts in various groups from within a template.
You can also access the facts made available to any server using the “host_vars” data structure.

I actually do this, but in the jinja templates that define the upstream config.