| 1 |
"""Store configuration options as a singleton.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import json |
| 6 |
import logging |
| 7 |
import os |
| 8 |
import sys |
| 9 |
import time |
| 10 |
import urllib.request |
| 11 |
import warnings |
| 12 |
from dataclasses import dataclass, field |
| 13 |
from http.client import HTTPException |
| 14 |
from importlib.metadata import PackageNotFoundError, distribution, version |
| 15 |
from pathlib import Path |
| 16 |
from typing import Any |
| 17 |
from urllib.error import HTTPError, URLError |
| 18 |
|
| 19 |
from packaging.version import Version |
| 20 |
|
| 21 |
from ansiblelint import __version__ |
| 22 |
from ansiblelint.loaders import yaml_from_file |
| 23 |
|
| 24 |
_logger = logging.getLogger(__name__) |
| 25 |
|
| 26 |
|
| 27 |
CACHE_DIR = ( |
| 28 |
os.path.expanduser(os.environ.get("XDG_CACHE_HOME", "~/.cache")) + "/ansible-lint" |
| 29 |
) |
| 30 |
|
| 31 |
DEFAULT_WARN_LIST = [ |
| 32 |
"experimental", |
| 33 |
"jinja[spacing]", # warning until we resolve all reported false-positives |
| 34 |
"fqcn[deep]", # 2023-05-31 added |
| 35 |
] |
| 36 |
|
| 37 |
DEFAULT_KINDS = [ |
| 38 |
# Do not sort this list, order matters. |
| 39 |
{"jinja2": "**/*.j2"}, # jinja2 templates are not always parsable as something else |
| 40 |
{"jinja2": "**/*.j2.*"}, |
| 41 |
{"yaml": ".github/**/*.{yaml,yml}"}, # github workflows |
| 42 |
{"text": "**/templates/**/*.*"}, # templates are unlikely to validate |
| 43 |
{"execution-environment": "**/execution-environment.yml"}, |
| 44 |
{"ansible-lint-config": "**/.ansible-lint"}, |
| 45 |
{"ansible-lint-config": "**/.ansible-lint.{yaml,yml}"}, |
| 46 |
{"ansible-lint-config": "**/.config/ansible-lint.{yaml,yml}"}, |
| 47 |
{"ansible-navigator-config": "**/ansible-navigator.{yaml,yml}"}, |
| 48 |
{"inventory": "**/inventory/**.{yaml,yml}"}, |
| 49 |
{"requirements": "**/meta/requirements.{yaml,yml}"}, # v1 only |
| 50 |
# https://docs.ansible.com/projects/ansible/latest/dev_guide/collections_galaxy_meta.html |
| 51 |
{"galaxy": "**/galaxy.yml"}, # Galaxy collection meta |
| 52 |
{"reno": "**/releasenotes/*/*.{yaml,yml}"}, # reno release notes |
| 53 |
{"vars": "**/{host_vars,group_vars,vars,defaults}/**/*.{yaml,yml}"}, |
| 54 |
{"tasks": "**/tasks/**/*.{yaml,yml}"}, |
| 55 |
{"rulebook": "**/rulebooks/*.{yml,yaml"}, |
| 56 |
{"pattern": "**/extensions/patterns/*/meta/pattern.json"}, |
| 57 |
{"play-argspec": "**/*.meta.{yaml,yml}"}, |
| 58 |
{"playbook": "**/playbooks/*.{yml,yaml}"}, |
| 59 |
{"playbook": "**/*playbook*.{yml,yaml}"}, |
| 60 |
{"playbook": "**/extensions/patterns/*/playbooks/*.{yml,yaml}"}, |
| 61 |
{"role": "**/roles/*/"}, |
| 62 |
{"handlers": "**/handlers/*.{yaml,yml}"}, |
| 63 |
{"test-meta": "**/tests/integration/targets/*/meta/main.{yaml,yml}"}, |
| 64 |
{"meta": "**/meta/main.{yaml,yml}"}, |
| 65 |
{"meta-runtime": "**/meta/runtime.{yaml,yml}"}, |
| 66 |
{"role-arg-spec": "**/meta/argument_specs.{yaml,yml}"}, # role argument specs |
| 67 |
{"yaml": ".config/molecule/config.{yaml,yml}"}, # molecule global config |
| 68 |
{ |
| 69 |
"requirements": "**/molecule/*/{collections,requirements}.{yaml,yml}", |
| 70 |
}, # molecule old collection requirements (v1), ansible 2.8 only |
| 71 |
{"yaml": "**/molecule/*/{base,molecule}.{yaml,yml}"}, # molecule config |
| 72 |
{"requirements": "**/requirements.{yaml,yml}"}, # v2 and v1 |
| 73 |
{"playbook": "**/molecule/*/*.{yaml,yml}"}, # molecule playbooks |
| 74 |
{"yaml": "**/{.ansible-lint,.yamllint}"}, |
| 75 |
{"changelog": "**/changelogs/changelog.{yaml,yml}"}, |
| 76 |
{"yaml": "**/*.{yaml,yml}"}, |
| 77 |
{"yaml": "**/.*.{yaml,yml}"}, |
| 78 |
{"sanity-ignore-file": "**/tests/sanity/ignore-*.txt"}, |
| 79 |
# what are these doc_fragments? We also ignore module_utils for now |
| 80 |
{ |
| 81 |
"plugin": "**/plugins/{action,become,cache,callback,connection,filter,inventory,lookup,modules,test}/**/*.py", |
| 82 |
}, |
| 83 |
{"python": "**/*.py"}, |
| 84 |
] |
| 85 |
|
| 86 |
BASE_KINDS = [ |
| 87 |
# These assignations are only for internal use and are only inspired by |
| 88 |
# MIME/IANA model. Their purpose is to be able to process a file based on |
| 89 |
# it type, including generic processing of text files using the prefix. |
| 90 |
{ |
| 91 |
"text/jinja2": "**/*.j2", |
| 92 |
}, # jinja2 templates are not always parsable as something else |
| 93 |
{"text/jinja2": "**/*.j2.*"}, |
| 94 |
{"text": "**/templates/**/*.*"}, # templates are unlikely to validate |
| 95 |
{"text/json": "**/*.json"}, # standardized |
| 96 |
{"text/markdown": "**/*.md"}, # https://tools.ietf.org/html/rfc7763 |
| 97 |
{"text/rst": "**/*.rst"}, # https://en.wikipedia.org/wiki/ReStructuredText |
| 98 |
{"text/ini": "**/*.ini"}, |
| 99 |
# YAML has no official IANA assignation |
| 100 |
{"text/yaml": "**/{.ansible-lint,.yamllint}"}, |
| 101 |
{"text/yaml": "**/*.{yaml,yml}"}, |
| 102 |
{"text/yaml": "**/.*.{yaml,yml}"}, |
| 103 |
{"text/python": "**/*.py"}, |
| 104 |
] |
| 105 |
|
| 106 |
# File kinds that are recognized by ansible, used internally to force use of |
| 107 |
# YAML 1.1 instead of 1.2 due to ansible-core dependency on pyyaml. |
| 108 |
ANSIBLE_OWNED_KINDS = { |
| 109 |
"handlers", |
| 110 |
"galaxy", |
| 111 |
"meta", |
| 112 |
"meta-runtime", |
| 113 |
"playbook", |
| 114 |
"requirements", |
| 115 |
"role-arg-spec", |
| 116 |
"rulebook", |
| 117 |
"tasks", |
| 118 |
"vars", |
| 119 |
} |
| 120 |
|
| 121 |
PROFILES = yaml_from_file(Path(__file__).parent / "data" / "profiles.yml") |
| 122 |
|
| 123 |
LOOP_VAR_PREFIX = "^(__|{role}_)" |
| 124 |
|
| 125 |
|
| 126 |
@dataclass |
| 127 |
class Options: # pylint: disable=too-many-instance-attributes |
| 128 |
"""Store ansible-lint effective configuration options.""" |
| 129 |
|
| 130 |
# Private attributes |
| 131 |
_skip_ansible_syntax_check: bool = False |
| 132 |
|
| 133 |
# Public attributes |
| 134 |
cache_dir: Path | None = None |
| 135 |
colored: bool = True |
| 136 |
configured: bool = False |
| 137 |
cwd: Path = Path() |
| 138 |
display_relative_path: bool = True |
| 139 |
exclude_paths: list[str] = field(default_factory=list) |
| 140 |
format: str = "brief" |
| 141 |
lintables: list[str] = field(default_factory=list) |
| 142 |
list_rules: bool = False |
| 143 |
list_tags: bool = False |
| 144 |
write_list: list[str] = field(default_factory=list) |
| 145 |
write_exclude_list: list[str] = field(default_factory=list) |
| 146 |
quiet: bool = False |
| 147 |
rulesdirs: list[Path] = field(default_factory=list) |
| 148 |
skip_list: list[str] = field(default_factory=list) |
| 149 |
tags: list[str] = field(default_factory=list) |
| 150 |
verbosity: int = 0 |
| 151 |
warn_list: list[str] = field(default_factory=list) |
| 152 |
kinds = DEFAULT_KINDS |
| 153 |
mock_filters: list[str] = field(default_factory=list) |
| 154 |
mock_modules: list[str] = field(default_factory=list) |
| 155 |
mock_roles: list[str] = field(default_factory=list) |
| 156 |
loop_var_prefix: str | None = None |
| 157 |
only_builtins_allow_collections: list[str] = field(default_factory=list) |
| 158 |
only_builtins_allow_modules: list[str] = field(default_factory=list) |
| 159 |
var_naming_pattern: str | None = None |
| 160 |
offline: bool = False |
| 161 |
project_dir: str = "." # default should be valid folder (do not use None here) |
| 162 |
extra_vars: dict[str, Any] | None = None |
| 163 |
enable_list: list[str] = field(default_factory=list) |
| 164 |
skip_action_validation: bool = True |
| 165 |
strict: bool = False |
| 166 |
rules: dict[str, Any] = field( |
| 167 |
default_factory=dict, |
| 168 |
) # Placeholder to set and keep configurations for each rule. |
| 169 |
profile: str | None = None |
| 170 |
task_name_prefix: str = "{stem} | " |
| 171 |
sarif_file: Path | None = None |
| 172 |
config_file: str | None = None |
| 173 |
generate_ignore: bool = False |
| 174 |
rulesdir: list[Path] = field(default_factory=list) |
| 175 |
use_default_rules: bool = False |
| 176 |
version: bool = False # display version command |
| 177 |
list_profiles: bool = False # display profiles command |
| 178 |
ignore_file: Path | None = None |
| 179 |
yamllint_file: Path | None = None |
| 180 |
max_tasks: int = 100 |
| 181 |
max_block_depth: int = 20 |
| 182 |
# Refer to https://docs.ansible.com/projects/ansible/latest/reference_appendices/release_and_maintenance.html#ansible-core-support-matrix |
| 183 |
_default_supported = ["2.15.", "2.16.", "2.17.", "2.18.", "2.19."] |
| 184 |
supported_ansible_also: list[str] = field(default_factory=list) |
| 185 |
|
| 186 |
@property |
| 187 |
def nodeps(self) -> bool: |
| 188 |
"""Returns value of nodeps feature.""" |
| 189 |
# We do not want this to be cached as it would affect our testings. |
| 190 |
return bool(int(os.environ.get("ANSIBLE_LINT_NODEPS", "0"))) |
| 191 |
|
| 192 |
def __post_init__(self) -> None: |
| 193 |
"""Extra initialization logic.""" |
| 194 |
if self.nodeps: |
| 195 |
self.offline = True |
| 196 |
|
| 197 |
@property |
| 198 |
def supported_ansible(self) -> list[str]: |
| 199 |
"""Returns list of ansible versions that are considered supported.""" |
| 200 |
return sorted([*self._default_supported, *self.supported_ansible_also]) |
| 201 |
|
| 202 |
@property |
| 203 |
def mock_collections_path(self) -> Path | None: |
| 204 |
"""Return the mock collections path if cache_dir is set.""" |
| 205 |
if self.cache_dir is None: |
| 206 |
return None |
| 207 |
return self.cache_dir / "collections" |
| 208 |
|
| 209 |
def has_collection_mocks(self) -> bool: |
| 210 |
"""Check if any mock roles or modules use collection format (ns.coll.name).""" |
| 211 |
return any(len(r.split(".")) >= 3 for r in self.mock_roles) or any( |
| 212 |
len(m.split(".")) >= 3 for m in self.mock_modules |
| 213 |
) |
| 214 |
|
| 215 |
|
| 216 |
options = Options() |
| 217 |
|
| 218 |
# Used to store detected tag deprecations |
| 219 |
used_old_tags: dict[str, str] = {} |
| 220 |
|
| 221 |
# Used to store log messages before logging is initialized (level, message) |
| 222 |
log_entries: list[tuple[int, str]] = [] |
| 223 |
|
| 224 |
|
| 225 |
def in_venv() -> bool: |
| 226 |
"""Determine whether Python is running from a venv.""" |
| 227 |
if hasattr(sys, "real_prefix") or os.environ.get("CONDA_EXE", None) is not None: |
| 228 |
return True |
| 229 |
|
| 230 |
pfx = getattr(sys, "base_prefix", sys.prefix) |
| 231 |
return pfx != sys.prefix |
| 232 |
|
| 233 |
|
| 234 |
def guess_install_method() -> str: |
| 235 |
"""Guess if pip upgrade command should be used.""" |
| 236 |
package_name = "ansible-lint" |
| 237 |
|
| 238 |
try: |
| 239 |
if (distribution(package_name).read_text("INSTALLER") or "").strip() != "pip": |
| 240 |
return "" |
| 241 |
except PackageNotFoundError as exc: |
| 242 |
_logger.debug(exc) |
| 243 |
return "" |
| 244 |
|
| 245 |
pip = "" |
| 246 |
if in_venv(): |
| 247 |
_logger.debug("Found virtualenv, assuming `pip3 install` will work.") |
| 248 |
pip = f"pip install --upgrade {package_name}" |
| 249 |
elif __file__.startswith(os.path.expanduser("~/.local/lib")): |
| 250 |
_logger.debug( |
| 251 |
"Found --user installation, assuming `pip3 install --user` will work.", |
| 252 |
) |
| 253 |
pip = f"pip3 install --user --upgrade {package_name}" |
| 254 |
|
| 255 |
# By default we assume pip is not safe to be used |
| 256 |
use_pip = False |
| 257 |
try: |
| 258 |
# Use pip to detect if is safe to use it to upgrade the package. |
| 259 |
# We do imports here to for performance and reasons, and also in order |
| 260 |
# to avoid errors if pip internals change. Also we want to avoid having |
| 261 |
# to add pip as a dependency, so we make use of it only when present. |
| 262 |
|
| 263 |
# trick to avoid runtime warning from inside pip: _distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils. |
| 264 |
with warnings.catch_warnings(record=True): |
| 265 |
warnings.simplefilter("always") |
| 266 |
# pylint: disable=import-outside-toplevel |
| 267 |
from pip._internal.metadata import get_default_environment |
| 268 |
from pip._internal.req.req_uninstall import uninstallation_paths |
| 269 |
|
| 270 |
dist = get_default_environment().get_distribution(package_name) |
| 271 |
if dist: |
| 272 |
_logger.debug("Found %s dist", dist) |
| 273 |
for _ in uninstallation_paths(dist): |
| 274 |
use_pip = True |
| 275 |
else: |
| 276 |
_logger.debug("Skipping %s as it is not installed.", package_name) |
| 277 |
use_pip = False |
| 278 |
except (AttributeError, ModuleNotFoundError) as exc: |
| 279 |
# On Fedora 36, we got a AttributeError exception from pip that we want to avoid |
| 280 |
# On NixOS, we got a ModuleNotFoundError exception from pip that we want to avoid |
| 281 |
_logger.debug(exc) |
| 282 |
use_pip = False |
| 283 |
|
| 284 |
# We only want to recommend pip for upgrade if it looks safe to do so. |
| 285 |
return pip if use_pip else "" |
| 286 |
|
| 287 |
|
| 288 |
def get_deps_versions() -> dict[str, Version | None]: |
| 289 |
"""Return versions of most important dependencies.""" |
| 290 |
result: dict[str, Version | None] = {} |
| 291 |
|
| 292 |
for name in ["ansible-core", "ansible-compat", "ruamel-yaml", "ruamel-yaml-clib"]: |
| 293 |
try: |
| 294 |
result[name] = Version(version(name)) |
| 295 |
except PackageNotFoundError: |
| 296 |
result[name] = None |
| 297 |
return result |
| 298 |
|
| 299 |
|
| 300 |
def get_version_warning() -> str: |
| 301 |
"""Display warning if current version is outdated.""" |
| 302 |
# 0.1dev1 is special fallback version |
| 303 |
if __version__ == "0.1.dev1": # pragma: no cover |
| 304 |
return "" |
| 305 |
pip = guess_install_method() |
| 306 |
# If we do not know how to upgrade, we do not want to show any warnings |
| 307 |
# about version. |
| 308 |
if not pip: |
| 309 |
return "" |
| 310 |
|
| 311 |
msg = "" |
| 312 |
data = {} |
| 313 |
current_version = Version(__version__) |
| 314 |
|
| 315 |
if not os.path.exists(CACHE_DIR): # pragma: no cover |
| 316 |
os.makedirs(CACHE_DIR) |
| 317 |
cache_file = f"{CACHE_DIR}/latest.json" |
| 318 |
refresh = True |
| 319 |
if os.path.exists(cache_file): |
| 320 |
age = time.time() - os.path.getmtime(cache_file) |
| 321 |
if age < 24 * 60 * 60: |
| 322 |
refresh = False |
| 323 |
with open(cache_file, encoding="utf-8") as f: |
| 324 |
data = json.load(f) |
| 325 |
|
| 326 |
if not options.offline and (refresh or not data): |
| 327 |
release_url = ( |
| 328 |
"https://api.github.com/repos/ansible/ansible-lint/releases/latest" |
| 329 |
) |
| 330 |
if not release_url.startswith( |
| 331 |
"https://" |
| 332 |
): # pragma: no cover (ruff compatibility) |
| 333 |
msg = "release_url must start with https://" |
| 334 |
raise ValueError(msg) |
| 335 |
try: |
| 336 |
with urllib.request.urlopen(release_url) as url: |
| 337 |
data = json.load(url) |
| 338 |
with open(cache_file, "w", encoding="utf-8") as f: |
| 339 |
json.dump(data, f) |
| 340 |
except (URLError, HTTPError, HTTPException) as exc: # pragma: no cover |
| 341 |
_logger.debug( |
| 342 |
"Unable to fetch latest version from %s due to: %s", |
| 343 |
release_url, |
| 344 |
exc, |
| 345 |
) |
| 346 |
return "" |
| 347 |
|
| 348 |
if data: |
| 349 |
html_url = data["html_url"] |
| 350 |
new_version = Version(data["tag_name"][1:]) # removing v prefix from tag |
| 351 |
|
| 352 |
if current_version > new_version: |
| 353 |
msg = "[dim]You are using a pre-release version of ansible-lint.[/]" |
| 354 |
elif current_version < new_version: |
| 355 |
msg = f"""[warning]A new release of ansible-lint is available: [warning]{current_version}[/] → [success][link={html_url}]{new_version}[/link][/][/]""" |
| 356 |
msg += f" Upgrade by running: [info]{pip}[/]" |
| 357 |
|
| 358 |
return msg |