2015-07-09 02:37:17 +02:00
|
|
|
# (C) 2012, Michael DeHaan, <michael.dehaan@gmail.com>
|
|
|
|
|
|
|
|
# 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/>.
|
|
|
|
|
2015-10-20 03:36:19 +02:00
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2015-07-09 02:37:17 +02:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import json
|
|
|
|
|
2015-09-24 00:23:29 +02:00
|
|
|
from ansible.utils.unicode import to_bytes
|
2015-07-09 02:37:17 +02:00
|
|
|
from ansible.plugins.callback import CallbackBase
|
|
|
|
|
|
|
|
# NOTE: in Ansible 1.2 or later general logging is available without
|
|
|
|
# this plugin, just set ANSIBLE_LOG_PATH as an environment variable
|
|
|
|
# or log_path in the DEFAULTS section of your ansible configuration
|
|
|
|
# file. This callback is an example of per hosts logging for those
|
|
|
|
# that want it.
|
|
|
|
|
|
|
|
|
|
|
|
class CallbackModule(CallbackBase):
|
|
|
|
"""
|
|
|
|
logs playbook results, per host, in /var/log/ansible/hosts
|
|
|
|
"""
|
|
|
|
CALLBACK_VERSION = 2.0
|
|
|
|
CALLBACK_TYPE = 'notification'
|
2015-07-11 05:48:12 +02:00
|
|
|
CALLBACK_NAME = 'log_plays'
|
2015-10-22 14:27:32 +02:00
|
|
|
CALLBACK_NEEDS_WHITELIST = True
|
2015-07-09 02:37:17 +02:00
|
|
|
|
|
|
|
TIME_FORMAT="%b %d %Y %H:%M:%S"
|
|
|
|
MSG_FORMAT="%(now)s - %(category)s - %(data)s\n\n"
|
|
|
|
|
2015-12-01 22:28:16 +01:00
|
|
|
def __init__(self):
|
2015-07-09 02:37:17 +02:00
|
|
|
|
2015-12-01 22:28:16 +01:00
|
|
|
super(CallbackModule, self).__init__()
|
2015-07-09 02:37:17 +02:00
|
|
|
|
|
|
|
if not os.path.exists("/var/log/ansible/hosts"):
|
|
|
|
os.makedirs("/var/log/ansible/hosts")
|
|
|
|
|
|
|
|
def log(self, host, category, data):
|
|
|
|
if type(data) == dict:
|
2015-07-28 04:15:44 +02:00
|
|
|
if '_ansible_verbose_override' in data:
|
|
|
|
# avoid logging extraneous data
|
2015-07-09 02:37:17 +02:00
|
|
|
data = 'omitted'
|
|
|
|
else:
|
|
|
|
data = data.copy()
|
2015-10-22 17:25:20 +02:00
|
|
|
invocation = data.pop('invocation', None)
|
2015-07-09 02:37:17 +02:00
|
|
|
data = json.dumps(data)
|
2015-10-22 17:25:20 +02:00
|
|
|
if invocation is not None:
|
|
|
|
data = json.dumps(invocation) + " => %s " % data
|
2015-07-09 02:37:17 +02:00
|
|
|
|
|
|
|
path = os.path.join("/var/log/ansible/hosts", host)
|
|
|
|
now = time.strftime(self.TIME_FORMAT, time.localtime())
|
2015-09-24 00:23:29 +02:00
|
|
|
|
|
|
|
msg = to_bytes(self.MSG_FORMAT % dict(now=now, category=category, data=data))
|
|
|
|
with open(path, "ab") as fd:
|
|
|
|
fd.write(msg)
|
2015-07-09 02:37:17 +02:00
|
|
|
|
|
|
|
def runner_on_failed(self, host, res, ignore_errors=False):
|
|
|
|
self.log(host, 'FAILED', res)
|
|
|
|
|
|
|
|
def runner_on_ok(self, host, res):
|
|
|
|
self.log(host, 'OK', res)
|
|
|
|
|
|
|
|
def runner_on_skipped(self, host, item=None):
|
|
|
|
self.log(host, 'SKIPPED', '...')
|
|
|
|
|
|
|
|
def runner_on_unreachable(self, host, res):
|
|
|
|
self.log(host, 'UNREACHABLE', res)
|
|
|
|
|
|
|
|
def runner_on_async_failed(self, host, res, jid):
|
|
|
|
self.log(host, 'ASYNC_FAILED', res)
|
|
|
|
|
|
|
|
def playbook_on_import_for_host(self, host, imported_file):
|
|
|
|
self.log(host, 'IMPORTED', imported_file)
|
|
|
|
|
|
|
|
def playbook_on_not_import_for_host(self, host, missing_file):
|
|
|
|
self.log(host, 'NOTIMPORTED', missing_file)
|