compound conditional at when clause using stat module not working.

268 views
Skip to first unread message

Adam R.

unread,
Apr 4, 2015, 8:53:00 AM4/4/15
to ansible...@googlegroups.com
Hello,


I am using the stat module to check if a file exist, if not, copy from src to destination. I also want to setup a flag in order to overwrite destination, to achieve this, i am using a conditional like the one on the when clause:

- stat: path=/path/to/file
  register: nameoffile
- copy: src=/home/user/source.tar.gz dest=/destination/path/file owner=root group=root
  when: flag_to_overwrite == true or (nameoffile.stat.isreg is undefined and nameoffile.stat.isreg == false)


In vars/main.yml:
flag_to_overwrite: true


If the destination does exist and flag_to_overwrite is set to true,  when i run the playbook, ansible-playbook doesn't show any error, but the file is not overwritten. 

If i use only the variable flag_to_overwrite, the value it's been evaluated and works ok (true copies the file, false does not do anything).
  when: flag_to_overwrite == true


I have used:
when: flag_to_overwrite or (nameoffile.stat.isreg is undefined and nameoffile.stat.isreg == false)

when: (flag_to_overwrite == true) or (nameoffile.stat.isreg is undefined and nameoffile.stat.isreg == false)

but the compound conditional does not work.


Any hint?


Thanks in advance!


Ansible 1.7





James Cammarata

unread,
Apr 6, 2015, 12:03:19 PM4/6/15
to ansible...@googlegroups.com
Hi Adam, the logic you have there is the problem. "(nameoffile.stat.isreg is undefined and nameoffile.stat.isreg == false)" will never evaluate to true, as "isreg" cannot be both undefined and false. I think you want to change the and to an or, as follows (also simplified a little):

when: flag_to_overwrite or nameoffile.stat.isreg is undefined or not nameoffile.stat.isreg

Hope that helps!


James Cammarata
Director, Ansible Core Engineering
github: jimi-c

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/74d19de5-32a7-4b40-b31b-3617fe18af5e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nino Pineda

unread,
Apr 7, 2015, 8:06:46 PM4/7/15
to ansible...@googlegroups.com
Hello James,

nameoffile.stat.isreg is undefined and nameoffile.stat.isreg == false,  evaluates to true when the file does not exists. I just deleted the target file, ran the playbook and this one copies the src to target. If the destination exists does not do anything.

I tried with the statement that you suggested:


           flag_to_overwrite or nameoffile.stat.isreg is undefined or not nameoffile.stat.isreg

but also does not matter what value flag_to_overwrite has, if the destination exists, the src it isn't copied.

The contents of vars/main.yml is:
---
flag_to_overwrite: true



How to use variables defined outside the when clause of the stat module?


I tested again placing inside of when clause:

flag_to_overwrite == true

but the file is only copied if destination does not exist.


If the destination does not exist placing flag_to_overwrite to false the playbook does not change anything (leaving only ----- when: flag_to_overwrite in the stat module invocation), changing flag_to_overwrite to true does copy the src to dest only one time. (i.e. if destination exists it is not overwritten even if flag_to_overwrite is set to true).


Thanks for your suggestions.



Adam

James Cammarata

unread,
Apr 8, 2015, 10:46:07 AM4/8/15
to ansible...@googlegroups.com
Is the checksum of the file different when it exists? If not, the file will not be replaced.

James Cammarata
Director, Ansible Core Engineering
github: jimi-c

Adam R.

unread,
Apr 8, 2015, 10:50:08 PM4/8/15
to ansible...@googlegroups.com
Hello James,

No, the checksum of the file is the same when it exists.

i tried using:

 - stat: path=/path/to/file get_checksum=no get_md5=no 

without success.


Thanks for your help! 


Any other tip is welcome.


Adam
Reply all
Reply to author
Forward
0 new messages