Use templating in HostVarsVars __repr__ (#64282)
* Fix HostVarsVars templating * Add some tests for HostVars and HostVarsVars templating * changelog
This commit is contained in:
parent
5dff5603af
commit
371d7aae31
5 changed files with 73 additions and 1 deletions
3
changelogs/fragments/64282-hostvarsvars-templating.yaml
Normal file
3
changelogs/fragments/64282-hostvarsvars-templating.yaml
Normal file
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
bugfixes:
|
||||
- HostVarsVars - Template the __repr__ value (https://github.com/ansible/ansible/issues/64128).
|
|
@ -134,4 +134,5 @@ class HostVarsVars(Mapping):
|
|||
return len(self._vars.keys())
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self._vars)
|
||||
templar = Templar(variables=self._vars, loader=self._loader)
|
||||
return repr(templar.template(self._vars, fail_on_undefined=False, static_vars=STATIC_VARS))
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
x: 100
|
||||
y: "{{ x }}"
|
||||
nested_x:
|
||||
value:
|
||||
x: 100
|
||||
nested_y: "{{ nested_x }}"
|
|
@ -10,3 +10,6 @@ ansible-playbook undefined.yml -i inventory -v "$@"
|
|||
|
||||
# this should work since we dont use the variable
|
||||
ansible-playbook undall.yml -i inventory -v "$@"
|
||||
|
||||
# test hostvars templating
|
||||
ansible-playbook task_vars_templating.yml -v "$@"
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
---
|
||||
- hosts: localhost
|
||||
connection: local
|
||||
gather_facts: no
|
||||
tasks:
|
||||
- add_host:
|
||||
name: host1
|
||||
ansible_connection: local
|
||||
ansible_host: 127.0.0.1
|
||||
|
||||
- hosts: all
|
||||
gather_facts: no
|
||||
tasks:
|
||||
- debug:
|
||||
msg: "{{ hostvars['host1']['x'] }}"
|
||||
register: x_1
|
||||
- debug:
|
||||
msg: "{{ hostvars['host1']['y'] }}"
|
||||
register: y_1
|
||||
- debug:
|
||||
msg: "{{ hostvars_['x'] }}"
|
||||
vars:
|
||||
hostvars_: "{{ hostvars['host1'] }}"
|
||||
register: x_2
|
||||
- debug:
|
||||
msg: "{{ hostvars_['y'] }}"
|
||||
vars:
|
||||
hostvars_: "{{ hostvars['host1'] }}"
|
||||
register: y_2
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- x_1 == x_2
|
||||
- y_1 == y_2
|
||||
- x_1 == y_1
|
||||
|
||||
- debug:
|
||||
msg: "{{ hostvars['host1']['nested_x']['value'] }}"
|
||||
register: x_1
|
||||
- debug:
|
||||
msg: "{{ hostvars['host1']['nested_y']['value'] }}"
|
||||
register: y_1
|
||||
- debug:
|
||||
msg: "{{ hostvars_['nested_x']['value'] }}"
|
||||
vars:
|
||||
hostvars_: "{{ hostvars['host1'] }}"
|
||||
register: x_2
|
||||
- debug:
|
||||
msg: "{{ hostvars_['nested_y']['value'] }}"
|
||||
vars:
|
||||
hostvars_: "{{ hostvars['host1'] }}"
|
||||
register: y_2
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- x_1 == x_2
|
||||
- y_1 == y_2
|
||||
- x_1 == y_1
|
Loading…
Reference in a new issue