Skip to content

Commit

Permalink
Allow profile to be set in config file (#2720)
Browse files Browse the repository at this point in the history
Co-authored-by: Sorin Sbarnea <[email protected]>
Closes #2686
  • Loading branch information
shatakshiiii committed Nov 23, 2022
1 parent 9d10bcc commit 0f7ca56
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
# .ansible-lint

profile: null # min, basic, moderate,safety, shared, production

# exclude_paths included in this file are parsed relative to this file's location
# and not relative to the CWD of execution. CLI arguments passed to the --exclude
# option are parsed relative to the CWD of execution.
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def main(argv: list[str] | None = None) -> int: # noqa: C901
from ansiblelint.rules import RulesCollection
from ansiblelint.runner import _get_matches

rules = RulesCollection(options.rulesdirs, profile=options.profile)
rules = RulesCollection(options.rulesdirs, profile_name=options.profile)

if options.list_profiles:
from ansiblelint.generate_docs import profiles_as_rich
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ def merge_config(file_config: dict[Any, Any], cli_config: Namespace) -> Namespac
scalar_map = {
"loop_var_prefix": None,
"project_dir": ".",
"profile": None,
}

if not file_config:
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
skip_action_validation=True,
strict=False,
rules={}, # Placeholder to set and keep configurations for each rule.
profile=None,
)

# Used to store detected tag deprecations
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/data/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ safety:
risky-octal:
risky-shell-pipe:
shared:
extends: safety
description: >
The `shared` profile ensures that content follows best practices for packaging and publishing.
This profile is intended for content creators who want to make Ansible
playbooks, roles, or collections available from
[galaxy.ansible.com](https://galaxy.ansible.com),
[automation-hub](https://console.redhat.com/ansible/automation-hub),
or a private instance.
extends: safety
rules:
galaxy: # <-- applies to both galaxy and automation-hub
ignore-errors:
Expand Down
10 changes: 6 additions & 4 deletions src/ansiblelint/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,14 @@ def __init__(
self,
rulesdirs: list[str] | None = None,
options: Namespace = default_options,
profile: list[str] | None = None,
profile_name: str | None = None,
conditional: bool = True,
) -> None:
"""Initialize a RulesCollection instance."""
self.options = options
self.profile = profile or []
self.profile = []
if profile_name:
self.profile = PROFILES[profile_name]
if rulesdirs is None:
rulesdirs = []
self.rulesdirs = expand_paths_vars(rulesdirs)
Expand All @@ -396,8 +398,8 @@ def __init__(
self.rules = sorted(self.rules)

# when we have a profile we unload some of the rules
if self.profile:
filter_rules_with_profile(self.rules, self.profile[0])
if profile_name:
filter_rules_with_profile(self.rules, profile_name)

def register(self, obj: AnsibleLintRule, conditional: bool = False) -> None:
"""Register a rule."""
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/rules/role_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def matchtask(
results = []
if task["action"]["__ansible_module__"] in ROLE_IMPORT_ACTION_NAMES:
name = task["action"].get("name", "")
# breakpoint()
if "/" in name:
results.append(
self.create_matcherror(
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/rules/syntax_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ def _get_ansible_syntax_check_matches(lintable: Lintable) -> list[MatchError]:
if pattern.tag == "empty-playbook":
rule = WarningRule()

# breakpoint()
results.append(
MatchError(
message=title,
Expand Down
5 changes: 5 additions & 0 deletions src/ansiblelint/schemas/ansible-lint-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"additionalProperties": false,
"examples": [".ansible-lint", ".config/ansible-lint.yml"],
"properties": {
"profile": {
"title": "Profile",
"type": ["null", "string"],
"enum": ["min", "basic", "moderate", "safety", "shared", "production", null]
},
"enable_list": {
"items": {
"type": "string"
Expand Down
1 change: 0 additions & 1 deletion src/ansiblelint/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def __init__(self, collection: RulesCollection) -> None:

def _call_runner(self, path: str) -> list[MatchError]:
runner = Runner(path, rules=self.collection)
# breakpoint()
return runner.run()

def run(self, filename: str) -> list[MatchError]:
Expand Down

0 comments on commit 0f7ca56

Please sign in to comment.