Fixing some bugs in _compute_delegate() in TaskExecutor

* Clearing interpreter settings from variables, so those set for the
  original host aren't incorrectly applied to the delegated to host
* Fixed incorrect string for remote user in delegated hosts hostvars
* Properly looking for multiple possiblities in the delegated-to hosts
  hostvars (ansible_ssh_host vs. ansible_host)
This commit is contained in:
James Cammarata 2015-09-18 11:30:20 -04:00
parent 5dd61ad3fe
commit fa69e8ebb8

View file

@ -545,23 +545,40 @@ class TaskExecutor:
else: else:
this_info = variables['hostvars'][self._task.delegate_to] this_info = variables['hostvars'][self._task.delegate_to]
# get the real ssh_address for the delegate and allow ansible_ssh_host to be templated
self._play_context.remote_addr = this_info.get('ansible_ssh_host', self._task.delegate_to)
self._play_context.remote_user = this_info.get('ansible_remote_user', self._task.remote_user)
self._play_context.port = this_info.get('ansible_ssh_port', self._play_context.port)
self._play_context.password = this_info.get('ansible_ssh_pass', self._play_context.password)
self._play_context.private_key_file = this_info.get('ansible_ssh_private_key_file', self._play_context.private_key_file)
self._play_context.become_pass = this_info.get('ansible_sudo_pass', self._play_context.become_pass)
conn = this_info.get('ansible_connection', self._task.connection)
if conn:
self._play_context.connection = conn
except Exception as e: except Exception as e:
# make sure the inject is empty for non-inventory hosts # make sure the inject is empty for non-inventory hosts
this_info = {} this_info = {}
self._display.debug("Delegate to lookup failed due to: %s" % str(e)) self._display.debug("Delegate to lookup failed due to: %s" % str(e))
conn = this_info.get('ansible_connection')
if conn:
self._play_context.connection = conn
if conn in ('smart', 'paramiko'):
# smart and paramiko connections will be using some kind of ssh,
# so use 'ssh' for the string to check connection variables
conn_test = 'ssh'
else:
conn_test = conn
else:
# default to ssh for the connection variable test, as
# that's the historical default
conn_test = 'ssh'
# get the real ssh_address for the delegate and allow ansible_ssh_host to be templated
self._play_context.remote_addr = this_info.get('ansible_%s_host' % conn_test, this_info.get('ansible_host', self._task.delegate_to))
self._play_context.remote_user = this_info.get('ansible_%s_user' % conn_test, this_info.get('ansible_user', self._play_context.remote_user))
self._play_context.port = this_info.get('ansible_%s_port' % conn_test, this_info.get('ansible_port', self._play_context.port))
self._play_context.password = this_info.get('ansible_%s_pass' % conn_test, this_info.get('ansible_pass', self._play_context.password))
self._play_context.private_key_file = this_info.get('ansible_%s_private_key_file' % conn_test, self._play_context.private_key_file)
# because of the switch from su/sudo -> become, the become pass for the
# delegated-to host may be in one of several fields, so try each until
# (maybe) one is found.
for become_pass in ('ansible_become_password', 'ansible_become_pass', 'ansible_sudo_password', 'ansible_sudo_pass'):
if become_pass in this_info:
self._play_context.become_pass = this_info[become_pass]
break
# Last chance to get private_key_file from global variables. # Last chance to get private_key_file from global variables.
# this is useful if delegated host is not defined in the inventory # this is useful if delegated host is not defined in the inventory
if self._play_context.private_key_file is None: if self._play_context.private_key_file is None:
@ -572,6 +589,13 @@ class TaskExecutor:
if key: if key:
self._play_context.private_key_file = os.path.expanduser(key) self._play_context.private_key_file = os.path.expanduser(key)
# since we're delegating, we don't want to use interpreter values
# which would have been set for the original target host
for i in variables.keys():
if i.startswith('ansible_') and i.endswith('_interpreter'):
del variables[i]
# now replace the interpreter values with those that may have come
# from the delegated-to host
for i in this_info: for i in this_info:
if i.startswith("ansible_") and i.endswith("_interpreter"): if i.startswith("ansible_") and i.endswith("_interpreter"):
variables[i] = this_info[i] variables[i] = this_info[i]