# compound conditional at when clause using stat module not working.

**URL:** <https://forum.ansible.com/t/compound-conditional-at-when-clause-using-stat-module-not-working/17904>\
**Category:** Ansible Project\
**Created:** [April 4, 2015, 6:53am UTC](https://forum.ansible.com/t/compound-conditional-at-when-clause-using-stat-module-not-working/17904 "2015-04-04T06:53:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_R](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@Adam\_R](https://forum.ansible.com/u/Adam_R)\
**Post date:** [April 4, 2015, 6:53am UTC](https://forum.ansible.com/t/compound-conditional-at-when-clause-using-stat-module-not-working/17904/1 "2015-04-04T06:53:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [April 6, 2015, 4:03pm UTC](https://forum.ansible.com/t/compound-conditional-at-when-clause-using-stat-module-not-working/17904/2 "2015-04-06T16:03:10Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Nino\_Pineda](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@Nino\_Pineda](https://forum.ansible.com/u/Nino_Pineda)\
**Post date:** [April 7, 2015, 9:39pm UTC](https://forum.ansible.com/t/compound-conditional-at-when-clause-using-stat-module-not-working/17904/3 "2015-04-07T21:39:02Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [April 8, 2015, 2:45pm UTC](https://forum.ansible.com/t/compound-conditional-at-when-clause-using-stat-module-not-working/17904/4 "2015-04-08T14:45:59Z")

</div>

Is the checksum of the file different when it exists? If not, the file will not be replaced.

---

<div class="post-metadata">

**Author:** ![Adam\_R](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@Adam\_R](https://forum.ansible.com/u/Adam_R)\
**Post date:** [April 9, 2015, 2:50am UTC](https://forum.ansible.com/t/compound-conditional-at-when-clause-using-stat-module-not-working/17904/5 "2015-04-09T02:50:08Z")

</div>

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
