2014-10-02 19:07:05 +02:00
|
|
|
# (c) 2012-2014, 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/>.
|
|
|
|
|
2014-10-16 01:22:54 +02:00
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2014-10-09 19:52:09 +02:00
|
|
|
import os
|
|
|
|
|
2014-11-14 23:14:08 +01:00
|
|
|
from ansible.errors.yaml_strings import *
|
2014-10-23 22:39:27 +02:00
|
|
|
|
2014-10-02 19:07:05 +02:00
|
|
|
class AnsibleError(Exception):
|
2014-10-23 22:39:27 +02:00
|
|
|
'''
|
|
|
|
This is the base class for all errors raised from Ansible code,
|
|
|
|
and can be instantiated with two optional parameters beyond the
|
|
|
|
error message to control whether detailed information is displayed
|
|
|
|
when the error occurred while parsing a data file of some kind.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
raise AnsibleError('some message here', obj=obj, show_content=True)
|
|
|
|
|
|
|
|
Where "obj" is some subclass of ansible.parsing.yaml.objects.AnsibleBaseYAMLObject,
|
|
|
|
which should be returned by the DataLoader() class.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, message, obj=None, show_content=True):
|
|
|
|
# we import this here to prevent an import loop problem,
|
|
|
|
# since the objects code also imports ansible.errors
|
2014-10-21 20:27:01 +02:00
|
|
|
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject
|
2014-10-23 22:39:27 +02:00
|
|
|
|
|
|
|
self._obj = obj
|
|
|
|
self._show_content = show_content
|
2014-11-14 23:14:08 +01:00
|
|
|
if obj and isinstance(obj, AnsibleBaseYAMLObject):
|
2014-10-09 19:52:09 +02:00
|
|
|
extended_error = self._get_extended_error()
|
|
|
|
if extended_error:
|
2014-11-14 23:14:08 +01:00
|
|
|
self.message = 'ERROR! %s\n\n%s' % (message, extended_error)
|
2014-10-09 19:52:09 +02:00
|
|
|
else:
|
2014-11-14 23:14:08 +01:00
|
|
|
self.message = 'ERROR! %s' % message
|
2014-10-09 19:52:09 +02:00
|
|
|
|
2014-11-05 15:00:00 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.message
|
|
|
|
|
2014-10-09 19:52:09 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return self.message
|
|
|
|
|
2014-10-23 22:39:27 +02:00
|
|
|
def _get_error_lines_from_file(self, file_name, line_number):
|
|
|
|
'''
|
|
|
|
Returns the line in the file which coresponds to the reported error
|
2014-12-04 23:23:35 +01:00
|
|
|
location, as well as the line preceding it (if the error did not
|
2014-10-23 22:39:27 +02:00
|
|
|
occur on the first line), to provide context to the error.
|
|
|
|
'''
|
|
|
|
|
|
|
|
target_line = ''
|
|
|
|
prev_line = ''
|
|
|
|
|
|
|
|
with open(file_name, 'r') as f:
|
2014-10-09 19:52:09 +02:00
|
|
|
lines = f.readlines()
|
2014-10-23 22:39:27 +02:00
|
|
|
|
|
|
|
target_line = lines[line_number]
|
|
|
|
if line_number > 0:
|
|
|
|
prev_line = lines[line_number - 1]
|
|
|
|
|
|
|
|
return (target_line, prev_line)
|
2014-10-09 19:52:09 +02:00
|
|
|
|
|
|
|
def _get_extended_error(self):
|
2014-10-23 22:39:27 +02:00
|
|
|
'''
|
|
|
|
Given an object reporting the location of the exception in a file, return
|
|
|
|
detailed information regarding it including:
|
|
|
|
|
2014-12-04 23:23:35 +01:00
|
|
|
* the line which caused the error as well as the one preceding it
|
2014-10-23 22:39:27 +02:00
|
|
|
* causes and suggested remedies for common syntax errors
|
|
|
|
|
|
|
|
If this error was created with show_content=False, the reporting of content
|
|
|
|
is suppressed, as the file contents may be sensitive (ie. vault data).
|
|
|
|
'''
|
|
|
|
|
2014-10-09 19:52:09 +02:00
|
|
|
error_message = ''
|
2014-10-08 21:59:24 +02:00
|
|
|
|
2014-10-09 19:52:09 +02:00
|
|
|
try:
|
2015-04-01 23:54:22 +02:00
|
|
|
(src_file, line_number, col_number) = self._obj.ansible_pos
|
2014-10-23 22:39:27 +02:00
|
|
|
error_message += YAML_POSITION_DETAILS % (src_file, line_number, col_number)
|
|
|
|
if src_file not in ('<string>', '<unicode>') and self._show_content:
|
|
|
|
(target_line, prev_line) = self._get_error_lines_from_file(src_file, line_number - 1)
|
|
|
|
if target_line:
|
|
|
|
stripped_line = target_line.replace(" ","")
|
2014-11-14 23:14:08 +01:00
|
|
|
arrow_line = (" " * (col_number-1)) + "^ here"
|
|
|
|
#header_line = ("=" * 73)
|
|
|
|
error_message += "\nThe offending line appears to be:\n\n%s\n%s\n%s\n" % (prev_line.rstrip(), target_line.rstrip(), arrow_line)
|
2014-10-23 22:39:27 +02:00
|
|
|
|
|
|
|
# common error/remediation checking here:
|
|
|
|
# check for unquoted vars starting lines
|
|
|
|
if ('{{' in target_line and '}}' in target_line) and ('"{{' not in target_line or "'{{" not in target_line):
|
|
|
|
error_message += YAML_COMMON_UNQUOTED_VARIABLE_ERROR
|
|
|
|
# check for common dictionary mistakes
|
|
|
|
elif ":{{" in stripped_line and "}}" in stripped_line:
|
|
|
|
error_message += YAML_COMMON_DICT_ERROR
|
|
|
|
# check for common unquoted colon mistakes
|
|
|
|
elif len(target_line) and len(target_line) > 1 and len(target_line) > col_number and target_line[col_number] == ":" and target_line.count(':') > 1:
|
|
|
|
error_message += YAML_COMMON_UNQUOTED_COLON_ERROR
|
|
|
|
# otherwise, check for some common quoting mistakes
|
|
|
|
else:
|
|
|
|
parts = target_line.split(":")
|
|
|
|
if len(parts) > 1:
|
|
|
|
middle = parts[1].strip()
|
|
|
|
match = False
|
|
|
|
unbalanced = False
|
|
|
|
|
|
|
|
if middle.startswith("'") and not middle.endswith("'"):
|
|
|
|
match = True
|
|
|
|
elif middle.startswith('"') and not middle.endswith('"'):
|
|
|
|
match = True
|
|
|
|
|
|
|
|
if len(middle) > 0 and middle[0] in [ '"', "'" ] and middle[-1] in [ '"', "'" ] and target_line.count("'") > 2 or target_line.count('"') > 2:
|
|
|
|
unbalanced = True
|
|
|
|
|
|
|
|
if match:
|
|
|
|
error_message += YAML_COMMON_PARTIALLY_QUOTED_LINE_ERROR
|
|
|
|
if unbalanced:
|
|
|
|
error_message += YAML_COMMON_UNBALANCED_QUOTES_ERROR
|
|
|
|
|
2014-11-05 15:00:00 +01:00
|
|
|
except (IOError, TypeError):
|
2014-10-09 19:52:09 +02:00
|
|
|
error_message += '\n(could not open file to display line)'
|
|
|
|
except IndexError:
|
|
|
|
error_message += '\n(specified line no longer in file, maybe it changed?)'
|
2014-10-08 21:59:24 +02:00
|
|
|
|
2014-10-09 19:52:09 +02:00
|
|
|
return error_message
|
2014-10-08 21:59:24 +02:00
|
|
|
|
|
|
|
class AnsibleParserError(AnsibleError):
|
|
|
|
''' something was detected early that is wrong about a playbook or data file '''
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AnsibleInternalError(AnsibleError):
|
|
|
|
''' internal safeguards tripped, something happened in the code that should never happen '''
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AnsibleRuntimeError(AnsibleError):
|
|
|
|
''' ansible had a problem while running a playbook '''
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AnsibleModuleError(AnsibleRuntimeError):
|
|
|
|
''' a module failed somehow '''
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AnsibleConnectionFailure(AnsibleRuntimeError):
|
|
|
|
''' the transport / connection_plugin had a fatal error '''
|
|
|
|
pass
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
class AnsibleFilterError(AnsibleRuntimeError):
|
|
|
|
''' a templating failure '''
|
|
|
|
pass
|
|
|
|
|
|
|
|
class AnsibleUndefinedVariable(AnsibleRuntimeError):
|
|
|
|
''' a templating failure '''
|
|
|
|
pass
|
2015-04-14 21:23:59 +02:00
|
|
|
|
|
|
|
class AnsibleFileNotFound(AnsibleRuntimeError):
|
|
|
|
''' a file missing failure '''
|
|
|
|
pass
|
2015-04-14 21:50:31 +02:00
|
|
|
|
|
|
|
class AnsibleParserError(AnsibleRuntimeError):
|
|
|
|
''' a parser error '''
|
|
|
|
pass
|