fixed winrm to use proper task vars (#31072)

it avoids hitting hostvars templating issue and ignoring exception
fixes #30911

also normal var precedence should work for ansible_winrm vars
pull/31081/head
Brian Coca 7 years ago committed by Jordan Borean
parent 32cf69c00a
commit 057eec94ee

@ -474,18 +474,11 @@ class TaskExecutor:
not getattr(self._connection, 'connected', False) or
self._play_context.remote_addr != self._connection._play_context.remote_addr):
self._connection = self._get_connection(variables=variables, templar=templar)
hostvars = variables.get('hostvars', None)
# 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:
target_hostvars = hostvars.get(self._host.name)
except:
# FIXME: this should catch the j2undefined error here
# specifically instead of all exceptions
target_hostvars = dict()
self._connection.set_host_overrides(host=self._host, hostvars=target_hostvars)
if sho_impl and sho_impl != ConnectionBase.set_host_overrides:
self._connection.set_host_overrides(self._host, variables, templar)
else:
# if connection is reused, its _play_context is no longer valid and needs
# to be replaced with the one templated above, in case other data changed

@ -33,12 +33,10 @@ import inspect
import os
import re
import shlex
import socket
import traceback
import json
import tempfile
import subprocess
import itertools
HAVE_KERBEROS = False
try:
@ -54,7 +52,7 @@ from ansible.module_utils.six.moves.urllib.parse import urlunsplit
from ansible.module_utils._text import to_bytes, to_native, to_text
from ansible.module_utils.six import binary_type
from ansible.plugins.connection import ConnectionBase
from ansible.plugins.shell.powershell import exec_wrapper, become_wrapper, leaf_exec
from ansible.plugins.shell.powershell import leaf_exec
from ansible.utils.hashing import secure_hash
from ansible.utils.path import makedirs_safe
@ -100,13 +98,18 @@ class Connection(ConnectionBase):
super(Connection, self).__init__(*args, **kwargs)
def set_host_overrides(self, host, hostvars=None):
def set_host_overrides(self, host, variables, templar):
'''
Override WinRM-specific options from host variables.
'''
if not HAS_WINRM:
return
hostvars = {}
for k in variables:
if k.startswith('ansible_winrm'):
hostvars[k] = templar.template(variables[k])
self._winrm_host = self._play_context.remote_addr
self._winrm_port = int(self._play_context.port or 5986)
self._winrm_scheme = hostvars.get('ansible_winrm_scheme', 'http' if self._winrm_port == 5985 else 'https')

Loading…
Cancel
Save