I'm struggling with creating rules that need to be skipped if a given directory exits.
If I do this:
tasks:
- name: create target dir
action: command mkdir -p $target_dir creates=$target_dir
register: create_directory
- name: do my thing
action: command woopty woop
only_if: '${create_directory.changed}'
But if the directory already exists, then the first task is skipped, and the variable does not exist. I can't be the first one to have such a dependency... What's the recommended way of doing this?
Thanks
I made it work by adding the variable and setting it to zero:
vars:
created_directory: 0
tasks:
- name: create target dir
action: command mkdir -p $target_dir creates=$target_dir
register: created_directory
- name: do my thing
action: command woopty woop
only_if: '${created_directory}'
This works. But now I wonder if there is a better, cleaner way of doing this.
Thanks.
You should not need to default the registered variable if you drop the
'creates=', the mkdir is itself harmless if run twice.
mkdir -p, without "creates=":
always executed, always "changed", always successful (rc=0), so it isn't useful to give me an idea of the state of the system.
I managed to make it works with mkdir, no -p, and "ignore_errors: yes", then I capture the state with
when_integer: ${created_directory.rc} == 0
But I don't like it, because the the mkdir task shows as failed, when really it hasn't, I'm looking for the state of the server, to make sure my playbook is idempotent. I've seen a mention of a stat module, I think it's a good idea, we need a simple way to use the existence of a file/directory as a state to trigger more tasks.
Ah, here's the catch
"But if the directory already exists, then the first task is skipped, "
Not true, the task still runs with the 'creates=' in there. It
decides to not execute on the remote end.
--Michael
Yves Dorfsman wrote:
mkdir -p, without "creates=":
always executed, always "changed", always successful (rc=0), so it isn't
useful to give me an idea of the state of the system.
I managed to make it works with mkdir, no -p, and "ignore_errors: yes",
then I
capture the state with
when_integer: ${created_directory.rc} == 0
But I don't like it, because the the mkdir task shows as failed, when
really
it hasn't, I'm looking for the state of the server, to make sure my
playbook
is idempotent. I've seen a mention of a stat module, I think it's a good
idea,
we need a simple way to use the existence of a file/directory as a state
to
trigger more tasks.
Is there any reason you're not using the file module for this?
Daniel
It does say "skipping: [all]" when I use "creates="
Also, ansible blows up when I try to access my variable, when the task is skipped.
Yves Dorfsman wrote:
I'm struggling with creating rules that need to be skipped if a given
directory exits.
If I do this:
tasks:
- name: create target dir
action: command mkdir -p $target_dir creates=$target_dir
register: create_directory
- name: do my thing
action: command woopty woop
only_if: '${create_directory.changed}'
But if the directory already exists, then the first task is skipped, and
the
variable does not exist. I can't be the first one to have such a
dependency...
What's the recommended way of doing this?
The recommended way is using
when_bool: ${create_directory.changed}
Daniel
Can you please show me full output of the run with -v -v -v set?
Yeah that's because it returns the skipped: True though, it is still executed.
It probably should just still set the variable but set it to skipped:
True in this case.
So an alternative to setup a variable is to use when_set, as mentioned by Daniel Hokka Zakrisson in another thread. I just tried it, I keep my "mkdir -p .../... creates=" (so the variable isn't set), and then use "when_set" on that variable, and it works.