Ansible json error callback plugin

This callback plugin will write any task errors (that are not
ignore_error: true) to a log file in JSON format.

In later patches, we can add the cli and workflows to make these errors
visible via the Mistral API.

Change-Id: I72a91473511f335f8a0e40c7a786ff927113f26a
(cherry picked from commit 6e36336365)
This commit is contained in:
James Slagle 2018-05-08 12:14:02 -04:00 committed by lshort
parent fe1d9ef4c9
commit 22e6135b5b
3 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,62 @@
# Copyright 2018 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# Ansible has another callback plugin just called "json.py", which overrides
# a normal import of "import json", so use absolute imports
from __future__ import absolute_import
import json
import os
from ansible.plugins.callback import CallbackBase
from tripleo_common import constants
DOCUMENTATION = '''
callback: json_error
short_description: Write errors in JSON format to a log file
description:
- This callback writes errors in JSON format to a log file
type: aggregate
options:
output_dir:
name: json-error log file
default: ansible-error.json
description: Log file where to write errors in JSON format.
env:
- name: JSON_ERROR_LOG_FILE
'''
class CallbackModule(CallbackBase):
CALLBACK_VERSION = 2.5
CALLBACK_TYPE = 'aggregate'
CALLBACK_NAME = 'json-error'
def __init__(self, display=None):
super(CallbackModule, self).__init__(display)
self.errors = {}
self.log_file = os.getenv(
'JSON_ERROR_LOG_FILE',
constants.ANSIBLE_ERRORS_FILE)
def v2_playbook_on_stats(self, stats):
with open(self.log_file, 'w') as f:
f.write(json.dumps(self.errors))
def v2_runner_on_failed(self, result, ignore_errors=False):
if not ignore_errors:
host_errors = self.errors.setdefault(result._host.name, [])
host_errors.append((result.task_name, result._result))

View File

@ -44,6 +44,7 @@ data_files =
share/tripleo-common/workbooks = workbooks/*
share/tripleo-common/healthcheck = healthcheck/*
share/ansible/roles/ = roles/*
share/ansible/plugins/ = ansible_plugins/*
[build_sphinx]
source-dir = doc/source

View File

@ -167,3 +167,5 @@ TRIPLEO_DEPLOYMENT_RESOURCE = 'TripleODeployment'
HOST_NETWORK = 'ctlplane'
EXTERNAL_TASKS = ['external_deploy_tasks']
ANSIBLE_ERRORS_FILE = 'ansible-errors.json'