# How to define variable based on when condition

**URL:** <https://forum.ansible.com/t/how-to-define-variable-based-on-when-condition/30649>\
**Category:** Ansible Developer\
**Created:** [June 25, 2019, 4:58pm UTC](https://forum.ansible.com/t/how-to-define-variable-based-on-when-condition/30649 "2019-06-25T16:58:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sase\_kumar1](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@sase\_kumar1](https://forum.ansible.com/u/sase_kumar1)\
**Post date:** [June 25, 2019, 4:58pm UTC](https://forum.ansible.com/t/how-to-define-variable-based-on-when-condition/30649/1 "2019-06-25T16:58:01Z")

</div>

Hi Folks,

I’m new to ansible and I have got some confusion in when the statement

here is my playbook -

hosts: localhost  
gather\_facts: false  
vars:  
nodetype: “DI”  
tasks:

- name: concatenate  
set\_fact:  
argu: “DI=1,DC=0,DP=0”  
when: “{{ nodetype }}” == “DI”  
register: argu  
set\_fact:  
argu: “DI=0,DC=1,DP=0”  
when: “{{ nodetype }}” == “DC”  
register: argu
- debug:  
msg: “{{ argu }}”

This is just defining variable “argu” based on when condition ,I want to just build variable based on the input nodetype . but this is not working and always it prints the second argu(DI=0,DC=1,DP=0) value

Is this the right way or simpler way to achieve this?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 25, 2019, 5:11pm UTC](https://forum.ansible.com/t/how-to-define-variable-based-on-when-condition/30649/2 "2019-06-25T17:11:51Z")

</div>

Remove the `register` on those tasks. `set_fact` doesn’t need this.

---

<div class="post-metadata">

**Author:** ![sase\_kumar](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@sase\_kumar](https://forum.ansible.com/u/sase_kumar)\
**Post date:** [June 25, 2019, 5:17pm UTC](https://forum.ansible.com/t/how-to-define-variable-based-on-when-condition/30649/3 "2019-06-25T17:17:35Z")

</div>

Hi Matt

I removed and tried it , but still it prints the second value of argu in debug msg even though i passed another value
