Skip to content

Commit

Permalink
fix to load handlers in plays and to apply rules to them (#196)
Browse files Browse the repository at this point in the history
Signed-off-by: hirokuni-kitahara <[email protected]>
  • Loading branch information
hirokuni-kitahara authored Oct 10, 2023
1 parent 8dcbb75 commit 86aabb2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
45 changes: 44 additions & 1 deletion ansible_risk_insight/model_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,15 @@ def load_play(
pre_tasks = []
post_tasks = []
tasks = []
handlers = []
roles = []
variables = {}
module_defaults = {}
play_options = {}
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions ansible_risk_insight/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions ansible_risk_insight/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
5 changes: 3 additions & 2 deletions ansible_risk_insight/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 86aabb2

Please sign in to comment.