Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
7ecab223
Commit
7ecab223
authored
Jan 10, 2013
by
Daniel Hokka Zakrisson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move sudo command making to one common function
parent
c339434e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
35 deletions
+27
-35
lib/ansible/runner/connection_plugins/local.py
+4
-3
lib/ansible/runner/connection_plugins/paramiko_ssh.py
+2
-16
lib/ansible/runner/connection_plugins/ssh.py
+3
-16
lib/ansible/utils/__init__.py
+18
-0
No files found.
lib/ansible/runner/connection_plugins/local.py
View file @
7ecab223
...
...
@@ -21,6 +21,7 @@ import pipes
import
shutil
import
subprocess
from
ansible
import
errors
from
ansible
import
utils
from
ansible.callbacks
import
vvv
class
Connection
(
object
):
...
...
@@ -48,11 +49,11 @@ class Connection(object):
# to do so. The primary usage of the local connection is for crontab and kickstart usage however
# so this doesn't seem to be a huge priority
raise
errors
.
AnsibleError
(
"sudo with password is presently only supported on the 'paramiko' (SSH) and native 'ssh' connection types"
)
sudocmd
=
"sudo -u
%
s -s
%
s -c
%
s"
%
(
sudo_user
,
executable
,
cmd
)
local_cmd
=
[
'/bin/sh'
,
'-c'
,
sudocmd
]
local_cmd
,
prompt
=
utils
.
make_sudo_cmd
(
sudo_user
,
executable
,
cmd
)
vvv
(
"EXEC
%
s"
%
local_cmd
,
host
=
self
.
host
)
p
=
subprocess
.
Popen
(
local_cmd
,
cwd
=
self
.
runner
.
basedir
,
executable
=
executable
,
p
=
subprocess
.
Popen
(
local_cmd
,
shell
=
isinstance
(
local_cmd
,
basestring
),
cwd
=
self
.
runner
.
basedir
,
executable
=
executable
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
stdout
,
stderr
=
p
.
communicate
()
return
(
p
.
returncode
,
''
,
stdout
,
stderr
)
...
...
lib/ansible/runner/connection_plugins/paramiko_ssh.py
View file @
7ecab223
...
...
@@ -22,6 +22,7 @@ import socket
import
random
from
ansible.callbacks
import
vvv
from
ansible
import
errors
from
ansible
import
utils
# prevent paramiko warning noise -- see http://stackoverflow.com/questions/3920502/
HAVE_PARAMIKO
=
False
...
...
@@ -117,22 +118,7 @@ class Connection(object):
vvv
(
"EXEC
%
s"
%
quoted_command
,
host
=
self
.
host
)
chan
.
exec_command
(
quoted_command
)
else
:
# Rather than detect if sudo wants a password this time, -k makes
# sudo always ask for a password if one is required.
# Passing a quoted compound command to sudo (or sudo -s)
# directly doesn't work, so we shellquote it with pipes.quote()
# and pass the quoted string to the user's shell. We loop reading
# output until we see the randomly-generated sudo prompt set with
# the -p option.
randbits
=
''
.
join
(
chr
(
random
.
randint
(
ord
(
'a'
),
ord
(
'z'
)))
for
x
in
xrange
(
32
))
prompt
=
'[sudo via ansible, key=
%
s] password: '
%
randbits
sudocmd
=
'sudo -k && sudo -p "
%
s" -u
%
s '
%
(
prompt
,
sudo_user
)
if
executable
:
sudocmd
+=
"
%
s -c
%
s"
%
(
executable
,
pipes
.
quote
(
cmd
))
else
:
sudocmd
+=
cmd
shcmd
=
'/bin/sh -c '
+
pipes
.
quote
(
sudocmd
)
shcmd
,
prompt
=
utils
.
make_sudo_cmd
(
sudo_user
,
executable
,
cmd
)
vvv
(
"EXEC
%
s"
%
shcmd
,
host
=
self
.
host
)
sudo_output
=
''
try
:
...
...
lib/ansible/runner/connection_plugins/ssh.py
View file @
7ecab223
...
...
@@ -26,6 +26,7 @@ import fcntl
import
ansible.constants
as
C
from
ansible.callbacks
import
vvv
from
ansible
import
errors
from
ansible
import
utils
class
Connection
(
object
):
''' ssh based connections '''
...
...
@@ -93,22 +94,8 @@ class Connection(object):
else
:
ssh_cmd
.
append
(
cmd
)
else
:
# Rather than detect if sudo wants a password this time, -k makes
# sudo always ask for a password if one is required.
# Passing a quoted compound command to sudo (or sudo -s)
# directly doesn't work, so we shellquote it with pipes.quote()
# and pass the quoted string to the user's shell. We loop reading
# output until we see the randomly-generated sudo prompt set with
# the -p option.
randbits
=
''
.
join
(
chr
(
random
.
randint
(
ord
(
'a'
),
ord
(
'z'
)))
for
x
in
xrange
(
32
))
prompt
=
'[sudo via ansible, key=
%
s] password: '
%
randbits
sudocmd
=
'sudo -k && sudo -p "
%
s" -u
%
s '
%
(
prompt
,
sudo_user
)
if
executable
:
sudocmd
+=
"
%
s -c
%
s"
%
(
executable
,
pipes
.
quote
(
cmd
))
else
:
sudocmd
+=
cmd
ssh_cmd
.
append
(
'/bin/sh -c '
+
pipes
.
quote
(
sudocmd
))
sudocmd
,
prompt
=
utils
.
make_sudo_cmd
(
sudo_user
,
executable
,
cmd
)
ssh_cmd
.
append
(
sudocmd
)
vvv
(
"EXEC
%
s"
%
ssh_cmd
,
host
=
self
.
host
)
try
:
...
...
lib/ansible/utils/__init__.py
View file @
7ecab223
...
...
@@ -31,6 +31,8 @@ import StringIO
import
stat
import
termios
import
tty
import
pipes
import
random
VERBOSITY
=
0
...
...
@@ -529,3 +531,19 @@ def compile_when_to_only_if(expression):
else
:
raise
errors
.
AnsibleError
(
"invalid usage of when_ operator:
%
s"
%
expression
)
def
make_sudo_cmd
(
sudo_user
,
executable
,
cmd
):
"""
helper function for connection plugins to create sudo commands
"""
# Rather than detect if sudo wants a password this time, -k makes
# sudo always ask for a password if one is required.
# Passing a quoted compound command to sudo (or sudo -s)
# directly doesn't work, so we shellquote it with pipes.quote()
# and pass the quoted string to the user's shell. We loop reading
# output until we see the randomly-generated sudo prompt set with
# the -p option.
randbits
=
''
.
join
(
chr
(
random
.
randint
(
ord
(
'a'
),
ord
(
'z'
)))
for
x
in
xrange
(
32
))
prompt
=
'[sudo via ansible, key=
%
s] password: '
%
randbits
sudocmd
=
'sudo -k && sudo -S -p "
%
s" -u
%
s
%
s -c
%
s'
%
(
prompt
,
sudo_user
,
executable
or
'$SHELL'
,
pipes
.
quote
(
cmd
))
return
(
'/bin/sh -c '
+
pipes
.
quote
(
sudocmd
),
prompt
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment