Q: Output is : "test2":{ "a": "1"}
A: This is correct. An output of Jinja is a string (it's a template engine). You declared test1.a
as an integer (without quotes)
- set_fact:
test1:
a: 1
- debug:
msg: "{{ test1.a|type_debug }}"
gives
msg: int
But, the evaluation of "{{ test1.a }}"
resulted in a string
- set_fact:
test2:
a: "{{ test1.a }}"
- debug:
var: test2
- debug:
msg: "{{ test2.a|type_debug }}"
give
test2:
a: '1'
msg: str
There are more options on how to preserve types.
- Use configuration option DEFAULT_JINJA2_NATIVE (default: False).
"This option preserves variable types during template operations."
This will influence the whole playbook. For example
shell> ANSIBLE_JINJA2_NATIVE=True ansible-playbook pb.yml
gives
test2:
a: 1
msg: int
- Next option is a combination of the dictionaries. For example
- set_fact:
test2: "{{ test2|default({})|combine({'a': test1.a}) }}"
- debug:
var: test2
- debug:
msg: "{{ test2.a|type_debug }}"
give
test2:
a: 1
msg: int
Single (bare) variables behave differently and preserve the types. For example
- set_fact:
v2: 1
- debug:
msg: "{{ v2 }}"
- debug:
msg: "{{ v2|type_debug }}"
- set_fact:
v3: "{{ v2 }}"
- debug:
msg: "{{ v3 }}"
- debug:
msg: "{{ v3|type_debug }}"
give
msg: 1
msg: int
msg: 1
msg: int
(I've used yaml
callback plugin "ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook pb.yml"
)