conditionally assign value to variable

20 views
Skip to first unread message

r144

unread,
Nov 10, 2015, 4:34:35 AM11/10/15
to Ansible Project
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

r144

unread,
Nov 10, 2015, 8:52:32 AM11/10/15
to Ansible Project


On Tuesday, November 10, 2015 at 10:34:35 AM UTC+1, r144 wrote:
I want to do something which should be quite easy, but I cannot find out exactly how I should do it.

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}}

Reply all
Reply to author
Forward
0 new messages