2015-08-11 23:18:10 +00:00
|
|
|
# (c) 2012-2014, Ansible, Inc
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from ansible.plugins.callback import CallbackBase
|
|
|
|
from ansible.utils.path import makedirs_safe
|
2015-09-23 22:23:29 +00:00
|
|
|
from ansible.utils.unicode import to_bytes
|
2015-08-11 23:18:10 +00:00
|
|
|
from ansible.constants import TREE_DIR
|
|
|
|
|
|
|
|
|
|
|
|
class CallbackModule(CallbackBase):
|
|
|
|
'''
|
|
|
|
This callback puts results into a host specific file in a directory in json format.
|
|
|
|
'''
|
|
|
|
|
|
|
|
CALLBACK_VERSION = 2.0
|
|
|
|
CALLBACK_TYPE = 'aggregate'
|
|
|
|
CALLBACK_NAME = 'tree'
|
2015-10-22 12:27:32 +00:00
|
|
|
CALLBACK_NEEDS_WHITELIST = True
|
2015-08-11 23:18:10 +00:00
|
|
|
|
2015-12-01 21:28:16 +00:00
|
|
|
def __init__(self):
|
|
|
|
super(CallbackModule, self).__init__()
|
2015-08-12 00:50:47 +00:00
|
|
|
|
|
|
|
self.tree = TREE_DIR
|
|
|
|
if not self.tree:
|
2015-12-08 19:59:04 +00:00
|
|
|
self.tree = os.path.expanduser("~/.ansible/tree")
|
2015-12-09 14:32:04 +00:00
|
|
|
self._display.warning("The tree callback is defaulting to ~/.ansible/tree, as an invalid directory was provided: %s" % self.tree)
|
2015-08-12 00:50:47 +00:00
|
|
|
|
|
|
|
def write_tree_file(self, hostname, buf):
|
2015-08-11 23:18:10 +00:00
|
|
|
''' write something into treedir/hostname '''
|
|
|
|
|
2015-09-23 22:23:29 +00:00
|
|
|
buf = to_bytes(buf)
|
2015-08-11 23:18:10 +00:00
|
|
|
try:
|
2015-08-12 00:50:47 +00:00
|
|
|
makedirs_safe(self.tree)
|
|
|
|
path = os.path.join(self.tree, hostname)
|
2015-09-23 22:23:29 +00:00
|
|
|
with open(path, 'wb+') as fd:
|
|
|
|
fd.write(buf)
|
2015-08-11 23:18:10 +00:00
|
|
|
except (OSError, IOError) as e:
|
2015-12-08 19:59:04 +00:00
|
|
|
self._display.warning("Unable to write to %s's file: %s" % (hostname, str(e)))
|
2015-08-11 23:18:10 +00:00
|
|
|
|
|
|
|
def result_to_tree(self, result):
|
2015-08-12 00:50:47 +00:00
|
|
|
if self.tree:
|
|
|
|
self.write_tree_file(result._host.get_name(), self._dump_results(result._result))
|
2015-08-11 23:18:10 +00:00
|
|
|
|
|
|
|
def v2_runner_on_ok(self, result):
|
|
|
|
self.result_to_tree(result)
|
|
|
|
|
2016-04-29 17:50:52 +00:00
|
|
|
def v2_runner_on_failed(self, result, ignore_errors=False):
|
2015-08-11 23:18:10 +00:00
|
|
|
self.result_to_tree(result)
|
|
|
|
|
|
|
|
def v2_runner_on_unreachable(self, result):
|
|
|
|
self.result_to_tree(result)
|
|
|
|
|