From da90c5f75e4db5c92092fe8dc39d2169557b0a45 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Mon, 26 Nov 2012 18:37:44 -0500 Subject: [PATCH] Document 'when' and tweak the code to be a little more comprehensive on what is false. --- docsite/rst/playbooks2.rst | 41 ++++++++++++++++++++++++++++++++++++ lib/ansible/playbook/task.py | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/docsite/rst/playbooks2.rst b/docsite/rst/playbooks2.rst index 9fd8bec0e0d..afc412a9571 100644 --- a/docsite/rst/playbooks2.rst +++ b/docsite/rst/playbooks2.rst @@ -288,6 +288,47 @@ While `only_if` is a pretty good option for advanced users, it exposes more guts we can do better. In 0.9, we will be adding `when`, which will be like a syntactic sugar for `only_if` and hide this level of complexity -- it will numerous built in operators. +Conditional Execution (Simplified) +`````````````````````````````````` + +In Ansible 0.9, we realized that only_if was a bit syntactically complicated, and exposed too much Python +to the user. As a result, the 'when' set of keywords was added. The 'when' statements do not have +to be quoted or casted to specify types, but you should seperate any variables used with whitespace. In +most cases users will be able to use 'when', but for more complex cases, only_if may still be required. + +Here are various examples of 'when' in use. 'when' is incompatible with 'only_if' in the same task:: + + - name: "do this if my favcolor is blue, and my dog is named fido" + action: shell /bin/false + when_string: $favcolor == 'blue' and $dog == 'fido' + + - name: "do this if my favcolor is not blue, and my dog is named fido" + action: shell /bin/true + when_string: $favcolor != 'blue' and $dog == 'fido' + + - name: "do this if my SSN is over 9000" + action: shell /bin/true + when_integer: $ssn > 9000 + + - name: "do this if I have one of these SSNs" + action: shell /bin/true + when_integer: $ssn in [ 8675309, 8675310, 8675311 ] + + - name: "do this if a variable named hippo is NOT defined" + action: shell /bin/true + when_unset: $hippo + + - name: "do this if a variable named hippo is defined" + action: shell /bin/true + when_set: $hippo + + - name: "do this if a variable named hippo is true" + action: shell /bin/true + when_boolean: $hippo + +The when_boolean check will look for variables that look to be true as well, such as the string 'True' or +'true', non-zero numbers, and so on. + Conditional Imports ``````````````````` diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index 42e09601f5f..355db227d4d 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -232,7 +232,7 @@ class Task(object): tcopy = tokens[1:] for (i, t) in enumerate(tcopy): if t.find("$") != -1: - tcopy[i] = "(is_set('''%s''') and '''%s'''.lower() not in ('false', 'none', '0', ''))" % (t, t) + tcopy[i] = "(is_set('''%s''') and '''%s'''.lower() not in ('false', 'no', 'n', 'none', '0', ''))" % (t, t) return " ".join(tcopy) else: