# dynamically set variables with set\_fact

**URL:** <https://forum.ansible.com/t/dynamically-set-variables-with-set-fact/24026>\
**Category:** Ansible Project\
**Created:** [March 21, 2017, 4:57pm UTC](https://forum.ansible.com/t/dynamically-set-variables-with-set-fact/24026 "2017-03-21T16:57:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Podber](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@Matthew\_Podber](https://forum.ansible.com/u/Matthew_Podber)\
**Post date:** [March 21, 2017, 4:57pm UTC](https://forum.ansible.com/t/dynamically-set-variables-with-set-fact/24026/1 "2017-03-21T16:57:46Z")

</div>

I want to dynamically set a value to a variable using set\_fact, however, it always uses the first value:

- name: Global\_vars - get env info  
set\_fact:  
jm\_env: “{{lookup(‘env’, ‘Environment’)}}”  
l\_env: “{% if ‘{{jm\_env}}==Develop’ %}d{% elif ‘{{jm\_env}}==Staging’%}s{% else %}p{% endif %}”

l\_env variable always returns d

I have tried putting quotes in different places, etc with no success.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 21, 2017, 8:04pm UTC](https://forum.ansible.com/t/dynamically-set-variables-with-set-fact/24026/2 "2017-03-21T20:04:13Z")

</div>

Correct syntac  
l\_env: "{% if jm\_env=='Develop' %}d{% elif jm\_env=='Staging'%}s{% else %}p{% endif %}"

I also suspect that you need to split them so jm\_env is in one task and l\_env in another task.
