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
3da6370a
Commit
3da6370a
authored
Feb 23, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use defaults better, improve/fix setup.py
parent
2c873a44
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
33 deletions
+67
-33
bin/ansible
+4
-8
lib/ansible/__init__.py
+57
-21
setup.py
+6
-4
No files found.
bin/ansible
View file @
3da6370a
...
@@ -26,8 +26,8 @@ import json
...
@@ -26,8 +26,8 @@ import json
import
os
import
os
import
ansible
import
ansible
DEFAULT_HOST_LIST
=
'
~/.ansible_
hosts'
DEFAULT_HOST_LIST
=
'
/etc/ansible/
hosts'
DEFAULT_MODULE_PATH
=
'
~
/ansible'
DEFAULT_MODULE_PATH
=
'
/usr/share
/ansible'
DEFAULT_MODULE_NAME
=
'ping'
DEFAULT_MODULE_NAME
=
'ping'
DEFAULT_PATTERN
=
'*'
DEFAULT_PATTERN
=
'*'
DEFAULT_FORKS
=
3
DEFAULT_FORKS
=
3
...
@@ -54,7 +54,6 @@ class Cli(object):
...
@@ -54,7 +54,6 @@ class Cli(object):
help
=
"hostname pattern"
,
default
=
DEFAULT_PATTERN
)
help
=
"hostname pattern"
,
default
=
DEFAULT_PATTERN
)
options
,
args
=
parser
.
parse_args
()
options
,
args
=
parser
.
parse_args
()
host_list
=
self
.
_host_list
(
options
.
host_list
)
# TODO: more shell like splitting on module_args would
# TODO: more shell like splitting on module_args would
# be a good idea
# be a good idea
...
@@ -63,15 +62,12 @@ class Cli(object):
...
@@ -63,15 +62,12 @@ class Cli(object):
module_name
=
options
.
module_name
,
module_name
=
options
.
module_name
,
module_path
=
options
.
module_path
,
module_path
=
options
.
module_path
,
module_args
=
options
.
module_args
.
split
(
' '
),
module_args
=
options
.
module_args
.
split
(
' '
),
host_list
=
host_list
,
host_list
=
options
.
host_list
,
forks
=
options
.
forks
,
forks
=
options
.
forks
,
pattern
=
options
.
pattern
,
pattern
=
options
.
pattern
,
verbose
=
False
,
)
)
def
_host_list
(
self
,
host_list
):
host_list
=
os
.
path
.
expanduser
(
host_list
)
return
file
(
host_list
)
.
read
()
.
split
(
"
\n
"
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
result
=
Cli
()
.
runner
()
.
run
()
result
=
Cli
()
.
runner
()
.
run
()
...
...
lib/ansible/__init__.py
View file @
3da6370a
...
@@ -24,19 +24,18 @@ from multiprocessing import Process, Pipe
...
@@ -24,19 +24,18 @@ from multiprocessing import Process, Pipe
from
itertools
import
izip
from
itertools
import
izip
import
os
import
os
import
json
import
json
import
traceback
# non-core
# non-core
import
paramiko
import
paramiko
# TODO -- library should have defaults, not just CLI
DEFAULT_HOST_LIST
=
'/etc/ansible/hosts'
# update Runner constructor below to use
DEFAULT_MODULE_PATH
=
'/usr/share/ansible'
DEFAULT_HOST_LIST
=
'~/.ansible_hosts'
DEFAULT_MODULE_PATH
=
'~/ansible'
DEFAULT_MODULE_NAME
=
'ping'
DEFAULT_MODULE_NAME
=
'ping'
DEFAULT_PATTERN
=
'*'
DEFAULT_PATTERN
=
'*'
DEFAULT_FORKS
=
3
DEFAULT_FORKS
=
3
DEFAULT_MODULE_ARGS
=
''
DEFAULT_MODULE_ARGS
=
''
DEFAULT_TIMEOUT
=
60
class
Pooler
(
object
):
class
Pooler
(
object
):
...
@@ -59,20 +58,40 @@ class Pooler(object):
...
@@ -59,20 +58,40 @@ class Pooler(object):
class
Runner
(
object
):
class
Runner
(
object
):
def
__init__
(
self
,
host_list
=
[],
module_path
=
None
,
def
__init__
(
self
,
module_name
=
None
,
module_args
=
[],
host_list
=
DEFAULT_HOST_LIST
,
forks
=
3
,
timeout
=
60
,
pattern
=
'*'
):
module_path
=
DEFAULT_MODULE_PATH
,
module_name
=
DEFAULT_MODULE_NAME
,
self
.
host_list
=
host_list
module_args
=
DEFAULT_MODULE_ARGS
,
forks
=
DEFAULT_FORKS
,
timeout
=
DEFAULT_TIMEOUT
,
pattern
=
DEFAULT_PATTERN
,
verbose
=
False
):
'''
Constructor.
'''
self
.
host_list
=
self
.
_parse_hosts
(
host_list
)
self
.
module_path
=
module_path
self
.
module_path
=
module_path
self
.
module_name
=
module_name
self
.
module_name
=
module_name
self
.
forks
=
forks
self
.
forks
=
forks
self
.
pattern
=
pattern
self
.
pattern
=
pattern
self
.
module_args
=
module_args
self
.
module_args
=
module_args
self
.
timeout
=
timeout
self
.
timeout
=
timeout
self
.
verbose
=
verbose
def
_parse_hosts
(
self
,
host_list
):
''' parse the host inventory file if not sent as an array '''
if
type
(
host_list
)
!=
list
:
host_list
=
os
.
path
.
expanduser
(
host_list
)
return
file
(
host_list
)
.
read
()
.
split
(
"
\n
"
)
return
host_list
def
_matches
(
self
,
host_name
):
def
_matches
(
self
,
host_name
):
''' returns if a hostname is matched by the pattern '''
if
host_name
==
''
:
if
host_name
==
''
:
return
False
return
False
if
fnmatch
.
fnmatch
(
host_name
,
self
.
pattern
):
if
fnmatch
.
fnmatch
(
host_name
,
self
.
pattern
):
...
@@ -80,6 +99,7 @@ class Runner(object):
...
@@ -80,6 +99,7 @@ class Runner(object):
return
False
return
False
def
_connect
(
self
,
host
):
def
_connect
(
self
,
host
):
''' obtains a paramiko connection to the host '''
ssh
=
paramiko
.
SSHClient
()
ssh
=
paramiko
.
SSHClient
()
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
try
:
try
:
...
@@ -87,20 +107,29 @@ class Runner(object):
...
@@ -87,20 +107,29 @@ class Runner(object):
allow_agent
=
True
,
look_for_keys
=
True
)
allow_agent
=
True
,
look_for_keys
=
True
)
return
ssh
return
ssh
except
:
except
:
# TODO -- just convert traceback to string
# and return a seperate hash of failed hosts
if
self
.
verbose
:
traceback
.
print_exc
()
return
None
return
None
def
_executor
(
self
,
host
):
def
_executor
(
self
,
host
):
''' callback executed in parallel for each host '''
# TODO: try/catch returning none
# TODO: try/catch returning none
conn
=
self
.
_connect
(
host
)
conn
=
self
.
_connect
(
host
)
if
not
conn
:
if
not
conn
:
return
[
host
,
None
]
return
[
host
,
None
]
if
self
.
module_name
!=
"copy"
:
if
self
.
module_name
!=
"copy"
:
# transfer a module, set it executable, and run it
outpath
=
self
.
_copy_module
(
conn
)
outpath
=
self
.
_copy_module
(
conn
)
self
.
_exec_command
(
conn
,
"chmod +x
%
s"
%
outpath
)
self
.
_exec_command
(
conn
,
"chmod +x
%
s"
%
outpath
)
cmd
=
self
.
_command
(
outpath
)
cmd
=
self
.
_command
(
outpath
)
result
=
self
.
_exec_command
(
conn
,
cmd
)
result
=
self
.
_exec_command
(
conn
,
cmd
)
result
=
json
.
loads
(
result
)
result
=
json
.
loads
(
result
)
else
:
else
:
# SFTP file copy module is not really a module
ftp
=
conn
.
open_sftp
()
ftp
=
conn
.
open_sftp
()
ftp
.
put
(
self
.
module_args
[
0
],
self
.
module_args
[
1
])
ftp
.
put
(
self
.
module_args
[
0
],
self
.
module_args
[
1
])
ftp
.
close
()
ftp
.
close
()
...
@@ -109,23 +138,32 @@ class Runner(object):
...
@@ -109,23 +138,32 @@ class Runner(object):
return
[
host
,
result
]
return
[
host
,
result
]
def
_command
(
self
,
outpath
):
def
_command
(
self
,
outpath
):
''' form up a command string '''
cmd
=
"
%
s
%
s"
%
(
outpath
,
" "
.
join
(
self
.
module_args
))
cmd
=
"
%
s
%
s"
%
(
outpath
,
" "
.
join
(
self
.
module_args
))
return
cmd
return
cmd
def
_exec_command
(
self
,
conn
,
cmd
):
def
_exec_command
(
self
,
conn
,
cmd
):
''' execute a command over SSH '''
stdin
,
stdout
,
stderr
=
conn
.
exec_command
(
cmd
)
stdin
,
stdout
,
stderr
=
conn
.
exec_command
(
cmd
)
results
=
stdout
.
read
()
results
=
stdout
.
read
()
return
results
return
results
def
_copy_module
(
self
,
conn
):
def
_copy_module
(
self
,
conn
):
inpath
=
os
.
path
.
expanduser
(
os
.
path
.
join
(
self
.
module_path
,
self
.
module_name
))
''' transfer a module over SFTP '''
outpath
=
os
.
path
.
join
(
"/var/spool/"
,
"ansible_
%
s"
%
self
.
module_name
)
in_path
=
os
.
path
.
expanduser
(
ftp
=
conn
.
open_sftp
()
os
.
path
.
join
(
self
.
module_path
,
self
.
module_name
)
ftp
.
put
(
inpath
,
outpath
)
)
ftp
.
close
()
out_path
=
os
.
path
.
join
(
return
outpath
"/var/spool/"
,
"ansible_
%
s"
%
self
.
module_name
)
sftp
=
conn
.
open_sftp
()
sftp
.
put
(
in_path
,
out_path
)
sftp
.
close
()
return
out_path
def
run
(
self
):
def
run
(
self
):
''' xfer & run module on all matched hosts '''
hosts
=
[
h
for
h
in
self
.
host_list
if
self
.
_matches
(
h
)
]
hosts
=
[
h
for
h
in
self
.
host_list
if
self
.
_matches
(
h
)
]
def
executor
(
x
):
def
executor
(
x
):
return
self
.
_executor
(
x
)
return
self
.
_executor
(
x
)
...
@@ -136,12 +174,10 @@ class Runner(object):
...
@@ -136,12 +174,10 @@ class Runner(object):
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
# test code...
# TODO: if host list is string load from file
r
=
Runner
(
r
=
Runner
(
host_list
=
[
'127.0.0.1'
],
host_list
=
DEFAULT_HOST_LIST
,
module_path
=
'~/ansible'
,
module_name
=
'ping'
,
module_name
=
'ping'
,
module_args
=
''
,
module_args
=
''
,
pattern
=
'*'
,
pattern
=
'*'
,
...
...
setup.py
View file @
3da6370a
...
@@ -14,10 +14,12 @@ setup(name='ansible',
...
@@ -14,10 +14,12 @@ setup(name='ansible',
'ansible'
,
'ansible'
,
],
],
data_files
=
[
data_files
=
[
(
'/usr/share/ancible'
,
'library/ping'
),
(
'/usr/share/ansible'
,
[
(
'/usr/share/ancible'
,
'library/command'
),
'library/ping'
,
(
'/usr/share/ancible'
,
'library/facter'
),
'library/command'
,
(
'/usr/share/ancible'
,
'library/copy'
),
'library/facter'
,
'library/copy'
,
])
],
],
scripts
=
[
scripts
=
[
'bin/ansible'
,
'bin/ansible'
,
...
...
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