diff --git a/ansible_risk_insight/model_loader.py b/ansible_risk_insight/model_loader.py index 2fdf8cff..97a96931 100644 --- a/ansible_risk_insight/model_loader.py +++ b/ansible_risk_insight/model_loader.py @@ -334,6 +334,7 @@ def load_play( pre_tasks = [] post_tasks = [] tasks = [] + handlers = [] roles = [] variables = {} module_defaults = {} @@ -341,7 +342,7 @@ def load_play( import_module = "" import_playbook = "" - tasks_keys = ["pre_tasks", "tasks", "post_tasks"] + tasks_keys = ["pre_tasks", "tasks", "post_tasks", "handlers"] keys = [k for k in data_block if k not in tasks_keys] keys.extend(tasks_keys) task_count = 0 @@ -482,6 +483,47 @@ def load_play( if error: task_loading["failure"] += 1 task_loading["errors"].append(error) + elif k == "handlers": + if not isinstance(v, list): + continue + task_blocks, _ = get_task_blocks(task_dict_list=v) + if task_blocks is None: + continue + for task_dict in task_blocks: + i = task_count + task_loading["total"] += 1 + error = None + try: + t = load_task( + path=path, + index=i, + task_block_dict=task_dict, + role_name=role_name, + collection_name=collection_name, + collections_in_play=collections_in_play, + play_index=index, + parent_key=pbObj.key, + parent_local_key=pbObj.local_key, + yaml_lines=yaml_lines, + basedir=basedir, + ) + handlers.append(t) + if t: + task_loading["success"] += 1 + except TaskFormatError as exc: + error = exc + if skip_task_format_error: + logger.debug("this task is wrong format; skip the task in {}," " index: {}; skip this".format(path, i)) + else: + raise TaskFormatError(f"this task is wrong format; skip the task in {path}," " index: {i}") + except Exception as exc: + error = exc + logger.exception("error while loading the task at {} (index={})".format(path, i)) + finally: + task_count += 1 + if error: + task_loading["failure"] += 1 + task_loading["errors"].append(error) elif k == "roles": if not isinstance(v, list): continue @@ -538,6 +580,7 @@ def load_play( pbObj.pre_tasks = pre_tasks pbObj.tasks = tasks pbObj.post_tasks = post_tasks + pbObj.handlers = handlers pbObj.roles = roles pbObj.variables = variables pbObj.module_defaults = module_defaults diff --git a/ansible_risk_insight/models.py b/ansible_risk_insight/models.py index c8834e0e..a22dfef7 100644 --- a/ansible_risk_insight/models.py +++ b/ansible_risk_insight/models.py @@ -1936,6 +1936,7 @@ class Play(Object, Resolvable): pre_tasks: list = field(default_factory=list) tasks: list = field(default_factory=list) post_tasks: list = field(default_factory=list) + handlers: list = field(default_factory=list) # not actual Role, but RoleInPlay defined in this playbook roles: list = field(default_factory=list) module_defaults: dict = field(default_factory=dict) @@ -1958,6 +1959,9 @@ def children_to_key(self): post_task_keys = [t.key if isinstance(t, Task) else t for t in self.post_tasks] self.post_tasks = post_task_keys + + handler_task_keys = [t.key if isinstance(t, Task) else t for t in self.handlers] + self.handlers = handler_task_keys return self @property diff --git a/ansible_risk_insight/parser.py b/ansible_risk_insight/parser.py index 955c3ec0..5e9a54f8 100644 --- a/ansible_risk_insight/parser.py +++ b/ansible_risk_insight/parser.py @@ -319,9 +319,11 @@ def run(self, load_data=None, load_json_path="", collection_name_of_project=""): pre_tasks_in_plays = [t for p in plays for t in p.pre_tasks] tasks_in_plays = [t for p in plays for t in p.tasks] post_tasks_in_plays = [t for p in plays for t in p.post_tasks] + handlers_in_plays = [t for p in plays for t in p.handlers] tasks.extend(pre_tasks_in_plays) tasks.extend(tasks_in_plays) tasks.extend(post_tasks_in_plays) + tasks.extend(handlers_in_plays) modules = [m for r in roles for m in r.modules] module_specs = {} diff --git a/ansible_risk_insight/tree.py b/ansible_risk_insight/tree.py index 7dfc9170..ea33a712 100644 --- a/ansible_risk_insight/tree.py +++ b/ansible_risk_insight/tree.py @@ -712,8 +712,6 @@ def _get_children_keys(self, obj, handover_from_upper_node={}): resolved_playbook_key = resolve_playbook(obj.import_playbook, self.dicts["playbooks"], obj.key) if resolved_playbook_key != "": children_keys.append(resolved_playbook_key) - children_keys.extend(obj.pre_tasks) - children_keys.extend(obj.tasks) for rip in obj.roles: if not isinstance(rip, RoleInPlay): continue @@ -773,7 +771,10 @@ def _get_children_keys(self, obj, handover_from_upper_node={}): if "roles_info" not in handover: handover["roles_info"] = {} handover["roles_info"][rip.key] = resolved_role_key + children_keys.extend(obj.pre_tasks) + children_keys.extend(obj.tasks) children_keys.extend(obj.post_tasks) + children_keys.extend(obj.handlers) elif isinstance(obj, Role): target_taskfiles = ["main.yml", "main.yaml"] if isinstance(handover_from_upper_node, dict) and "tasks_from" in handover_from_upper_node: