From 4a262efbb4445c252c667b513c24d1e87ed03c35 Mon Sep 17 00:00:00 2001 From: Jimmy McCrory Date: Thu, 21 Jun 2018 17:33:52 -0700 Subject: [PATCH] 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]: https://github.com/ansible/ansible/commit/23b1dbacaf8a20f7147de0f67f8d6c67de2142ef Co-Authored-By: Markos Chandras Change-Id: I16bc76f17ae5d2da4803fd7cdcdcfe72e77f9df1 --- connection/ssh.py | 29 +++++++++++++---------------- strategy/linear.py | 2 +- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/connection/ssh.py b/connection/ssh.py index 95b4512c..7f12169c 100644 --- a/connection/ssh.py +++ b/connection/ssh.py @@ -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) diff --git a/strategy/linear.py b/strategy/linear.py index dbf7d7c7..67a3a0e4 100644 --- a/strategy/linear.py +++ b/strategy/linear.py @@ -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