From 93a20a33e9695ce621b418723dba299fa0a87678 Mon Sep 17 00:00:00 2001 From: Jim Richardson Date: Thu, 3 May 2012 16:08:36 -0700 Subject: [PATCH] first bit of getting sudo -u functionality --- bin/ansible | 6 ++++-- lib/ansible/constants.py | 1 + lib/ansible/runner.py | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/bin/ansible b/bin/ansible index 886a9cbf7b7..86b41910d10 100755 --- a/bin/ansible +++ b/bin/ansible @@ -52,7 +52,8 @@ class Cli(object): parser.add_option('-m', '--module-name', dest='module_name', help="module name to execute (default=%s)" % C.DEFAULT_MODULE_NAME, default=C.DEFAULT_MODULE_NAME) - + parser.add_option('-U','--sudo_user', dest='sudo_user', help='desired sudo user', + default=C.DEFAULT_SUDO_USER) options, args = parser.parse_args() self.callbacks.options = options @@ -76,6 +77,7 @@ class Cli(object): sshpass = None sudopass = None + sudouser = options.sudo_user if options.ask_pass: sshpass = getpass.getpass(prompt="SSH password: ") if options.ask_sudo_pass: @@ -92,7 +94,7 @@ class Cli(object): forks=options.forks, pattern=pattern, callbacks=self.callbacks, sudo=options.sudo, - sudo_pass=sudopass, + sudo_pass=sudopass,sudo_user=options.sudo_user, transport=options.connection, debug=options.debug ) diff --git a/lib/ansible/constants.py b/lib/ansible/constants.py index c7a2bf0c9e7..aee41669061 100644 --- a/lib/ansible/constants.py +++ b/lib/ansible/constants.py @@ -33,6 +33,7 @@ DEFAULT_POLL_INTERVAL = 15 DEFAULT_REMOTE_USER = 'root' DEFAULT_REMOTE_PASS = None DEFAULT_SUDO_PASS = None +DEFAULT_SUDO_USER = 'root' DEFAULT_REMOTE_PORT = 22 DEFAULT_TRANSPORT = 'paramiko' DEFAULT_TRANSPORT_OPTS = ['local', 'paramiko'] diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py index fdb5404f98e..c09409d9a39 100644 --- a/lib/ansible/runner.py +++ b/lib/ansible/runner.py @@ -77,8 +77,8 @@ class Runner(object): remote_pass=C.DEFAULT_REMOTE_PASS, remote_port=C.DEFAULT_REMOTE_PORT, sudo_pass=C.DEFAULT_SUDO_PASS, background=0, basedir=None, setup_cache=None, transport=C.DEFAULT_TRANSPORT, conditional='True', - callbacks=None, debug=False, sudo=False, module_vars=None, - is_playbook=False, inventory=None): + callbacks=None, debug=False, sudo=False, sudo_user=C.DEFAULT_SUDO_USER + ,module_vars=None, is_playbook=False, inventory=None): """ host_list : path to a host list file, like /etc/ansible/hosts @@ -91,6 +91,7 @@ class Runner(object): remote_user : connect as this remote username remote_pass : supply this password (if not using keys) remote_port : use this default remote port (if not set by the inventory system) + sudo_user : If you want to sudo to a user other than root. sudo_pass : sudo password if using sudo and sudo requires a password background : run asynchronously with a cap of this many # of seconds (if not 0) basedir : paths used by modules if not absolute are relative to here @@ -143,6 +144,7 @@ class Runner(object): self.basedir = basedir self.sudo = sudo self.sudo_pass = sudo_pass + self.sudo_user = sudo_user self.is_playbook = is_playbook euid = pwd.getpwuid(os.geteuid())[0]