|
|
@ -15,38 +15,52 @@
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################
|
|
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
import simplejson as json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################
|
|
|
|
|
|
|
|
# UTILITY FUNCTIONS FOR COMMAND LINE TOOLS
|
|
|
|
|
|
|
|
###############################################################
|
|
|
|
|
|
|
|
|
|
|
|
def err(msg):
|
|
|
|
def err(msg):
|
|
|
|
|
|
|
|
''' print an error message to stderr '''
|
|
|
|
print >> sys.stderr, msg
|
|
|
|
print >> sys.stderr, msg
|
|
|
|
|
|
|
|
|
|
|
|
def exit(msg, rc=1):
|
|
|
|
def exit(msg, rc=1):
|
|
|
|
|
|
|
|
''' quit with an error to stdout and a failure code '''
|
|
|
|
err(msg)
|
|
|
|
err(msg)
|
|
|
|
sys.exit(rc)
|
|
|
|
sys.exit(rc)
|
|
|
|
|
|
|
|
|
|
|
|
def _bigjson(result):
|
|
|
|
def _bigjson(result):
|
|
|
|
|
|
|
|
''' format JSON output (uncompressed) '''
|
|
|
|
return json.dumps(result, sort_keys=True, indent=4)
|
|
|
|
return json.dumps(result, sort_keys=True, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
def _json(result):
|
|
|
|
def _json(result):
|
|
|
|
|
|
|
|
''' format JSON output (compressed) '''
|
|
|
|
return json.dumps(result, sort_keys=True)
|
|
|
|
return json.dumps(result, sort_keys=True)
|
|
|
|
|
|
|
|
|
|
|
|
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 '''
|
|
|
|
if not oneline:
|
|
|
|
if not oneline:
|
|
|
|
return "%s | %s >>\n%s" % (hostname, caption, _bigjson(result))
|
|
|
|
return "%s | %s >>\n%s" % (hostname, caption, _bigjson(result))
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return "%s | %s >> %s" % (hostname, caption, _json(result))
|
|
|
|
return "%s | %s >> %s" % (hostname, caption, _json(result))
|
|
|
|
|
|
|
|
|
|
|
|
def regular_success_msg(hostname, result, oneline):
|
|
|
|
def regular_success_msg(hostname, result, oneline):
|
|
|
|
|
|
|
|
''' output the result of a successful module run '''
|
|
|
|
return regular_generic_msg(hostname, result, oneline, 'success')
|
|
|
|
return regular_generic_msg(hostname, result, oneline, 'success')
|
|
|
|
|
|
|
|
|
|
|
|
def regular_failure_msg(hostname, result, oneline):
|
|
|
|
def regular_failure_msg(hostname, result, oneline):
|
|
|
|
|
|
|
|
''' output the result of a failed module run '''
|
|
|
|
return regular_generic_msg(hostname, result, oneline, 'FAILED')
|
|
|
|
return regular_generic_msg(hostname, result, oneline, 'FAILED')
|
|
|
|
|
|
|
|
|
|
|
|
def command_generic_msg(hostname, result, oneline, caption):
|
|
|
|
def command_generic_msg(hostname, result, oneline, caption):
|
|
|
|
|
|
|
|
''' output the result of a command run '''
|
|
|
|
rc = result.get('rc', '0')
|
|
|
|
rc = result.get('rc', '0')
|
|
|
|
stdout = result.get('stdout','')
|
|
|
|
stdout = result.get('stdout','')
|
|
|
|
stderr = result.get('stderr', '')
|
|
|
|
stderr = result.get('stderr', '')
|
|
|
@ -67,18 +81,24 @@ def command_generic_msg(hostname, result, oneline, caption):
|
|
|
|
return "%s | %s | rc=%s | (stdout) %s" % (hostname, caption, rc, stdout)
|
|
|
|
return "%s | %s | rc=%s | (stdout) %s" % (hostname, caption, rc, stdout)
|
|
|
|
|
|
|
|
|
|
|
|
def command_success_msg(hostname, result, oneline):
|
|
|
|
def command_success_msg(hostname, result, oneline):
|
|
|
|
|
|
|
|
''' output from a successful command run '''
|
|
|
|
return command_generic_msg(hostname, result, oneline, 'success')
|
|
|
|
return command_generic_msg(hostname, result, oneline, 'success')
|
|
|
|
|
|
|
|
|
|
|
|
def command_failure_msg(hostname, result, oneline):
|
|
|
|
def command_failure_msg(hostname, result, oneline):
|
|
|
|
|
|
|
|
''' output from a failed command run '''
|
|
|
|
return command_generic_msg(hostname, result, oneline, 'FAILED')
|
|
|
|
return command_generic_msg(hostname, result, oneline, 'FAILED')
|
|
|
|
|
|
|
|
|
|
|
|
def write_tree_file(hostname,buf):
|
|
|
|
def write_tree_file(hostname,buf):
|
|
|
|
|
|
|
|
''' write something into treedir/hostname '''
|
|
|
|
|
|
|
|
# TODO: might be nice to append playbook runs per host in a similar way
|
|
|
|
|
|
|
|
# in which case, we'd want append mode.
|
|
|
|
path = os.path.join(options.tree, hostname)
|
|
|
|
path = os.path.join(options.tree, hostname)
|
|
|
|
fd = open(path, "w+")
|
|
|
|
fd = open(path, "w+")
|
|
|
|
fd.write(buf)
|
|
|
|
fd.write(buf)
|
|
|
|
fd.close()
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
|
|
|
|
def is_failed(result):
|
|
|
|
def is_failed(result):
|
|
|
|
|
|
|
|
''' is a given JSON result a failed result? '''
|
|
|
|
failed = False
|
|
|
|
failed = False
|
|
|
|
rc = 0
|
|
|
|
rc = 0
|
|
|
|
if type(result) == dict:
|
|
|
|
if type(result) == dict:
|
|
|
@ -89,6 +109,7 @@ def is_failed(result):
|
|
|
|
return failed
|
|
|
|
return failed
|
|
|
|
|
|
|
|
|
|
|
|
def host_report_msg(hostname, module_name, result, oneline):
|
|
|
|
def host_report_msg(hostname, module_name, result, oneline):
|
|
|
|
|
|
|
|
''' summarize the JSON results for a particular host '''
|
|
|
|
buf = ''
|
|
|
|
buf = ''
|
|
|
|
failed = is_failed(result)
|
|
|
|
failed = is_failed(result)
|
|
|
|
if module_name == 'command':
|
|
|
|
if module_name == 'command':
|
|
|
@ -104,6 +125,7 @@ def host_report_msg(hostname, module_name, result, oneline):
|
|
|
|
return buf
|
|
|
|
return buf
|
|
|
|
|
|
|
|
|
|
|
|
def dark_hosts_msg(results):
|
|
|
|
def dark_hosts_msg(results):
|
|
|
|
|
|
|
|
''' 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 += "*** Hosts which could not be contacted or did not respond: ***"
|
|
|
@ -113,15 +135,19 @@ def dark_hosts_msg(results):
|
|
|
|
return buf
|
|
|
|
return buf
|
|
|
|
|
|
|
|
|
|
|
|
def has_dark_hosts(results):
|
|
|
|
def has_dark_hosts(results):
|
|
|
|
|
|
|
|
''' are there any uncontactable hosts? '''
|
|
|
|
return len(results['dark'].keys()) > 0
|
|
|
|
return len(results['dark'].keys()) > 0
|
|
|
|
|
|
|
|
|
|
|
|
def contacted_hosts(results):
|
|
|
|
def contacted_hosts(results):
|
|
|
|
|
|
|
|
''' what are the contactable hosts? '''
|
|
|
|
return sorted(results['contacted'])
|
|
|
|
return sorted(results['contacted'])
|
|
|
|
|
|
|
|
|
|
|
|
def contacted_host_result(results, hostname):
|
|
|
|
def contacted_host_result(results, hostname):
|
|
|
|
|
|
|
|
''' what are the results for a given host? '''
|
|
|
|
return results['contacted'][hostname]
|
|
|
|
return results['contacted'][hostname]
|
|
|
|
|
|
|
|
|
|
|
|
def prepare_writeable_dir(tree):
|
|
|
|
def prepare_writeable_dir(tree):
|
|
|
|
|
|
|
|
''' make sure a directory exists and is writeable '''
|
|
|
|
if tree != '/':
|
|
|
|
if tree != '/':
|
|
|
|
tree = os.path.realpath(os.path.expanduser(options.tree))
|
|
|
|
tree = os.path.realpath(os.path.expanduser(options.tree))
|
|
|
|
if not os.path.exists(tree):
|
|
|
|
if not os.path.exists(tree):
|
|
|
|