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
bb5e4fad
Commit
bb5e4fad
authored
Mar 10, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Abstracted out transport from implementation so it can be pluggable. Also fixes for output format.
parent
8808c814
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
120 additions
and
23 deletions
+120
-23
lib/ansible/connection.py
+106
-0
lib/ansible/runner.py
+10
-19
lib/ansible/utils.py
+4
-4
No files found.
lib/ansible/connection.py
0 → 100755
View file @
bb5e4fad
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
################################################
import
paramiko
import
exceptions
################################################
class
Connection
(
object
):
''' Handles abstract connections to remote hosts '''
def
__init__
(
self
,
runner
,
transport
):
self
.
runner
=
runner
self
.
transport
=
transport
def
connect
(
self
,
host
):
conn
=
None
if
self
.
transport
==
'paramiko'
:
conn
=
ParamikoConnection
(
self
.
runner
,
host
)
if
conn
is
None
:
raise
Exception
(
"unsupported connection type"
)
return
conn
.
connect
()
################################################
class
AnsibleConnectionException
(
exceptions
.
Exception
):
''' Subclass of exception for catching in Runner() code '''
def
__init__
(
self
,
value
):
self
.
value
=
value
def
__str__
(
self
):
return
repr
(
self
.
value
)
################################################
# want to implement another connection type?
# follow duck-typing of ParamikoConnection
# you may wish to read config files in __init__
# if you have any. Paramiko does not need any.
class
ParamikoConnection
(
object
):
''' SSH based connections with Paramiko '''
def
__init__
(
self
,
runner
,
host
):
self
.
ssh
=
None
self
.
runner
=
runner
self
.
host
=
host
def
connect
(
self
):
''' connect to the remote host '''
self
.
ssh
=
paramiko
.
SSHClient
()
self
.
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
try
:
self
.
ssh
.
connect
(
self
.
host
,
username
=
self
.
runner
.
remote_user
,
allow_agent
=
True
,
look_for_keys
=
True
,
password
=
self
.
runner
.
remote_pass
,
timeout
=
self
.
runner
.
timeout
)
except
Exception
,
e
:
raise
AnsibleConnectionException
(
str
(
e
))
return
self
def
exec_command
(
self
,
cmd
):
''' run a command on the remote host '''
stdin
,
stdout
,
stderr
=
self
.
ssh
.
exec_command
(
cmd
)
return
(
stdin
,
stdout
,
stderr
)
def
put_file
(
self
,
in_path
,
out_path
):
''' transfer a file from local to remote '''
sftp
=
self
.
ssh
.
open_sftp
()
sftp
.
put
(
in_path
,
out_path
)
sftp
.
close
()
def
close
(
self
):
''' terminate the connection '''
self
.
ssh
.
close
()
############################################
# add other connection types here
lib/ansible/runner.py
View file @
bb5e4fad
...
@@ -28,9 +28,9 @@ import multiprocessing
...
@@ -28,9 +28,9 @@ import multiprocessing
import
signal
import
signal
import
os
import
os
import
ansible.constants
as
C
import
ansible.constants
as
C
import
ansible.connection
import
Queue
import
Queue
import
random
import
random
import
paramiko
import
jinja2
import
jinja2
from
ansible.utils
import
*
from
ansible.utils
import
*
...
@@ -66,6 +66,7 @@ class Runner(object):
...
@@ -66,6 +66,7 @@ class Runner(object):
background
=
0
,
background
=
0
,
basedir
=
None
,
basedir
=
None
,
setup_cache
=
{},
setup_cache
=
{},
transport
=
'paramiko'
,
verbose
=
False
):
verbose
=
False
):
'''
'''
...
@@ -105,6 +106,7 @@ class Runner(object):
...
@@ -105,6 +106,7 @@ class Runner(object):
random
.
seed
()
random
.
seed
()
self
.
generated_jid
=
str
(
random
.
randint
(
0
,
999999999999
))
self
.
generated_jid
=
str
(
random
.
randint
(
0
,
999999999999
))
self
.
connector
=
ansible
.
connection
.
Connection
(
self
,
transport
)
@classmethod
@classmethod
def
parse_hosts
(
cls
,
host_list
):
def
parse_hosts
(
cls
,
host_list
):
...
@@ -159,19 +161,13 @@ class Runner(object):
...
@@ -159,19 +161,13 @@ class Runner(object):
def
_connect
(
self
,
host
):
def
_connect
(
self
,
host
):
'''
'''
obtains a
paramiko
connection to the host.
obtains a connection to the host.
on success, returns (True, connection)
on success, returns (True, connection)
on failure, returns (False, traceback str)
on failure, returns (False, traceback str)
'''
'''
ssh
=
paramiko
.
SSHClient
()
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
try
:
try
:
# try paramiko
return
[
True
,
self
.
connector
.
connect
(
host
)
]
ssh
.
connect
(
host
,
username
=
self
.
remote_user
,
allow_agent
=
True
,
except
ansible
.
connection
.
AnsibleConnectionException
,
e
:
look_for_keys
=
True
,
password
=
self
.
remote_pass
,
timeout
=
self
.
timeout
)
return
[
True
,
ssh
]
except
Exception
,
e
:
# it failed somehow, return the failure string
return
[
False
,
"FAILED:
%
s"
%
str
(
e
)
]
return
[
False
,
"FAILED:
%
s"
%
str
(
e
)
]
def
_return_from_module
(
self
,
conn
,
host
,
result
):
def
_return_from_module
(
self
,
conn
,
host
,
result
):
...
@@ -195,9 +191,7 @@ class Runner(object):
...
@@ -195,9 +191,7 @@ class Runner(object):
def
_transfer_file
(
self
,
conn
,
source
,
dest
):
def
_transfer_file
(
self
,
conn
,
source
,
dest
):
''' transfers a remote file '''
''' transfers a remote file '''
self
.
remote_log
(
conn
,
'COPY remote:
%
s local:
%
s'
%
(
source
,
dest
))
self
.
remote_log
(
conn
,
'COPY remote:
%
s local:
%
s'
%
(
source
,
dest
))
sftp
=
conn
.
open_sftp
()
conn
.
put_file
(
source
,
dest
)
sftp
.
put
(
source
,
dest
)
sftp
.
close
()
def
_transfer_module
(
self
,
conn
,
tmp
,
module
):
def
_transfer_module
(
self
,
conn
,
tmp
,
module
):
'''
'''
...
@@ -217,7 +211,7 @@ class Runner(object):
...
@@ -217,7 +211,7 @@ class Runner(object):
if
type
(
args
)
==
list
:
if
type
(
args
)
==
list
:
args
=
[
str
(
x
)
for
x
in
module_args
]
args
=
[
str
(
x
)
for
x
in
module_args
]
args
=
" "
.
join
(
args
)
args
=
" "
.
join
(
args
)
inject_vars
=
self
.
setup_cache
.
get
(
conn
.
_
host
,{})
inject_vars
=
self
.
setup_cache
.
get
(
conn
.
host
,{})
# the metadata location for the setup module is transparently managed
# the metadata location for the setup module is transparently managed
# since it's an 'internals' module, kind of a black box. See playbook
# since it's an 'internals' module, kind of a black box. See playbook
...
@@ -250,7 +244,7 @@ class Runner(object):
...
@@ -250,7 +244,7 @@ class Runner(object):
# run AFTER setup use these variables for templating when executed
# run AFTER setup use these variables for templating when executed
# from playbooks
# from playbooks
if
self
.
module_name
==
'setup'
:
if
self
.
module_name
==
'setup'
:
host
=
conn
.
_
host
host
=
conn
.
host
try
:
try
:
var_result
=
json
.
loads
(
result
)
var_result
=
json
.
loads
(
result
)
except
:
except
:
...
@@ -351,7 +345,6 @@ class Runner(object):
...
@@ -351,7 +345,6 @@ class Runner(object):
if
not
ok
:
if
not
ok
:
return
[
host
,
False
,
conn
]
return
[
host
,
False
,
conn
]
conn
.
_host
=
host
tmp
=
self
.
_get_tmp_path
(
conn
)
tmp
=
self
.
_get_tmp_path
(
conn
)
result
=
None
result
=
None
if
self
.
module_name
not
in
[
'copy'
,
'template'
]:
if
self
.
module_name
not
in
[
'copy'
,
'template'
]:
...
@@ -397,9 +390,7 @@ class Runner(object):
...
@@ -397,9 +390,7 @@ class Runner(object):
os
.
path
.
join
(
self
.
module_path
,
module
)
os
.
path
.
join
(
self
.
module_path
,
module
)
)
)
out_path
=
tmp
+
module
out_path
=
tmp
+
module
sftp
=
conn
.
open_sftp
()
conn
.
put_file
(
in_path
,
out_path
)
sftp
.
put
(
in_path
,
out_path
)
sftp
.
close
()
return
out_path
return
out_path
def
match_hosts
(
self
,
pattern
):
def
match_hosts
(
self
,
pattern
):
...
...
lib/ansible/utils.py
View file @
bb5e4fad
...
@@ -54,9 +54,9 @@ def task_start_msg(name, conditional):
...
@@ -54,9 +54,9 @@ def task_start_msg(name, conditional):
def
regular_generic_msg
(
hostname
,
result
,
oneline
,
caption
):
def
regular_generic_msg
(
hostname
,
result
,
oneline
,
caption
):
''' output on the result of a module run that is not command '''
''' output on the result of a module run that is not command '''
if
not
oneline
:
if
not
oneline
:
return
"
%
s |
%
s >>
\n
%
s
"
%
(
hostname
,
caption
,
bigjson
(
result
))
return
"
%
s |
%
s >>
%
s
\n
"
%
(
hostname
,
caption
,
bigjson
(
result
))
else
:
else
:
return
"
%
s |
%
s >>
%
s"
%
(
hostname
,
caption
,
smjson
(
result
))
return
"
%
s |
%
s >>
%
s
\n
"
%
(
hostname
,
caption
,
smjson
(
result
))
def
regular_success_msg
(
hostname
,
result
,
oneline
):
def
regular_success_msg
(
hostname
,
result
,
oneline
):
''' output the result of a successful module run '''
''' output the result of a successful module run '''
...
@@ -135,9 +135,9 @@ def dark_hosts_msg(results):
...
@@ -135,9 +135,9 @@ def dark_hosts_msg(results):
''' summarize the results of all uncontactable hosts '''
''' summarize the results of all uncontactable hosts '''
buf
=
''
buf
=
''
if
len
(
results
[
'dark'
]
.
keys
())
>
0
:
if
len
(
results
[
'dark'
]
.
keys
())
>
0
:
buf
+=
"
*** Hosts which could not be contacted or did not respond: ***
"
buf
+=
"
\n
*** Hosts which could not be contacted or did not respond: ***
\n
"
for
hostname
in
results
[
'dark'
]
.
keys
():
for
hostname
in
results
[
'dark'
]
.
keys
():
buf
+=
"
%
s:
\n
%
s
\n
"
%
(
hostname
,
results
[
'dark'
][
hostname
])
buf
+=
"
%
s:
%
s
\n
"
%
(
hostname
,
results
[
'dark'
][
hostname
])
buf
+=
"
\n
"
buf
+=
"
\n
"
return
buf
return
buf
...
...
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