conditionally assign value to variable

hello group,

I want to do something which should be quite easy, but I cannot find out exactly how I should do it.

I want variable usedir to have the name of directory {{currentdir}}/rails if it exists, else I want to have it the name of directory {{prevdir}}/rails if that directory exists, else I want it to be the empty string.

I have two registered variables with the stat value of the two directories with names curstat and prevstat. How can I use them to set variable userdir?

Thanks for your help.
Ruud

I found a solution, although it’s maybe not the best. In case someone is interested:

tasks:

  • name: check currentdir
    stat: path={{currentdir}}/rails
    register: curstat

  • name: check prevdir
    stat: path={{prevdir}}/rails
    register: prevstat

  • name: set variable
    set_fact: userdir=‘’

  • name: next prev
    when: prevstat.stat.exists
    set_fact: userdir={{prevstat.stat.path}}

  • name: next current
    when: curstat.stat.exists
    set_fact: userdir={{curstat.stat.path}}