From e5d45311f9dc816b7d237c1c0777cbb0b07cbbe8 Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Thu, 10 Oct 2013 21:34:37 +0200 Subject: [PATCH 1/2] Implement a |changed filter plugin --- lib/ansible/runner/filter_plugins/core.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/ansible/runner/filter_plugins/core.py b/lib/ansible/runner/filter_plugins/core.py index 2729938dae..15d13e6c69 100644 --- a/lib/ansible/runner/filter_plugins/core.py +++ b/lib/ansible/runner/filter_plugins/core.py @@ -34,7 +34,8 @@ def to_nice_json(*a, **kw): return json.dumps(*a, indent=4, sort_keys=True, **kw) def failed(*a, **kw): - item = a[0] + ''' Test if task result yields failed ''' + item = a[0] if type(item) != dict: raise errors.AnsibleFilterError("|failed expects a dictionary") rc = item.get('rc',0) @@ -45,9 +46,18 @@ def failed(*a, **kw): return False def success(*a, **kw): + ''' Test if task result yields success ''' return not failed(*a, **kw) +def changed(*a, **kw): + ''' Test if task result yields changed ''' + item = a[0] + if type(item) != dict: + raise errors.AnsibleFilterError("|changed expects a dictionary") + return item.get('changed', False) + def skipped(*a, **kw): + ''' Test if task result yields skipped ''' item = a[0] if type(item) != dict: raise errors.AnsibleFilterError("|skipped expects a dictionary") @@ -106,6 +116,9 @@ class FilterModule(object): 'failed' : failed, 'success' : success, + # changed testing + 'changed' : changed, + # skip testing 'skipped' : skipped, From c4d20094b42f64c01af6cdf27729517b5cc7f4ee Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Thu, 10 Oct 2013 22:08:38 +0200 Subject: [PATCH 2/2] make changed filter understand results lists --- lib/ansible/runner/filter_plugins/core.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/ansible/runner/filter_plugins/core.py b/lib/ansible/runner/filter_plugins/core.py index 15d13e6c69..55bde22abc 100644 --- a/lib/ansible/runner/filter_plugins/core.py +++ b/lib/ansible/runner/filter_plugins/core.py @@ -54,7 +54,16 @@ def changed(*a, **kw): item = a[0] if type(item) != dict: raise errors.AnsibleFilterError("|changed expects a dictionary") - return item.get('changed', False) + if not 'changed' in item: + changed = False + if ('results' in item # some modules return a 'results' key + and type(item['results']) == list + and type(item['results'][0]) == dict): + for result in item['results']: + changed = changed or result.get('changed', False) + else: + changed = item.get('changed', False) + return changed def skipped(*a, **kw): ''' Test if task result yields skipped '''