1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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:
Michael DeHaan 2012-02-25 06:58:32 -08:00
commit 2c5d5a328b
3 changed files with 21 additions and 5 deletions

View file

@ -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,
is entirely up to the module.
import ansible
import ansible.runner
runner = ansible.runner.Runner(
pattern='*',
module_name='inventory',
@ -91,6 +92,7 @@ is entirely up to the module.
)
data = runner.run()
data is a dictionary:
{
'contacted' : {
'xyz.example.com' : [ 'any kind of datastructure is returnable' ],

View file

@ -25,6 +25,7 @@ from optparse import OptionParser
import json
import os
import getpass
import shlex
import ansible.runner
import ansible.playbook
import ansible.constants as C
@ -68,7 +69,7 @@ class Cli(object):
return ansible.runner.Runner(
module_name=options.module_name,
module_path=options.module_path,
module_args=options.module_args.split(' '),
module_args=shlex.split(options.module_args),
remote_user=options.remote_user,
remote_pass=sshpass,
host_list=options.host_list,

View file

@ -73,11 +73,13 @@ class Runner(object):
return host_list
def _matches(self, host_name):
def _matches(self, host_name, pattern=None):
''' returns if a hostname is matched by the pattern '''
if host_name == '':
return False
if fnmatch.fnmatch(host_name, self.pattern):
if not pattern:
pattern = self.pattern
if fnmatch.fnmatch(host_name, pattern):
return True
return False
@ -120,6 +122,7 @@ class Runner(object):
return [ host, True, json.loads(result) ]
else:
# 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.put(self.module_args[0], self.module_args[1])
ftp.close()
@ -132,8 +135,14 @@ class Runner(object):
cmd = "%s %s" % (outpath, " ".join(self.module_args))
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):
''' execute a command over SSH '''
msg = '%s: %s' % (self.module_name, cmd)
self.remote_log(conn, msg)
stdin, stdout, stderr = conn.exec_command(cmd)
results = "\n".join(stdout.readlines())
return results
@ -154,11 +163,15 @@ class Runner(object):
sftp.close()
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):
''' xfer & run module on all matched hosts '''
# 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
pool = multiprocessing.Pool(self.forks)