Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made chroot exe configurable #53147

Merged
merged 2 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/chroot_exe.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- Add config option for chroot binary for chroot connection plugin
28 changes: 22 additions & 6 deletions lib/ansible/plugins/connection/chroot.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,31 @@
- name: ANSIBLE_EXECUTABLE
vars:
- name: ansible_executable
default: /bin/sh
chroot_exe:
version_added: '2.8'
description:
- User specified chroot binary
ini:
- section: chroot_connection
key: exe
env:
- name: ANSIBLE_CHROOT_EXE
vars:
- name: ansible_chroot_exe
default: chroot
"""

import distutils.spawn
import os
import os.path
import subprocess
import traceback

from ansible import constants as C
from ansible.errors import AnsibleError
from ansible.module_utils.basic import is_executable
from ansible.module_utils.common.process import get_bin_path
from ansible.module_utils.six.moves import shlex_quote
from ansible.module_utils._text import to_bytes
from ansible.module_utils._text import to_bytes, to_native
from ansible.plugins.connection import ConnectionBase, BUFSIZE
from ansible.utils.display import Display

Expand Down Expand Up @@ -84,9 +96,13 @@ def __init__(self, play_context, new_stdin, *args, **kwargs):
if not (is_executable(chrootsh) or (os.path.lexists(chrootsh) and os.path.islink(chrootsh))):
raise AnsibleError("%s does not look like a chrootable dir (/bin/sh missing)" % self.chroot)

self.chroot_cmd = distutils.spawn.find_executable('chroot')
if os.path.isabs(self.get_option('chroot_exe')):
self.chroot_cmd = self.get_option('chroot_exe')
else:
self.chroot_cmd = get_bin_path(self.get_option('chroot_exe'))

if not self.chroot_cmd:
raise AnsibleError("chroot command not found in PATH")
raise AnsibleError("chroot command (%s) not found in PATH" % to_native(self.get_option('chroot_exe')))

def _connect(self):
''' connect to the chroot; nothing to do here '''
Expand All @@ -103,7 +119,7 @@ def _buffered_exec_command(self, cmd, stdin=subprocess.PIPE):
compared to exec_command() it looses some niceties like being able to
return the process's exit code immediately.
'''
executable = C.DEFAULT_EXECUTABLE.split()[0] if C.DEFAULT_EXECUTABLE else '/bin/sh'
executable = self.get_option('executable')
bcoca marked this conversation as resolved.
Show resolved Hide resolved
local_cmd = [self.chroot_cmd, self.chroot, executable, '-c', cmd]

display.vvv("EXEC %s" % (local_cmd), host=self.chroot)
Expand Down