Skip to content

Commit

Permalink
Avoid crash with None tasks
Browse files Browse the repository at this point in the history
As ansible allows tasks to be None, we should also avoid crashing when
encountering them.

Includes regression prevention by assuring we can parse files
with empty tasks.

Fixes: #468
  • Loading branch information
ssbarnea committed Oct 2, 2020
1 parent 8ae9768 commit aee0027
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
3 changes: 3 additions & 0 deletions examples/example.yml
Expand Up @@ -47,3 +47,6 @@
- name: always run
debug: msg="always_run is deprecated"
always_run: true

# empty task is currently accepted by ansible as valid code:
-
7 changes: 4 additions & 3 deletions lib/ansiblelint/rules/BecomeUserWithoutBecomeRule.py
Expand Up @@ -34,14 +34,15 @@ def _get_subtasks(data):
'always',
'rescue']
for name in block_names:
if name in data:
if data and name in data:
result += (data[name] or [])
return result


def _nested_search(term, data):
return ((term in data) or
reduce((lambda x, y: x or _nested_search(term, y)), _get_subtasks(data), False))
if data and term in data:
return True
return reduce((lambda x, y: x or _nested_search(term, y)), _get_subtasks(data), False)


def _become_user_without_become(becomeuserabove, data):
Expand Down
5 changes: 3 additions & 2 deletions lib/ansiblelint/rules/IncludeMissingFileRule.py
Expand Up @@ -27,8 +27,9 @@ def matchplay(self, file, data):
# avoid failing with a playbook having tasks: null
for task in (data.get('tasks', []) or []):

# check if the id of the current rule is not in list of skipped rules for this play
if self.id in task.get('skipped_rules', ()):
# ignore None tasks or
# if the id of the current rule is not in list of skipped rules for this play
if not task or self.id in task.get('skipped_rules', ()):
continue

# collect information which file was referenced for include / import
Expand Down
7 changes: 6 additions & 1 deletion lib/ansiblelint/skip_utils.py
Expand Up @@ -110,6 +110,11 @@ def _append_skipped_rules(pyyaml_data: Sequence, file_text: str, file_type: File

# append skipped_rules for each task
for ruamel_task, pyyaml_task in zip(ruamel_tasks, pyyaml_tasks):

# ignore empty tasks
if not pyyaml_task and not ruamel_task:
continue

if pyyaml_task.get('name') != ruamel_task.get('name'):
raise RuntimeError('Error in matching skip comment to a task')
pyyaml_task['skipped_rules'] = _get_rule_skips_from_yaml(ruamel_task)
Expand Down Expand Up @@ -147,7 +152,7 @@ def _get_tasks_from_blocks(task_blocks: Sequence) -> Generator:
def get_nested_tasks(task: Any) -> Generator[Any, None, None]:
return (
subtask
for k in NESTED_TASK_KEYS if k in task
for k in NESTED_TASK_KEYS if task and k in task
for subtask in task[k]
)

Expand Down
15 changes: 15 additions & 0 deletions lib/ansiblelint/utils.py
Expand Up @@ -235,6 +235,11 @@ def _include_children(basedir, k, v, parent_type):
def _taskshandlers_children(basedir, k, v, parent_type: FileType) -> List:
results = []
for th in v:

# ignore empty tasks, `-`
if not th:
continue

with contextlib.suppress(LookupError):
children = _get_task_handler_children_for_tasks_or_playbooks(
th, basedir, k, parent_type,
Expand Down Expand Up @@ -267,9 +272,16 @@ def _get_task_handler_children_for_tasks_or_playbooks(
) -> dict:
"""Try to get children of taskhandler for include/import tasks/playbooks."""
child_type = k if parent_type == 'playbook' else parent_type

task_include_keys = 'include', 'include_tasks', 'import_playbook', 'import_tasks'
for task_handler_key in task_include_keys:

with contextlib.suppress(KeyError):

# ignore empty tasks
if not task_handler:
continue

return {
'path': path_dwim(basedir, task_handler[task_handler_key]),
'type': child_type,
Expand Down Expand Up @@ -546,6 +558,9 @@ def extract_from_list(blocks, candidates):
def add_action_type(actions, action_type):
results = list()
for action in actions:
# ignore empty task
if not action:
continue
action['__ansible_action_type__'] = BLOCK_NAME_TO_ACTION_TYPE_MAP[action_type]
results.append(action)
return results
Expand Down

0 comments on commit aee0027

Please sign in to comment.