template hostvars for set_host_overrides (#23839)
* fixes #23586 * temporary solution until connection-specific vars are handled by play_context
This commit is contained in:
parent
79b9c585ea
commit
02ed223d49
1 changed files with 7 additions and 5 deletions
|
@ -31,6 +31,7 @@ from ansible.module_utils.six import iteritems, string_types, binary_type
|
||||||
from ansible.module_utils._text import to_text
|
from ansible.module_utils._text import to_text
|
||||||
from ansible.playbook.conditional import Conditional
|
from ansible.playbook.conditional import Conditional
|
||||||
from ansible.playbook.task import Task
|
from ansible.playbook.task import Task
|
||||||
|
from ansible.plugins.connection import ConnectionBase
|
||||||
from ansible.template import Templar
|
from ansible.template import Templar
|
||||||
from ansible.utils.encrypt import key_for_hostname
|
from ansible.utils.encrypt import key_for_hostname
|
||||||
from ansible.utils.listify import listify_lookup_plugin_terms
|
from ansible.utils.listify import listify_lookup_plugin_terms
|
||||||
|
@ -476,15 +477,16 @@ class TaskExecutor:
|
||||||
self._play_context.remote_addr != self._connection._play_context.remote_addr):
|
self._play_context.remote_addr != self._connection._play_context.remote_addr):
|
||||||
self._connection = self._get_connection(variables=variables, templar=templar)
|
self._connection = self._get_connection(variables=variables, templar=templar)
|
||||||
hostvars = variables.get('hostvars', None)
|
hostvars = variables.get('hostvars', None)
|
||||||
if hostvars:
|
# only template the vars if the connection actually implements set_host_overrides
|
||||||
|
# NB: this is expensive, and should be removed once connection-specific vars are being handled by play_context
|
||||||
|
sho_impl = getattr(type(self._connection), 'set_host_overrides', None)
|
||||||
|
if hostvars and sho_impl and sho_impl != ConnectionBase.set_host_overrides:
|
||||||
try:
|
try:
|
||||||
target_hostvars = hostvars.raw_get(self._host.name)
|
target_hostvars = hostvars.get(self._host.name)
|
||||||
except:
|
except:
|
||||||
# FIXME: this should catch the j2undefined error here
|
# FIXME: this should catch the j2undefined error here
|
||||||
# specifically instead of all exceptions
|
# specifically instead of all exceptions
|
||||||
target_hostvars = dict()
|
target_hostvars = dict()
|
||||||
else:
|
|
||||||
target_hostvars = dict()
|
|
||||||
self._connection.set_host_overrides(host=self._host, hostvars=target_hostvars)
|
self._connection.set_host_overrides(host=self._host, hostvars=target_hostvars)
|
||||||
else:
|
else:
|
||||||
# if connection is reused, its _play_context is no longer valid and needs
|
# if connection is reused, its _play_context is no longer valid and needs
|
||||||
|
|
Loading…
Reference in a new issue