Fix connection plugin for Ansible 2.6

inventory_hostname is not reliable to be used as container_name. When
delegating in 2.6+, the inventory_hostname of the delegating host is
passed to the connection plugin, additionally when a host doesn't have
its own container_name variable a cached inventory_hostname is used in
its place.

To get the connection plugin working with Ansible 2.6 this change
essentially allows the container_check to fall through when delegating
to physical hosts or hosts not in the inventory by:
- removing inventory_hostname as a fallback for container_name
- unsetting container_name when delegating to a known physical host
- unsetting container_name when delegating to a host not mentioned in
  the inventory

container_name can no longer be used as a group variable based on
inventory_hostname, it will need to be an inventory/host variable for
each individual host.

Also, when container_user is used the remote_tmp path needs to be set to
a system writable directory instead of the root user's home.

Change-Id: If2eb4c16273e19599f6ec3f0cba6b3573912c6a0
This commit is contained in:
Jimmy McCrory 2018-10-07 08:58:15 -07:00
parent d94217eb0d
commit 3eb9d79a1a
5 changed files with 17 additions and 12 deletions

View File

@ -26,7 +26,6 @@ DOCUMENTATION = '''
description: Hostname of a container
vars:
- name: container_name
- name: inventory_hostname
container_tech:
description: Container technology used by a container host
default: lxc
@ -371,11 +370,9 @@ class Connection(SSH.Connection):
super(Connection, self).set_options(task_keys=None, var_options=var_options, direct=direct)
self.chroot_path = self.get_option('chroot_path')
if var_options and \
self.get_option('container_name') == var_options.get('inventory_hostname'):
self.container_name = self.get_option('container_name')
self.physical_host = self.get_option('physical_host')
self.container_name = self.get_option('container_name')
self.container_tech = self.get_option('container_tech')
self.physical_host = self.get_option('physical_host')
# Check to see if container_user is setup first, if so use that value.
# If it isn't, then default to 'root'
@ -389,7 +386,10 @@ class Connection(SSH.Connection):
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)
if self.host in physical_host_addrs.values():
self.container_name = None
else:
self._set_physical_host_addr(physical_host_addrs)
def _set_physical_host_addr(self, physical_host_addrs):
physical_host_addr = physical_host_addrs.get(self.physical_host,

View File

@ -0,0 +1,6 @@
---
issues:
- |
When using the connection plugin's ``container_user`` option,
``ansible_remote_tmp`` should be set to a system writable path
such as '/var/tmp/'.

View File

@ -139,6 +139,7 @@ class StrategyModule(LINEAR.StrategyModule):
# This checks if we are delegating to a host which does not exist
# in the inventory (possibly using its IP address)
if delegated_host_info is None:
task_vars['container_name'] = None
continue
physical_host_vars = delegated_host_info.get_vars()
physical_host_templar = LINEAR.Templar(loader=self._loader,

View File

@ -13,8 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
container_name: "{{ inventory_hostname }}"
container_networks:
management_address:
address: "{{ ansible_host }}"

View File

@ -2,14 +2,14 @@
localhost
[all_containers]
container1
container2
container1 container_name=container1 ansible_remote_tmp=/var/tmp
container2 container_name=container2
# This is used to test I75f9d0f55ecd875caa1bf608a77c92f950b679a1
[hosts]
localhost_alt
[all_containers]
container3
container3 container_name=container3
# This is meant to test If594914df53efacc6d5bba148f4f46280f5a117d
[fake_hosts]
@ -17,4 +17,4 @@ fakehost ansible_host=1.1.1.1
[hosts:children]
fake_hosts
[fake_containers]
fakecontainer container_name="{{ inventory_hostname }}" physical_host=fakehost
fakecontainer container_name=fakecontainer physical_host=fakehost