# copy command only if another file does not exist

**URL:** <https://forum.ansible.com/t/copy-command-only-if-another-file-does-not-exist/11586>\
**Category:** Ansible Project\
**Created:** [May 29, 2012, 11:40am UTC](https://forum.ansible.com/t/copy-command-only-if-another-file-does-not-exist/11586 "2012-05-29T11:40:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ansibleguest](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@ansibleguest](https://forum.ansible.com/u/ansibleguest)\
**Post date:** [May 29, 2012, 11:40am UTC](https://forum.ansible.com/t/copy-command-only-if-another-file-does-not-exist/11586/1 "2012-05-29T11:40:49Z")

</div>

Hi,

for the 'copy' command that nice 'if'- option creates=filename does  
not seam to be available ?

Alternative would be to get variable filled somehow with 'if a file  
exists or not'  
and based on this to use some onlyif clause.

Any idea to achieve this ?  
Thanks

(ansible-0.3.1-1.el6, Centos 6.2)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [May 29, 2012, 11:42am UTC](https://forum.ansible.com/t/copy-command-only-if-another-file-does-not-exist/11586/2 "2012-05-29T11:42:47Z")

</div>

creates=filename makes sense for command executions, but not so much  
for copy commands.

What is your use case here? first\_available\_file seems to do what you want.

---

<div class="post-metadata">

**Author:** ![ansibleguest](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@ansibleguest](https://forum.ansible.com/u/ansibleguest)\
**Post date:** [May 30, 2012, 12:35pm UTC](https://forum.ansible.com/t/copy-command-only-if-another-file-does-not-exist/11586/3 "2012-05-30T12:35:45Z")

</div>

I am using a file '.installed' as a flag on the machine after  
sucessfull software installation like this:

- action: copy src=files/$product/$version/archive.tar.gz dest=$base/  
$user/$product/archive.tar.gz  
- action: command gzip -d $base/$user/$product/archive.tar.gz creates=  
$base/$user/$product/.installed  
- action: command tar -xvf $base/$user/$product/archive.tar -C $base/  
$user/$product/ creates=$base/$user/$product/.installed  
- action: command echo $version \>\> $base/$user/$product/.installed

Of course i DONT want to copy the archive and untar everytime i run  
the playbook against the server...

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [May 30, 2012, 12:39pm UTC](https://forum.ansible.com/t/copy-command-only-if-another-file-does-not-exist/11586/4 "2012-05-30T12:39:08Z")

</div>

--Michael

> I am using a file '.installed' as a flag on the machine after  
> sucessfull software installation like this:
> 
> - action: copy src=files/$product/$version/archive.tar.gz dest=$base/  
> $user/$product/archive.tar.gz  
> - action: command gzip -d $base/$user/$product/archive.tar.gz creates=  
> $base/$user/$product/.installed  
> - action: command tar -xvf $base/$user/$product/archive.tar -C $base/  
> $user/$product/ creates=$base/$user/$product/.installed  
> - action: command echo $version \>\> $base/$user/$product/.installed
> 
> Of course i DONT want to copy the archive and untar everytime i run  
> the playbook against the server...

One of the proposals for 0.5/0.6 was to take md5sums prior to transfer for large files.

You should be able to use the "change" event on the copy to notify one or more handlers.

---

<div class="post-metadata">

**Author:** ![Pas](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@Pas](https://forum.ansible.com/u/Pas)\
**Post date:** [May 31, 2012, 3:00pm UTC](https://forum.ansible.com/t/copy-command-only-if-another-file-does-not-exist/11586/5 "2012-05-31T15:00:17Z")

</div>

Hello!

On a related note are there any plans for checking for certain features and conditionally executing large parts of playbooks? (Or it should be achieved via variables?)

My use case is very similar, but sometimes checking whether a certain software is available is a bit more complex than just looking for a file. (Then comparing versions might require other trickery.) Plus using creates=… seems rather cumbersome instead of some nice YAML structure, also it could speed up playbook execution (which is always a good thing in my book, especially for pull-based mode) by leaving out most part.

Thanks,  
Pas

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [May 31, 2012, 3:09pm UTC](https://forum.ansible.com/t/copy-command-only-if-another-file-does-not-exist/11586/6 "2012-05-31T15:09:16Z")

</div>

> Hello!
> 
> On a related note are there any plans for checking for certain features and conditionally executing large parts of playbooks? (Or it should be achieved via variables?)

only\_if can use variables to keep from copy/pasting the expression.

Tip though – If you want to apply certain things to certain classes of hosts it’s better to use groups for that than detection, i.e. “put php on hosts where I have apache” is troublesome rather than saying explicitly, “on these hosts I want apache + php”

Make a group called “php\_servers” in this case, and then you can avoid using only\_if at all, etc.

> My use case is very similar, but sometimes checking whether a certain software is available is a bit more complex than just looking for a file. (Then comparing versions might require other trickery.) Plus using creates=… seems rather cumbersome instead of some nice YAML structure, also it could speed up playbook execution (which is always a good thing in my book, especially for pull-based mode) by leaving out most part.

This is not too terrible if you write a custom module that returns an “ansible\_facts” dictionary, and you can then use that custom fact in only\_if expressions.

(This is more or less how you have to do it in Puppet land, and I’m okay with it being a little clunky as I suspect most people won’t need to do it that way.)
