summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Cammarata <jimi@sngx.net>2014-09-10 09:51:55 -0500
committerJames Cammarata <jimi@sngx.net>2014-09-24 13:54:11 -0500
commite110126121a3f9c0bf8d76755f0865c09a54800b (patch)
treeb9cc2bdf3de7b9264903ea6824ab2bb4da790462
parent7954be25936304df0a68bdab13e0d36ed1f33123 (diff)
downloadansible-e110126121a3f9c0bf8d76755f0865c09a54800b.tar.gz
Convert boolean strings from set_fact to proper boolean values
Also adds integration tests for booleanification of strings Fixes #8629
-rw-r--r--lib/ansible/runner/action_plugins/set_fact.py12
-rw-r--r--library/utilities/set_fact8
-rw-r--r--test/integration/roles/test_conditionals/tasks/main.yml74
3 files changed, 93 insertions, 1 deletions
diff --git a/lib/ansible/runner/action_plugins/set_fact.py b/lib/ansible/runner/action_plugins/set_fact.py
index 37502ce3a2..7ac972cac6 100644
--- a/lib/ansible/runner/action_plugins/set_fact.py
+++ b/lib/ansible/runner/action_plugins/set_fact.py
@@ -32,6 +32,16 @@ class ActionModule(object):
options = {}
if complex_args:
options.update(complex_args)
- options.update(utils.parse_kv(module_args))
+
+ # parse the k=v arguments and convert any special boolean
+ # strings into proper booleans (issue #8629)
+ parsed_args = utils.parse_kv(module_args)
+ for k,v in parsed_args.iteritems():
+ # convert certain strings to boolean values
+ if isinstance(v, basestring) and v.lower() in ('true', 'false', 'yes', 'no'):
+ parsed_args[k] = utils.boolean(v)
+
+ # and finally update the options with the parsed/modified args
+ options.update(parsed_args)
return ReturnData(conn=conn, result=dict(ansible_facts=options))
diff --git a/library/utilities/set_fact b/library/utilities/set_fact
index 916e603c59..ea67cc43a3 100644
--- a/library/utilities/set_fact
+++ b/library/utilities/set_fact
@@ -46,4 +46,12 @@ EXAMPLES = '''
- set_fact:
one_fact: something
other_fact: "{{ local_var * 2 }}"
+
+# As of 1.8, Ansible will convert boolean strings ('true', 'false', 'yes', 'no')
+# to proper boolean values when using the key=value syntax, however it is still
+# recommended that booleans be set using the complex argument style:
+- set_fact:
+ one_fact: true
+ other_fact: false
+
'''
diff --git a/test/integration/roles/test_conditionals/tasks/main.yml b/test/integration/roles/test_conditionals/tasks/main.yml
index f82566483c..f2aa0068c6 100644
--- a/test/integration/roles/test_conditionals/tasks/main.yml
+++ b/test/integration/roles/test_conditionals/tasks/main.yml
@@ -193,3 +193,77 @@
assert:
that:
- "result.skipped == true"
+
+#-----------------------------------------------------------------------
+# proper booleanification tests (issue #8629)
+
+- name: set fact to string 'false'
+ set_fact: bool_test1=false
+
+- name: set fact to string 'False'
+ set_fact: bool_test2=False
+
+- name: set fact to a proper boolean using complex args
+ set_fact:
+ bool_test3: false
+
+- name: "test boolean value 'false' string using 'when: var'"
+ command: echo 'hi'
+ when: bool_test1
+ register: result
+
+- name: assert that the task did not run for 'false'
+ assert:
+ that:
+ - "result.skipped == true"
+
+- name: "test boolean value 'false' string using 'when: not var'"
+ command: echo 'hi'
+ when: not bool_test1
+ register: result
+
+- name: assert that the task DID run for not 'false'
+ assert:
+ that:
+ - "result.changed"
+
+- name: "test boolean value of 'False' string using 'when: var'"
+ command: echo 'hi'
+ when: bool_test2
+ register: result
+
+- name: assert that the task did not run for 'False'
+ assert:
+ that:
+ - "result.skipped == true"
+
+- name: "test boolean value 'False' string using 'when: not var'"
+ command: echo 'hi'
+ when: not bool_test2
+ register: result
+
+- name: assert that the task DID run for not 'False'
+ assert:
+ that:
+ - "result.changed"
+
+- name: "test proper boolean value of complex arg using 'when: var'"
+ command: echo 'hi'
+ when: bool_test3
+ register: result
+
+- name: assert that the task did not run for proper boolean false
+ assert:
+ that:
+ - "result.skipped == true"
+
+- name: "test proper boolean value of complex arg using 'when: not var'"
+ command: echo 'hi'
+ when: not bool_test3
+ register: result
+
+- name: assert that the task DID run for not false
+ assert:
+ that:
+ - "result.changed"
+