# compute variables inside playbooks

**URL:** <https://forum.ansible.com/t/compute-variables-inside-playbooks/12996>\
**Category:** Ansible Project\
**Created:** [June 20, 2013, 3:08pm UTC](https://forum.ansible.com/t/compute-variables-inside-playbooks/12996 "2013-06-20T15:08:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prune](https://avatars.discourse-cdn.com/v4/letter/p/77aa72/32.png) [@Prune](https://forum.ansible.com/u/Prune)\
**Post date:** [June 20, 2013, 3:08pm UTC](https://forum.ansible.com/t/compute-variables-inside-playbooks/12996/1 "2013-06-20T15:08:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 20, 2013, 4:05pm UTC](https://forum.ansible.com/t/compute-variables-inside-playbooks/12996/2 "2013-06-20T16:05:10Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 20, 2013, 7:08pm UTC](https://forum.ansible.com/t/compute-variables-inside-playbooks/12996/3 "2013-06-20T19:08:17Z")

</div>

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