[PR #8303/136419c5 backport][stable-8] bug(lookup/merge_variables): Fix rendering foreign variables (#8338)

bug(lookup/merge_variables): Fix rendering foreign variables (#8303)

* manually prepare variables of foreign host including hostvars property

* render variables from context of current host

* add integration test for cross host merge

* lint fixes

* adjust cross host merge unit tests to provide a tiny bit of the HostVars Class API

* add license information

* lint

* add changelog fragment

* Update tests/integration/targets/lookup_merge_variables/test_cross_host_merge_play.yml

Okay

Co-authored-by: Mark <40321020+m-a-r-k-e@users.noreply.github.com>

* Update tests/integration/targets/lookup_merge_variables/test_cross_host_merge_play.yml

Okay

Co-authored-by: Mark <40321020+m-a-r-k-e@users.noreply.github.com>

* Update tests/integration/targets/lookup_merge_variables/test_cross_host_merge_play.yml

Okay

Co-authored-by: Mark <40321020+m-a-r-k-e@users.noreply.github.com>

* rename _HostVars to HostVarsMock

* removing unnecessary task

---------

Co-authored-by: Gitlab CI <alexander.petrenz@posteo.de>
Co-authored-by: Mark <40321020+m-a-r-k-e@users.noreply.github.com>
(cherry picked from commit 136419c5c0)

Co-authored-by: Alexander Petrenz <petrenz.a@gmail.com>
This commit is contained in:
patchback[bot] 2024-05-11 17:11:18 +02:00 committed by GitHub
parent 4c39295b97
commit 01a54bba2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 165 additions and 83 deletions

View file

@ -157,7 +157,9 @@ class LookupModule(LookupBase):
cross_host_merge_result = initial_value
for host in variables["hostvars"]:
if self._is_host_in_allowed_groups(variables["hostvars"][host]["group_names"]):
cross_host_merge_result = self._merge_vars(term, cross_host_merge_result, variables["hostvars"][host])
host_variables = dict(variables["hostvars"].raw_get(host))
host_variables["hostvars"] = variables["hostvars"] # re-add hostvars
cross_host_merge_result = self._merge_vars(term, cross_host_merge_result, host_variables)
ret.append(cross_host_merge_result)
return ret
@ -195,7 +197,8 @@ class LookupModule(LookupBase):
result = initial_value
for var_name in var_merge_names:
var_value = self._templar.template(variables[var_name]) # Render jinja2 templates
with self._templar.set_temporary_context(available_variables=variables): # tmp. switch renderer to context of current variables
var_value = self._templar.template(variables[var_name]) # Render jinja2 templates
var_type = _verify_and_get_type(var_value)
if prev_var_type is None: