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
2c5d5a32
Commit
2c5d5a32
authored
Feb 25, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9 from skvidal/master
use logger to track all events run via ansible on the remote host.
parents
e61d6430
160470bc
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
5 deletions
+21
-5
README.md
+3
-1
bin/ansible
+2
-1
lib/ansible/runner.py
+16
-3
No files found.
README.md
View file @
2c5d5a32
...
@@ -83,7 +83,8 @@ track of which hosts were successfully contacted seperately from hosts
...
@@ -83,7 +83,8 @@ track of which hosts were successfully contacted seperately from hosts
that had communication problems. The format of the return, if successful,
that had communication problems. The format of the return, if successful,
is entirely up to the module.
is entirely up to the module.
import ansible
import ansible.runner
runner = ansible.runner.Runner(
runner = ansible.runner.Runner(
pattern='*',
pattern='*',
module_name='inventory',
module_name='inventory',
...
@@ -91,6 +92,7 @@ is entirely up to the module.
...
@@ -91,6 +92,7 @@ is entirely up to the module.
)
)
data = runner.run()
data = runner.run()
data is a dictionary:
{
{
'contacted' : {
'contacted' : {
'xyz.example.com' :
[
'any kind of datastructure is returnable'
]
,
'xyz.example.com' :
[
'any kind of datastructure is returnable'
]
,
...
...
bin/ansible
View file @
2c5d5a32
...
@@ -25,6 +25,7 @@ from optparse import OptionParser
...
@@ -25,6 +25,7 @@ from optparse import OptionParser
import
json
import
json
import
os
import
os
import
getpass
import
getpass
import
shlex
import
ansible.runner
import
ansible.runner
import
ansible.playbook
import
ansible.playbook
import
ansible.constants
as
C
import
ansible.constants
as
C
...
@@ -68,7 +69,7 @@ class Cli(object):
...
@@ -68,7 +69,7 @@ class Cli(object):
return
ansible
.
runner
.
Runner
(
return
ansible
.
runner
.
Runner
(
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
=
shlex
.
split
(
options
.
module_args
),
remote_user
=
options
.
remote_user
,
remote_user
=
options
.
remote_user
,
remote_pass
=
sshpass
,
remote_pass
=
sshpass
,
host_list
=
options
.
host_list
,
host_list
=
options
.
host_list
,
...
...
lib/ansible/runner.py
View file @
2c5d5a32
...
@@ -73,11 +73,13 @@ class Runner(object):
...
@@ -73,11 +73,13 @@ class Runner(object):
return
host_list
return
host_list
def
_matches
(
self
,
host_name
):
def
_matches
(
self
,
host_name
,
pattern
=
None
):
''' returns if a hostname is matched by the pattern '''
''' 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
not
pattern
:
pattern
=
self
.
pattern
if
fnmatch
.
fnmatch
(
host_name
,
pattern
):
return
True
return
True
return
False
return
False
...
@@ -120,6 +122,7 @@ class Runner(object):
...
@@ -120,6 +122,7 @@ class Runner(object):
return
[
host
,
True
,
json
.
loads
(
result
)
]
return
[
host
,
True
,
json
.
loads
(
result
)
]
else
:
else
:
# SFTP file copy module is not really a module
# SFTP file copy module is not really a module
self
.
remote_log
(
conn
,
'COPY remote:
%
s local:
%
s'
%
(
self
.
module_args
[
0
],
self
.
module_args
[
1
]))
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
()
...
@@ -132,8 +135,14 @@ class Runner(object):
...
@@ -132,8 +135,14 @@ class Runner(object):
cmd
=
"
%
s
%
s"
%
(
outpath
,
" "
.
join
(
self
.
module_args
))
cmd
=
"
%
s
%
s"
%
(
outpath
,
" "
.
join
(
self
.
module_args
))
return
cmd
return
cmd
def
remote_log
(
self
,
conn
,
msg
):
stdin
,
stdout
,
stderr
=
conn
.
exec_command
(
'/usr/bin/logger -t ansible -p auth.info
%
r'
%
msg
)
def
_exec_command
(
self
,
conn
,
cmd
):
def
_exec_command
(
self
,
conn
,
cmd
):
''' execute a command over SSH '''
''' execute a command over SSH '''
msg
=
'
%
s:
%
s'
%
(
self
.
module_name
,
cmd
)
self
.
remote_log
(
conn
,
msg
)
stdin
,
stdout
,
stderr
=
conn
.
exec_command
(
cmd
)
stdin
,
stdout
,
stderr
=
conn
.
exec_command
(
cmd
)
results
=
"
\n
"
.
join
(
stdout
.
readlines
())
results
=
"
\n
"
.
join
(
stdout
.
readlines
())
return
results
return
results
...
@@ -154,11 +163,15 @@ class Runner(object):
...
@@ -154,11 +163,15 @@ class Runner(object):
sftp
.
close
()
sftp
.
close
()
return
out_path
return
out_path
def
match_hosts
(
self
,
pattern
=
None
):
''' return all matched hosts '''
return
[
h
for
h
in
self
.
host_list
if
self
.
_matches
(
h
,
pattern
)
]
def
run
(
self
):
def
run
(
self
):
''' xfer & run module on all matched hosts '''
''' xfer & run module on all matched hosts '''
# find hosts that match the pattern
# find hosts that match the pattern
hosts
=
[
h
for
h
in
self
.
host_list
if
self
.
_matches
(
h
)
]
hosts
=
self
.
match_hosts
()
# attack pool of hosts in N forks
# attack pool of hosts in N forks
pool
=
multiprocessing
.
Pool
(
self
.
forks
)
pool
=
multiprocessing
.
Pool
(
self
.
forks
)
...
...
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