From 057eec94ee3ae990fe63d4787f393d844d882509 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 28 Sep 2017 21:12:56 -0400 Subject: [PATCH] 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 --- lib/ansible/executor/task_executor.py | 11 ++--------- lib/ansible/plugins/connection/winrm.py | 11 +++++++---- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index 4c6fa75d0f7..cd84a55a454 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -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 diff --git a/lib/ansible/plugins/connection/winrm.py b/lib/ansible/plugins/connection/winrm.py index 50dceffafc4..500162d6a03 100644 --- a/lib/ansible/plugins/connection/winrm.py +++ b/lib/ansible/plugins/connection/winrm.py @@ -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')