mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Convert boolean strings from set_fact to proper boolean values
Also adds integration tests for booleanification of strings Fixes #8629
This commit is contained in:
parent
8708a00cbd
commit
a571fd4efe
3 changed files with 93 additions and 1 deletions
|
@ -32,6 +32,16 @@ class ActionModule(object):
|
||||||
options = {}
|
options = {}
|
||||||
if complex_args:
|
if complex_args:
|
||||||
options.update(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))
|
return ReturnData(conn=conn, result=dict(ansible_facts=options))
|
||||||
|
|
|
@ -46,4 +46,12 @@ EXAMPLES = '''
|
||||||
- set_fact:
|
- set_fact:
|
||||||
one_fact: something
|
one_fact: something
|
||||||
other_fact: "{{ local_var * 2 }}"
|
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
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
|
@ -193,3 +193,77 @@
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- "result.skipped == true"
|
- "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"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue