Add `state.when` context manager as an alternative to `if` conditionals.
This avoids unexpected branches which can result in non-deterministic operation order. This happens in the case where the conditional differs between hosts. Documentation around this to follow.
@@ -193,6 +193,17 @@ class State(object):
self.limit_hosts = old_limit_hosts
logger.debug('Reset limit to hosts: {0}'.format(old_limit_hosts))
+ @contextmanager
+ def when(self, predicate):
+ # Truth-y? Just yield/end, nothing happens here!
+ if predicate:
+ yield
+ return
+
+ # Otherwise limit any operations within to match no hosts
+ with self.limit([]):
+ yield
+
@contextmanager
def deploy(self, name, kwargs, data):
# Handle nested deploy names
