Use templating in HostVarsVars __repr__ (#64282)

* Fix HostVarsVars templating

* Add some tests for HostVars and HostVarsVars templating

* changelog
This commit is contained in:
Sloane Hertel 2019-11-01 15:51:34 -04:00 committed by GitHub
parent 5dff5603af
commit 371d7aae31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 1 deletions

View file

@ -0,0 +1,3 @@
---
bugfixes:
- HostVarsVars - Template the __repr__ value (https://github.com/ansible/ansible/issues/64128).

View file

@ -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))

View file

@ -0,0 +1,7 @@
---
x: 100
y: "{{ x }}"
nested_x:
value:
x: 100
nested_y: "{{ nested_x }}"

View file

@ -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 "$@"

View file

@ -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