I was just wondering if there's a bail-out or break module/keyword
that I can use to stop processing a play on that host after a certain
condition is met.
For instance, I'm doing some setup on some servers where some certain
things need to happen based on how the machine was provisioned
(hardware setup). The condition doesn't really belong in a group and
doesn't change the server's role, just whether or not I need to run
the rest of the play. I can determine this condition and I've used
register + set_fact to make it available, but as it stands now I have
to put a "when: do_all_the_things" on each and every subsequent task.
Not a biggies, just wondering if there's a better way.
Yes, there is a “fail” module which will terminate execution, which you can couple with the “when” keyword to target certain conditions.
http://docs.ansible.com/fail_module.html
That's close but not quite what I want. I don't want to halt execution
entirely, just skip the rest of this play for this host. There are
other plays involved that should be run (part of a multiple role
setup). Also, if you use fail and it's combined with serial and
max_fail_percentage it will cause the entire run to halt and for this
play to not even be attempted on other hosts. I don't want this
condition to trigger a "failure". It's more like "next" or "continue"
in most programming languages. I know ansible is not a full language,
but it already has lots of flow control capabilities I was just
wondering if it had this one too.
Ahh, if you’re not wanting to completely bail out of the play entirely then you have a few options:
- start using tags to label your tasks and use the --tags or --skip-tags options to control which parts are run/skipped.
- move those tasks into a new file and use “include” with “when” to control when those tasks are run.
- split your playbook into roles (which can also use tags and when to conditionally control execution).
Ahh, if you're not wanting to completely bail out of the play entirely then
you have a few options:
1) start using tags to label your tasks and use the --tags or --skip-tags
options to control which parts are run/skipped.
This doesn't work since the rest of the play is conditional on
something that is dynamically discovered on the machine (let's say a
battery-backed cache or something like that). We don't know if
something is going to be skipped until the play has reached this
point.
2) move those tasks into a new file and use "include" with "when" to control
when those tasks are run.
That has some promise. I'll report back after playing around.
3) split your playbook into roles (which can also use tags and when to
conditionally control execution).
Not something that is known ahead of time and can change. The play
being executed is already in a role. Can a role conditionally include
another role while it's executing? Also seems clumsy to have a logical
role split into 2 separate roles. Makes having the logical 2nd half
share the vars/handlers/etc of the 1st half trickier.
Plays cannot be included in roles, only the other way around
Includes are never conditional, conditionals get applied to the tasks included, but not to the include itself.
The idea of a “skip” task has been proposed.
It would need to be implemented by something that probably set a flag in the fact cache that playbook/init.py knew about, and some automagic, and then cleared between starting new plays.
Pull requests might be entertained if it looked like:
- skip: msg=“I’m not that kind of Doctor”
when: timelord
etc
Could be useful for security roles that opt out of running when certain apps aren’t installed! (think STIG and such).
If someone wants to take a crack at it, have at!