Fix connection plugin for Ansible 2.5

For 'physical_host_addrs' to be able to be looked up by the connection
plugin it will need to be included in the 'task_vars' dictionary which
is eventually passed to the task executor. Moreover, we remove the now
obsoleted[1] 'set_host_overrides' function.

If the 'container_name' variable is available, it should take precedence
over 'inventory_hostname' for determining a container's name.

The 'container_user' variable is now set through 'set_options' instead
of a host variable and, when it is available, the connection plugin's
shell is initialized with a system writable temp directory to avoid
requiring additional privileges for the user.

The container check has been updated to also check 'container_name'
against 'ansible_host'. If these match, the container is only known by
an IP address and will require a direct SSH connection.

[1]: 23b1dbacaf
Co-Authored-By: Markos Chandras <mchandras@suse.de>
Change-Id: I16bc76f17ae5d2da4803fd7cdcdcfe72e77f9df1
This commit is contained in:
Jimmy McCrory 2018-06-21 17:33:52 -07:00
parent cef7946b3b
commit 4a262efbb4
2 changed files with 14 additions and 17 deletions

View File

@ -25,8 +25,8 @@ DOCUMENTATION = '''
container_name:
description: Hostname of a container
vars:
- name: inventory_hostname
- name: container_name
- name: inventory_hostname
container_tech:
description: Container technology used by a container host
default: lxc
@ -311,23 +311,9 @@ class Connection(SSH.Connection):
# revise this in the future.
self.container_tech = 'lxc'
# Check to see if container_user is setup first, if so use that value.
# Remote user is normally set, but if it isn't, then default to 'root'
if hasattr(self._play_context, 'container_user'):
self.container_user = self._play_context.container_user
elif self._play_context.remote_user:
self.container_user = self._play_context.remote_user
else:
self.container_user = 'root'
# Store the container pid for multi-use
self.container_pid = None
def set_host_overrides(self, host, hostvars=None, templar=None):
if self._container_check() or self._chroot_check():
physical_host_addrs = host.get_vars().get('physical_host_addrs', {})
self._set_physical_host_addr(physical_host_addrs)
def set_options(self, task_keys=None, var_options=None, direct=None):
super(Connection, self).set_options(task_keys=None, var_options=var_options, direct=direct)
@ -339,6 +325,16 @@ class Connection(SSH.Connection):
self.physical_host = self.get_option('physical_host')
self.container_tech = self.get_option('container_tech')
# Check to see if container_user is setup first, if so use that value.
# If it isn't, then default to 'root'
# The connection's shell plugin also needs to be initialized here and
# updated to use a system writable temp directory to avoid requiring
# that container_user have sudo privileges.
self.container_user = self.get_option('container_user') or 'root'
if self.container_user != 'root':
self._shell.set_options(var_options={})
self._shell.set_option('remote_tmp', self._shell.get_option('system_tmpdirs')[0])
if self._container_check() or self._chroot_check():
physical_host_addrs = self.get_option('physical_host_addrs') or {}
self._set_physical_host_addr(physical_host_addrs)
@ -402,7 +398,8 @@ class Connection(SSH.Connection):
SSH.display.vvv(
u'physical_host: "%s"' % self.physical_host
)
if self.container_name != self.physical_host:
if self.container_name != self.physical_host and \
self.container_name != self.host:
SSH.display.vvv(u'Container confirmed')
SSH.display.vvv(u'Container type "{}"'.format(
self.container_tech)

View File

@ -136,7 +136,7 @@ class StrategyModule(LINEAR.StrategyModule):
physical_host_addr = physical_host_vars.get('ansible_host',
physical_host)
physical_host_addrs[physical_host] = physical_host_addr
host.set_variable('physical_host_addrs', physical_host_addrs)
task_vars['physical_host_addrs'] = physical_host_addrs
if task.delegate_to:
# If a task uses delegation change the play_context