file exists check with only_if

Hello,

In order to make a command task idempotence, I want to check a file
exists by using "only_if" something like this

- name: do something
  command: do_something.sh
  only_if: not os.path.exists("result.file")

I know make one more task and use "register" can realize this, but I
want to keep a playbook simple.

Is there a way to evaluate more programmable expression at the "only_if" ?

Regards,

WAKAYAMA Shirou

What you want is already built into the command module. See the "creates"
option.

http://ansible.cc/docs/modules.html#command

So your call would look like this:

- name: do something
  command: do_something.sh creates=/path/to/result.file

This cause the command to be skipped if the file is present.

So “only_if” is the old way do conditionals, we have “when:” in 1.2 (the “when_integer” and so on were a stopgap in 1.1).

Anyway, it executes locally, not remotely.

It seems like you can benefit from the “creates=” and “removes=” parameters to the command and shell modules for what you want to do as long as it involves file existence/absence.

Hello,

What you want is already built into the command module. See the "creates" option.
- name: do something
command: do_something.sh creates=/path/to/result.file

Thank you, it works!

But, these "creates" and "removes" are little misunderstanding names I think.
How about "if_exists" and "not_exists" ?

Regards,
Shirou

Not going to change them.