Skip to content

Commit

Permalink
fixed winrm to use proper task vars (#31072)
Browse files Browse the repository at this point in the history
it avoids hitting hostvars templating issue and ignoring exception
fixes #30911

also normal var precedence should work for ansible_winrm vars

(cherry picked from commit 057eec9)
  • Loading branch information
bcoca authored and jborean93 committed Sep 29, 2017
1 parent fcba6c0 commit 1665178
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
11 changes: 2 additions & 9 deletions lib/ansible/executor/task_executor.py
Expand Up @@ -476,18 +476,11 @@ def _execute(self, variables=None):
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
Expand Down
11 changes: 7 additions & 4 deletions lib/ansible/plugins/connection/winrm.py
Expand Up @@ -33,12 +33,10 @@
import os
import re
import shlex
import socket
import traceback
import json
import tempfile
import subprocess
import itertools

HAVE_KERBEROS = False
try:
Expand All @@ -54,7 +52,7 @@
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

Expand Down Expand Up @@ -100,13 +98,18 @@ def __init__(self, *args, **kwargs):

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')
Expand Down

0 comments on commit 1665178

Please sign in to comment.