I have some ansible tasks that are run in two parts. The first part sets a condition, and the second part uses when
to conditionally execute. Pseudo-example:
- name: check if installed
command: (...)
register: is_installed
- name: run install script
when: is_installed.stdout == "yes"
command: (...)
The second task shows as "skipped". This is not really right, I did not forgo the installation, but confirmed that it was already done, so it should show as "OK". It should show "skipped" only if the given host doesn't need this step. I know this is just cosmetic, but I would still like to know if there is a way to get it to say "OK".
I tried setting changed_when
to False
. While this sets the result to OK, it still runs the command. Somebody asked for an ok_when
setting, but it was declined and I'm not sure the developers understood the request.
I couldn't come up with general solution here. But if tasks actually installs something you could remove first part and add
creates
to second part to implement check.when:
evaluating to false results in a status skipped on that task. Printing something on skipped comes from the standard out callback plugin. For default based callbacks, this can be disabled globally with thedisplay_skipped_hosts
configuration item. See the docs:ansible-doc -t callback default
An elegant solution could be to wrap this thing in a real package manager, and install that. Several idempotent package manager modules exist for Ansible.
Or, the install script could be made safe to re-run, and returns with a zero code on success.
While I don't speak for the developers, they are cautious about adding features to core. And possibly they don't see a problem with a task reported as skipped, not every task of every play gets run.
when:
controls if a task runs.changed_when:
andfailed_when:
modify the status of a task after it runs, such as based off return code or standard out. Together these give control over if a task is a status of changed or failed, even for generic command tasks.For more control over idempotency, use a less generic module. And if you still are offended by what is printed, consider a custom callback plugin.