From a571fd4efecf0b7a870f40e9b5433559def4244e Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 10 Sep 2014 09:51:55 -0500 Subject: [PATCH] Convert boolean strings from set_fact to proper boolean values Also adds integration tests for booleanification of strings Fixes #8629 --- lib/ansible/runner/action_plugins/set_fact.py | 12 ++- library/utilities/set_fact | 8 ++ .../roles/test_conditionals/tasks/main.yml | 74 +++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) 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" +