zuul-jobs/.rules/ZuulJobsNamespaceLoopVar.py
Albin Vass 9ad8c11469 Check blocks recursively for loops
Also updates tests for the custom ansible-lint rules.

Adds a simple testsuite so test roles and playbooks that are known to be
faulty or valid can be put in a directory structure like:

<rule id>/valid/playbooks/valid_playbook.yaml
<rule id>/valid/roles/valid_role/tasks/main.yaml
<rule id>/valid/roles/valid_role/handlers/main.yaml

<rule id>/faulty/playbooks/faulty_playbook.yaml
<rule id>/faulty/roles/faulty_role/tasks/main.yaml
<rule id>/faulty/roles/faulty_role/handlers/main.yaml

So they will verify that the rules work as expected.

Change-Id: I29d0766c67e690e35490586f6befb95e4cd31b98
2020-05-05 22:56:46 +02:00

56 lines
1.6 KiB
Python

from ansiblelint import AnsibleLintRule
class ZuulJobsNamespaceLoopVar(AnsibleLintRule):
id = 'ZUULJOBS0001'
shortdesc = 'Loop vars should have zj_ prefix'
description = """
Check for tasks that does not follow
the policy of namespacing loop variables with zj_ prefix.
See: \
https://zuul-ci.org/docs/zuul-jobs/policy.html\
#ansible-loops-in-roles
"""
tags = {'zuul-jobs-namespace-loop-var'}
def matchplay(self, file, task):
results = []
if file.get('type') not in ('tasks', 'handlers'):
return results
results.extend(self.handle_play(task))
return results
def handle_play(self, task):
results = []
if 'block' in task:
results.extend(self.handle_playlist(task['block']))
else:
results.extend(self.handle_task(task))
return results
def handle_playlist(self, playlist):
results = []
for play in playlist:
results.extend(self.handle_play(play))
return results
def handle_task(self, task):
results = []
has_loop = 'loop' in task
for key in task.keys():
if key.startswith('with_'):
has_loop = True
if has_loop:
if 'loop_control' not in task:
results.append(("", self.shortdesc))
elif 'loop_var' not in task.get('loop_control'):
results.append(("", self.shortdesc))
elif not task.get('loop_control')\
.get('loop_var').startswith('zj_'):
results.append(("", self.shortdesc))
return results