From 8ce864db6f288c7e65f34d374b5e29838c229b3e Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Sat, 24 Oct 2015 15:23:12 -0400 Subject: [PATCH] sanatize results --- lib/ansible/executor/process/result.py | 3 ++- lib/ansible/plugins/callback/__init__.py | 6 ++---- lib/ansible/vars/__init__.py | 11 +++++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/ansible/executor/process/result.py b/lib/ansible/executor/process/result.py index 9e1f6921f4..dc4dabab2a 100644 --- a/lib/ansible/executor/process/result.py +++ b/lib/ansible/executor/process/result.py @@ -21,6 +21,7 @@ __metaclass__ = type from ansible.compat.six.moves import queue from ansible.compat.six import iteritems, text_type +from ansible.vars import strip_internal_keys import multiprocessing import time @@ -105,7 +106,7 @@ class ResultProcess(multiprocessing.Process): # if this task is registering a result, do it now if result._task.register: - self._send_result(('register_host_var', result._host, result._task.register, result._result)) + self._send_result(('register_host_var', result._host, result._task.register, strip_internal_keys(result._result))) # send callbacks, execute other options based on the result status # TODO: this should all be cleaned up and probably moved to a sub-function. diff --git a/lib/ansible/plugins/callback/__init__.py b/lib/ansible/plugins/callback/__init__.py index 612266a9f7..aaea1a525c 100644 --- a/lib/ansible/plugins/callback/__init__.py +++ b/lib/ansible/plugins/callback/__init__.py @@ -27,6 +27,7 @@ from copy import deepcopy from ansible.compat.six import string_types from ansible import constants as C +from ansible.vars import strip_internal_keys from ansible.utils.unicode import to_unicode __all__ = ["CallbackBase"] @@ -56,10 +57,7 @@ class CallbackBase: indent = 4 # All result keys stating with _ansible_ are internal, so remove them from the result before we output anything. - abridged_result = result.copy() - for k in abridged_result.keys(): - if isinstance(k, string_types) and k.startswith('_ansible_'): - del abridged_result[k] + abridged_result = strip_internal_keys(result) # Remove invocation unless verbosity is turned up or the specific # callback wants to keep it diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index b84747a49a..557b98c82e 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -69,6 +69,17 @@ def preprocess_vars(a): return data +def strip_internal_keys(dirty): + ''' + All keys stating with _ansible_ are internal, so create a copy of the 'dirty' dict + and remove them from the clean one before returning it + ''' + clean = dirty.copy() + for k in dirty.keys(): + if isinstance(k, string_types) and k.startswith('_ansible_'): + del clean[k] + return clean + class VariableManager: def __init__(self):