copy command only if another file does not exist

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)

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.

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

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

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

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.)