Merge pull request #9182 from bbasleeper/feature_sudo_exe_from_inventory

Add a new inventory parameter (ansible_sudo_exe) to specify sudo command...
pull/9106/merge
Toshio Kuratomi 10 years ago
commit 0af750e3e5

@ -207,6 +207,8 @@ mentioned::
The ssh password to use (this is insecure, we strongly recommend using --ask-pass or SSH keys) The ssh password to use (this is insecure, we strongly recommend using --ask-pass or SSH keys)
ansible_sudo_pass ansible_sudo_pass
The sudo password to use (this is insecure, we strongly recommend using --ask-sudo-pass) The sudo password to use (this is insecure, we strongly recommend using --ask-sudo-pass)
ansible_sudo_exe (new in version 1.8)
The sudo command path.
ansible_connection ansible_connection
Connection type of the host. Candidates are local, ssh or paramiko. The default is paramiko before Ansible 1.2, and 'smart' afterwards which detects whether usage of 'ssh' would be feasible based on whether ControlPersist is supported. Connection type of the host. Candidates are local, ssh or paramiko. The default is paramiko before Ansible 1.2, and 'smart' afterwards which detects whether usage of 'ssh' would be feasible based on whether ControlPersist is supported.
ansible_ssh_private_key_file ansible_ssh_private_key_file

@ -154,6 +154,7 @@ class Runner(object):
run_hosts=None, # an optional list of pre-calculated hosts to run on run_hosts=None, # an optional list of pre-calculated hosts to run on
no_log=False, # option to enable/disable logging for a given task no_log=False, # option to enable/disable logging for a given task
run_once=False, # option to enable/disable host bypass loop for a given task run_once=False, # option to enable/disable host bypass loop for a given task
sudo_exe=C.DEFAULT_SUDO_EXE, # ex: /usr/local/bin/sudo
): ):
# used to lock multiprocess inputs and outputs at various levels # used to lock multiprocess inputs and outputs at various levels
@ -212,6 +213,7 @@ class Runner(object):
self.vault_pass = vault_pass self.vault_pass = vault_pass
self.no_log = no_log self.no_log = no_log
self.run_once = run_once self.run_once = run_once
self.sudo_exe = sudo_exe
if self.transport == 'smart': if self.transport == 'smart':
# If the transport is 'smart', check to see if certain conditions # If the transport is 'smart', check to see if certain conditions
@ -814,6 +816,7 @@ class Runner(object):
self.sudo_pass = inject.get('ansible_sudo_pass', self.sudo_pass) self.sudo_pass = inject.get('ansible_sudo_pass', self.sudo_pass)
self.su = inject.get('ansible_su', self.su) self.su = inject.get('ansible_su', self.su)
self.su_pass = inject.get('ansible_su_pass', self.su_pass) self.su_pass = inject.get('ansible_su_pass', self.su_pass)
self.sudo_exe = inject.get('ansible_sudo_exe', self.sudo_exe)
# select default root user in case self.sudo requested # select default root user in case self.sudo requested
# but no user specified; happens e.g. in host vars when # but no user specified; happens e.g. in host vars when

@ -239,7 +239,7 @@ class Connection(object):
executable = constants.DEFAULT_EXECUTABLE executable = constants.DEFAULT_EXECUTABLE
if self.runner.sudo and sudoable and sudo_user: if self.runner.sudo and sudoable and sudo_user:
cmd, prompt, success_key = utils.make_sudo_cmd(sudo_user, executable, cmd) cmd, prompt, success_key = utils.make_sudo_cmd(self.runner.sudo_exe, sudo_user, executable, cmd)
vvv("EXEC COMMAND %s" % cmd) vvv("EXEC COMMAND %s" % cmd)

@ -57,7 +57,7 @@ class Connection(object):
else: else:
local_cmd = cmd local_cmd = cmd
else: else:
local_cmd, prompt, success_key = utils.make_sudo_cmd(sudo_user, executable, cmd) local_cmd, prompt, success_key = utils.make_sudo_cmd(self.runner.sudo_exe, sudo_user, executable, cmd)
executable = executable.split()[0] if executable else None executable = executable.split()[0] if executable else None
vvv("EXEC %s" % (local_cmd), host=self.host) vvv("EXEC %s" % (local_cmd), host=self.host)

@ -225,7 +225,7 @@ class Connection(object):
width=int(os.getenv('COLUMNS', 0)), width=int(os.getenv('COLUMNS', 0)),
height=int(os.getenv('LINES', 0))) height=int(os.getenv('LINES', 0)))
if self.runner.sudo or sudoable: if self.runner.sudo or sudoable:
shcmd, prompt, success_key = utils.make_sudo_cmd(sudo_user, executable, cmd) shcmd, prompt, success_key = utils.make_sudo_cmd(self.runner.sudo_exe, sudo_user, executable, cmd)
elif self.runner.su or su: elif self.runner.su or su:
shcmd, prompt, success_key = utils.make_su_cmd(su_user, executable, cmd) shcmd, prompt, success_key = utils.make_su_cmd(su_user, executable, cmd)

@ -283,7 +283,7 @@ class Connection(object):
else: else:
ssh_cmd.append(cmd) ssh_cmd.append(cmd)
else: else:
sudocmd, prompt, success_key = utils.make_sudo_cmd(sudo_user, executable, cmd) sudocmd, prompt, success_key = utils.make_sudo_cmd(self.runner.sudo_exe, sudo_user, executable, cmd)
ssh_cmd.append(sudocmd) ssh_cmd.append(sudocmd)
vvv("EXEC %s" % ' '.join(ssh_cmd), host=self.host) vvv("EXEC %s" % ' '.join(ssh_cmd), host=self.host)

@ -1175,7 +1175,7 @@ def boolean(value):
else: else:
return False return False
def make_sudo_cmd(sudo_user, executable, cmd): def make_sudo_cmd(sudo_exe, sudo_user, executable, cmd):
""" """
helper function for connection plugins to create sudo commands helper function for connection plugins to create sudo commands
""" """
@ -1190,7 +1190,7 @@ def make_sudo_cmd(sudo_user, executable, cmd):
prompt = '[sudo via ansible, key=%s] password: ' % randbits prompt = '[sudo via ansible, key=%s] password: ' % randbits
success_key = 'SUDO-SUCCESS-%s' % randbits success_key = 'SUDO-SUCCESS-%s' % randbits
sudocmd = '%s -k && %s %s -S -p "%s" -u %s %s -c %s' % ( sudocmd = '%s -k && %s %s -S -p "%s" -u %s %s -c %s' % (
C.DEFAULT_SUDO_EXE, C.DEFAULT_SUDO_EXE, C.DEFAULT_SUDO_FLAGS, sudo_exe, sudo_exe, C.DEFAULT_SUDO_FLAGS,
prompt, sudo_user, executable or '$SHELL', pipes.quote('echo %s; %s' % (success_key, cmd))) prompt, sudo_user, executable or '$SHELL', pipes.quote('echo %s; %s' % (success_key, cmd)))
return ('/bin/sh -c ' + pipes.quote(sudocmd), prompt, success_key) return ('/bin/sh -c ' + pipes.quote(sudocmd), prompt, success_key)

@ -471,7 +471,7 @@ class TestUtils(unittest.TestCase):
self.assertEqual(ansible.utils.boolean("foo"), False) self.assertEqual(ansible.utils.boolean("foo"), False)
def test_make_sudo_cmd(self): def test_make_sudo_cmd(self):
cmd = ansible.utils.make_sudo_cmd('root', '/bin/sh', '/bin/ls') cmd = ansible.utils.make_sudo_cmd(C.DEFAULT_SUDO_EXE, 'root', '/bin/sh', '/bin/ls')
self.assertTrue(isinstance(cmd, tuple)) self.assertTrue(isinstance(cmd, tuple))
self.assertEqual(len(cmd), 3) self.assertEqual(len(cmd), 3)
self.assertTrue('-u root' in cmd[0]) self.assertTrue('-u root' in cmd[0])

Loading…
Cancel
Save