How to define variable based on when condition

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?

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

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