diff --git a/ansible_risk_insight/model_loader.py b/ansible_risk_insight/model_loader.py index 944fbda3..738290bd 100644 --- a/ansible_risk_insight/model_loader.py +++ b/ansible_risk_insight/model_loader.py @@ -482,6 +482,7 @@ def load_play( task_blocks, _ = get_task_blocks(task_dict_list=v) if task_blocks is None: continue + last_task_line_num = -1 for task_dict in task_blocks: task_loading["total"] += 1 i = task_count @@ -498,11 +499,14 @@ def load_play( parent_key=pbObj.key, parent_local_key=pbObj.local_key, yaml_lines=yaml_lines, + previous_task_line=last_task_line_num, basedir=basedir, ) pre_tasks.append(t) if t: task_loading["success"] += 1 + if t.line_num_in_file and len(t.line_num_in_file) == 2: + last_task_line_num = t.line_num_in_file[1] except TaskFormatError as exc: error = exc if skip_task_format_error: @@ -523,6 +527,7 @@ def load_play( task_blocks, _ = get_task_blocks(task_dict_list=v) if task_blocks is None: continue + last_task_line_num = -1 for task_dict in task_blocks: i = task_count task_loading["total"] += 1 @@ -539,11 +544,14 @@ def load_play( parent_key=pbObj.key, parent_local_key=pbObj.local_key, yaml_lines=yaml_lines, + previous_task_line=last_task_line_num, basedir=basedir, ) tasks.append(t) if t: task_loading["success"] += 1 + if t.line_num_in_file and len(t.line_num_in_file) == 2: + last_task_line_num = t.line_num_in_file[1] except TaskFormatError as exc: error = exc if skip_task_format_error: @@ -564,6 +572,7 @@ def load_play( task_blocks, _ = get_task_blocks(task_dict_list=v) if task_blocks is None: continue + last_task_line_num = -1 for task_dict in task_blocks: i = task_count task_loading["total"] += 1 @@ -580,11 +589,14 @@ def load_play( parent_key=pbObj.key, parent_local_key=pbObj.local_key, yaml_lines=yaml_lines, + previous_task_line=last_task_line_num, basedir=basedir, ) post_tasks.append(t) if t: task_loading["success"] += 1 + if t.line_num_in_file and len(t.line_num_in_file) == 2: + last_task_line_num = t.line_num_in_file[1] except TaskFormatError as exc: error = exc if skip_task_format_error: @@ -605,6 +617,7 @@ def load_play( task_blocks, _ = get_task_blocks(task_dict_list=v) if task_blocks is None: continue + last_task_line_num = -1 for task_dict in task_blocks: i = task_count task_loading["total"] += 1 @@ -621,11 +634,14 @@ def load_play( parent_key=pbObj.key, parent_local_key=pbObj.local_key, yaml_lines=yaml_lines, + previous_task_line=last_task_line_num, basedir=basedir, ) handlers.append(t) if t: task_loading["success"] += 1 + if t.line_num_in_file and len(t.line_num_in_file) == 2: + last_task_line_num = t.line_num_in_file[1] except TaskFormatError as exc: error = exc if skip_task_format_error: @@ -1440,6 +1456,7 @@ def load_task( parent_key="", parent_local_key="", yaml_lines="", + previous_task_line=-1, basedir="", ): @@ -1481,7 +1498,12 @@ def load_task( module_name=module_name, module_options=module_options, task_options=task_options, + previous_task_line=previous_task_line, ) + print("[DEBUG] fullpath:", fullpath) + print("[DEBUG] index:", index) + print("[DEBUG] task_name:", task_name) + print("[DEBUG] taskObj.line_num_in_file:", taskObj.line_num_in_file) # module_options can be passed as a string like below # @@ -1642,6 +1664,7 @@ def load_taskfile(path, yaml_str="", role_name="", collection_name="", basedir=" "failure": 0, "errors": [], } + last_task_line_num = -1 for i, t_dict in enumerate(task_dicts): task_loading["total"] += 1 error = None @@ -1655,11 +1678,14 @@ def load_taskfile(path, yaml_str="", role_name="", collection_name="", basedir=" yaml_lines=yaml_lines, parent_key=tfObj.key, parent_local_key=tfObj.local_key, + previous_task_line=last_task_line_num, basedir=basedir, ) tasks.append(t) if t: task_loading["success"] += 1 + if t.line_num_in_file and len(t.line_num_in_file) == 2: + last_task_line_num = t.line_num_in_file[1] except TaskFormatError as exc: error = exc if skip_task_format_error: diff --git a/ansible_risk_insight/models.py b/ansible_risk_insight/models.py index e4fdcb91..7773b884 100644 --- a/ansible_risk_insight/models.py +++ b/ansible_risk_insight/models.py @@ -1110,7 +1110,7 @@ class Task(Object, Resolvable): module_info: dict = field(default_factory=dict) include_info: dict = field(default_factory=dict) - def set_yaml_lines(self, fullpath="", yaml_lines="", task_name="", module_name="", module_options=None, task_options=None): + def set_yaml_lines(self, fullpath="", yaml_lines="", task_name="", module_name="", module_options=None, task_options=None, previous_task_line=-1): if not task_name and not module_options: return @@ -1127,6 +1127,12 @@ def set_yaml_lines(self, fullpath="", yaml_lines="", task_name="", module_name=" # - if module option is dict, at least one key is included candidate_line_nums = [] for i, line in enumerate(lines): + + # skip lines until `previous_task_line` if provided + if previous_task_line > 0: + if i <= previous_task_line - 1: + continue + if task_name: if task_name in line: candidate_line_nums.append(i)