Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ansiblelint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self, options: Options):
# pylint: disable=import-outside-toplevel
from ansiblelint.yaml_utils import load_yamllint_config

self.yamllint_config = load_yamllint_config()
self.yamllint_config = load_yamllint_config(options.yamllint_file)

def render_matches(self, matches: list[MatchError]) -> None:
"""Display given matches (if they are not fixed)."""
Expand Down
7 changes: 7 additions & 0 deletions src/ansiblelint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@ def get_cli_parser() -> argparse.ArgumentParser:
default=None,
help=f"Specify ignore file to use. By default it will look for '{IGNORE_FILE.default}' or '{IGNORE_FILE.alternative}'",
)
parser.add_argument(
"--yamllint-file",
dest="yamllint_file",
type=Path,
default=None,
help="Specify yamllint config file to use. By default it will look for '.yamllint', '.yamllint.yaml', '.yamllint.yml', '~/.config/yamllint/config' or environment variables XDG_CONFIG_HOME and YAMLLINT_CONFIG_FILE.",
)
parser.add_argument(
"--offline",
dest="offline",
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class Options: # pylint: disable=too-many-instance-attributes
version: bool = False # display version command
list_profiles: bool = False # display profiles command
ignore_file: Path | None = None
yamllint_file: Path | None = None
max_tasks: int = 100
max_block_depth: int = 20
# Refer to https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html#ansible-core-support-matrix
Expand Down
14 changes: 10 additions & 4 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,25 @@ def deannotate(data: Any) -> Any:
return data


def load_yamllint_config() -> CustomYamlLintConfig:
def load_yamllint_config(yamllint_file: Path | None = None) -> CustomYamlLintConfig:
"""Load our default yamllint config and any customized override file."""
config = CustomYamlLintConfig(file=Path(__file__).parent / "data" / ".yamllint")
config.incompatible = ""
# if we detect local yamllint config we use it but raise a warning
# Declare local yamllint config file locations.
# If we detect local yamllint config we use it but raise a warning
# as this is likely to get out of sync with our internal config.
for path in [
yamllint_config_locations = [
".yamllint",
".yamllint.yaml",
".yamllint.yml",
os.getenv("YAMLLINT_CONFIG_FILE", ""),
os.getenv("XDG_CONFIG_HOME", "~/.config") + "/yamllint/config",
]:
]
if yamllint_file:
# Ensure the CLI option yamllint_file config is the first
# file to be loaded
yamllint_config_locations.insert(0, str(yamllint_file))
for path in yamllint_config_locations:
file = Path(path).expanduser()
if file.is_file():
_logger.debug(
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/yamllint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
rules:
line-length:
max: 222
7 changes: 7 additions & 0 deletions test/test_yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,3 +1035,10 @@ def test_document_start(
yaml.dumps(yaml.load(_SINGLE_QUOTE_WITHOUT_INDENTS)).startswith("---")
== explicit_start
)


def test_yamllint_file_config_loaded() -> None:
"""Ensure the yamllint configuration from a file is loaded correctly."""
config_fixture = Path(fixtures_dir / "yamllint.yml")
config = ansiblelint.yaml_utils.load_yamllint_config(yamllint_file=config_fixture)
assert config.rules["line-length"]["max"] == 222
Loading