Merge pull request #9 from skvidal/master
use logger to track all events run via ansible on the remote host.
This commit is contained in:
commit
2c5d5a328b
3 changed files with 21 additions and 5 deletions
|
@ -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.
|
||||||
)
|
)
|
||||||
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' ],
|
||||||
|
|
|
@ -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):
|
||||||
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,
|
||||||
|
|
|
@ -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):
|
||||||
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):
|
||||||
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):
|
||||||
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)
|
||||||
|
|
Loading…
Reference in a new issue