# Setting a variable by conditional in a playbook

**URL:** <https://forum.ansible.com/t/setting-a-variable-by-conditional-in-a-playbook/15335>\
**Category:** Ansible Project\
**Created:** [May 27, 2014, 2:52am UTC](https://forum.ansible.com/t/setting-a-variable-by-conditional-in-a-playbook/15335 "2014-05-27T02:52:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lars\_Sommer](https://avatars.discourse-cdn.com/v4/letter/l/aca169/32.png) [@Lars\_Sommer](https://forum.ansible.com/u/Lars_Sommer)\
**Post date:** [May 27, 2014, 2:52am UTC](https://forum.ansible.com/t/setting-a-variable-by-conditional-in-a-playbook/15335/1 "2014-05-27T02:52:00Z")

</div>

I have spent the last few hours trying to find a way to do this and keep running into formatting issues with every suggestion I find.  
What I am trying to accomplish is to have a variable that is set based on the value of another variable.

So if {{ group }} == dev\* then I want to set uwsgi\_env: development  
but if {{ group }} == prod\* then I want to set uwsgi\_env: production

This seems really reasonable, is there a simple way to accomplish this in a playbook?

---

<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:** [May 27, 2014, 2:42pm UTC](https://forum.ansible.com/t/setting-a-variable-by-conditional-in-a-playbook/15335/2 "2014-05-27T14:42:36Z")

</div>

I just set uwsgi\_env in group\_vars/{production,development}, but you should be able to do this also use set\_fact and when to do the same.​

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 27, 2014, 8:02pm UTC](https://forum.ansible.com/t/setting-a-variable-by-conditional-in-a-playbook/15335/3 "2014-05-27T20:02:24Z")

</div>

+1 to group\_variables over set\_fact. It’s more native and probably more intuitive.

set\_fact should only be used for really oddball niche scenarios, like when you need to multiply a number by 10 or round a value, etc.
