Don't perform string to bool conversion in set_fact when jinja2 native types is enabled (#43425)
* Don't perform string to bool conversion in set_fact when jinja2 native types is enabled. Fixes #42599 * Add tests for boolean conversions in set_fact
This commit is contained in:
parent
43c508b3c3
commit
77d2008150
4 changed files with 47 additions and 1 deletions
|
@ -23,6 +23,8 @@ from ansible.module_utils.parsing.convert_bool import boolean
|
||||||
from ansible.plugins.action import ActionBase
|
from ansible.plugins.action import ActionBase
|
||||||
from ansible.utils.vars import isidentifier
|
from ansible.utils.vars import isidentifier
|
||||||
|
|
||||||
|
import ansible.constants as C
|
||||||
|
|
||||||
|
|
||||||
class ActionModule(ActionBase):
|
class ActionModule(ActionBase):
|
||||||
|
|
||||||
|
@ -49,7 +51,7 @@ class ActionModule(ActionBase):
|
||||||
"letters, numbers and underscores." % k)
|
"letters, numbers and underscores." % k)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
if isinstance(v, string_types) and v.lower() in ('true', 'false', 'yes', 'no'):
|
if not C.DEFAULT_JINJA2_NATIVE and isinstance(v, string_types) and v.lower() in ('true', 'false', 'yes', 'no'):
|
||||||
v = boolean(v, strict=False)
|
v = boolean(v, strict=False)
|
||||||
facts[k] = v
|
facts[k] = v
|
||||||
|
|
||||||
|
|
|
@ -17,3 +17,7 @@ export ANSIBLE_CACHE_PLUGIN=jsonfile ANSIBLE_CACHE_PLUGIN_CONNECTION="${MYTMPDIR
|
||||||
ansible-playbook -i ../../inventory "$@" set_fact_cached_1.yml
|
ansible-playbook -i ../../inventory "$@" set_fact_cached_1.yml
|
||||||
ansible-playbook -i ../../inventory "$@" set_fact_cached_2.yml
|
ansible-playbook -i ../../inventory "$@" set_fact_cached_2.yml
|
||||||
ansible-playbook -i ../../inventory --flush-cache "$@" set_fact_no_cache.yml
|
ansible-playbook -i ../../inventory --flush-cache "$@" set_fact_no_cache.yml
|
||||||
|
|
||||||
|
# Test boolean conversions in set_fact
|
||||||
|
ansible-playbook -v set_fact_bool_conv.yml
|
||||||
|
ANSIBLE_JINJA2_NATIVE=1 ansible-playbook -v set_fact_bool_conv_jinja2_native.yml
|
||||||
|
|
20
test/integration/targets/set_fact/set_fact_bool_conv.yml
Normal file
20
test/integration/targets/set_fact/set_fact_bool_conv.yml
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
- hosts: localhost
|
||||||
|
gather_facts: false
|
||||||
|
vars:
|
||||||
|
string_var: "no"
|
||||||
|
tasks:
|
||||||
|
- set_fact:
|
||||||
|
this_is_string: "yes"
|
||||||
|
this_is_not_string: yes
|
||||||
|
this_is_also_string: "{{ string_var }}"
|
||||||
|
this_is_another_string: !!str "{% set thing = '' + string_var + '' %}{{ thing }}"
|
||||||
|
this_is_more_strings: '{{ string_var + "" }}'
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- string_var == 'no'
|
||||||
|
- this_is_string == True
|
||||||
|
- this_is_not_string == True
|
||||||
|
- this_is_also_string == False
|
||||||
|
- this_is_another_string == False
|
||||||
|
- this_is_more_strings == False
|
|
@ -0,0 +1,20 @@
|
||||||
|
- hosts: localhost
|
||||||
|
gather_facts: false
|
||||||
|
vars:
|
||||||
|
string_var: "no"
|
||||||
|
tasks:
|
||||||
|
- set_fact:
|
||||||
|
this_is_string: "yes"
|
||||||
|
this_is_not_string: yes
|
||||||
|
this_is_also_string: "{{ string_var }}"
|
||||||
|
this_is_another_string: !!str "{% set thing = '' + string_var + '' %}{{ thing }}"
|
||||||
|
this_is_more_strings: '{{ string_var + "" }}'
|
||||||
|
|
||||||
|
- assert:
|
||||||
|
that:
|
||||||
|
- string_var == 'no'
|
||||||
|
- this_is_string == 'yes'
|
||||||
|
- this_is_not_string == True
|
||||||
|
- this_is_also_string == 'no'
|
||||||
|
- this_is_another_string == 'no'
|
||||||
|
- this_is_more_strings == 'no'
|
Loading…
Reference in a new issue