ansible-later/ansiblelater/rules/rolefiles.py

36 lines
1.2 KiB
Python
Raw Normal View History

2019-04-10 12:50:48 +00:00
"""Checks related to ansible roles files."""
2019-04-15 16:55:54 +00:00
from ansible.parsing.yaml.objects import AnsibleMapping
2018-12-19 10:19:07 +00:00
from nested_lookup import nested_lookup
from ansiblelater.command.candidates import Error
from ansiblelater.command.candidates import Result
from ansiblelater.utils.rulehelper import get_raw_yaml
from ansiblelater.utils.rulehelper import get_tasks
2018-12-19 10:19:07 +00:00
def check_meta_main(candidate, settings):
content, errors = get_raw_yaml(candidate, settings)
keys = ["author", "description", "min_ansible_version", "platforms", "dependencies"]
2020-04-11 14:20:41 +00:00
description = "file should contain '{key}' key"
2018-12-19 10:19:07 +00:00
if not errors:
for key in keys:
if not nested_lookup(key, content):
2020-04-11 14:20:41 +00:00
errors.append(Error(None, description.format(key=key)))
2018-12-19 10:19:07 +00:00
return Result(candidate.path, errors)
def check_scm_in_src(candidate, settings):
roles, errors = get_tasks(candidate, settings)
description = "usage of src: scm+url not recommended"
if not errors:
for role in roles:
2019-04-15 15:26:02 +00:00
if isinstance(role, AnsibleMapping):
if "+" in role.get("src"):
errors.append(Error(role["__line__"], description))
2018-12-19 10:19:07 +00:00
return Result(candidate.path, errors)