From 8a4d29134225d831a9f66068ab958196937aafe4 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sat, 26 Sep 2020 14:13:09 +0200 Subject: [PATCH] fix issue with conflicting action statements This PR will address https://github.com/xoxys/ansible-later/issues/35. Collected metadata will be temp. removed from the raw task dict before passing it to Ansibles `ModuleArgsParser` class. After processing, the metadata will be added back to the result dict. --- ansiblelater/utils/yamlhelper.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/ansiblelater/utils/yamlhelper.py b/ansiblelater/utils/yamlhelper.py index fa5c3fc..f08ffe5 100644 --- a/ansiblelater/utils/yamlhelper.py +++ b/ansiblelater/utils/yamlhelper.py @@ -396,16 +396,26 @@ def _kv_to_dict(v): def normalize_task(task, filename, custom_modules=[]): """Ensure tasks have an action key and strings are converted to python objects.""" ansible_action_type = task.get("__ansible_action_type__", "task") - ansible_action_meta = task.get("__ansible_action_meta__", dict()) if "__ansible_action_type__" in task: del (task["__ansible_action_type__"]) + # temp. extract metadata + ansible_meta = dict() + for key in ["__line__", "__file__", "__ansible_action_meta__"]: + default = None + + if key == "__ansible_action_meta__": + default = dict() + + ansible_meta[key] = task.pop(key, default) + normalized = dict() - # TODO: Workaround for custom modules + builtin = list(ansible.parsing.mod_args.BUILTIN_TASKS) builtin = list(set(builtin + custom_modules)) ansible.parsing.mod_args.BUILTIN_TASKS = frozenset(builtin) mod_arg_parser = ModuleArgsParser(task) + try: action, arguments, normalized["delegate_to"] = mod_arg_parser.parse() except AnsibleParserError as e: @@ -435,7 +445,12 @@ def normalize_task(task, filename, custom_modules=[]): normalized[FILENAME_KEY] = filename normalized["__ansible_action_type__"] = ansible_action_type - normalized["__ansible_action_meta__"] = ansible_action_meta + + # add back extracted metadata + for (k, v) in ansible_meta.items(): + if v: + normalized[k] = v + return normalized