diff --git a/src/ansiblelint/__main__.py b/src/ansiblelint/__main__.py index 916ecdde4f..bf4c33e72c 100755 --- a/src/ansiblelint/__main__.py +++ b/src/ansiblelint/__main__.py @@ -129,6 +129,11 @@ def initialize_options(arguments: list[str] | None = None) -> BaseFileLock | Non for k, v in new_options.__dict__.items(): setattr(options, k, v) + if options.nodeps is None or options.nodeps is False: + options.nodeps = bool(int(os.environ.get("ANSIBLE_LINT_NODEPS", "0"))) + if options.nodeps: + options.offline = True + # rename deprecated ids/tags to newer names options.tags = [normalize_tag(tag) for tag in options.tags] options.skip_list = [normalize_tag(tag) for tag in options.skip_list] diff --git a/src/ansiblelint/cli.py b/src/ansiblelint/cli.py index 41a0fdf9b2..d0a693bcd6 100644 --- a/src/ansiblelint/cli.py +++ b/src/ansiblelint/cli.py @@ -472,6 +472,14 @@ def get_cli_parser() -> argparse.ArgumentParser: const=True, help="Disable installation of requirements.yml and schema refreshing", ) + parser.add_argument( + "--nodeps", + dest="nodeps", + action="store_const", + const=True, + default=None, + help="Disable external dependency checking", + ) parser.add_argument( "--version", action="store_true", @@ -495,6 +503,7 @@ def merge_config(file_config: dict[Any, Any], cli_config: Options) -> Options: "strict", "use_default_rules", "offline", + "nodeps", ) # maps lists to their default config values lists_map = { diff --git a/src/ansiblelint/config.py b/src/ansiblelint/config.py index 63e040f45c..1ff8baba6b 100644 --- a/src/ansiblelint/config.py +++ b/src/ansiblelint/config.py @@ -155,6 +155,7 @@ class Options: # pylint: disable=too-many-instance-attributes only_builtins_allow_modules: list[str] = field(default_factory=list) var_naming_pattern: str | None = None offline: bool = False + nodeps: bool | None = None project_dir: str = "." # default should be valid folder (do not use None here) extra_vars: dict[str, Any] | None = None enable_list: list[str] = field(default_factory=list) @@ -180,16 +181,8 @@ class Options: # pylint: disable=too-many-instance-attributes _default_supported = ["2.15.", "2.16.", "2.17."] supported_ansible_also: list[str] = field(default_factory=list) - @property - def nodeps(self) -> bool: - """Returns value of nodeps feature.""" - # We do not want this to be cached as it would affect our testings. - return bool(int(os.environ.get("ANSIBLE_LINT_NODEPS", "0"))) - def __post_init__(self) -> None: """Extra initialization logic.""" - if self.nodeps: - self.offline = True @property def supported_ansible(self) -> list[str]: diff --git a/src/ansiblelint/schemas/ansible-lint-config.json b/src/ansiblelint/schemas/ansible-lint-config.json index ae26bf0153..829af00930 100644 --- a/src/ansiblelint/schemas/ansible-lint-config.json +++ b/src/ansiblelint/schemas/ansible-lint-config.json @@ -87,6 +87,11 @@ "title": "Offline", "type": "boolean" }, + "nodeps": { + "default": null, + "title": "Nodeps", + "type": ["boolean", "null"] + }, "only_builtins_allow_collections": { "items": { "type": "string" diff --git a/test/test_main.py b/test/test_main.py index 57f4700d1f..ef345baed2 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -122,8 +122,16 @@ def test_get_version_warning_offline(mocker: MockerFixture) -> None: def test_nodeps(lintable: str) -> None: """Asserts ability to be called w/ or w/o venv activation.""" env = os.environ.copy() - env["ANSIBLE_LINT_NODEPS"] = "1" py_path = Path(sys.executable).parent + proc = subprocess.run( + [str(py_path / "ansible-lint"), "--nodeps", lintable], + check=False, + capture_output=True, + text=True, + env=env, + ) + assert proc.returncode == 0, proc + env["ANSIBLE_LINT_NODEPS"] = "1" proc = subprocess.run( [str(py_path / "ansible-lint"), lintable], check=False, diff --git a/test/test_transformer.py b/test/test_transformer.py index 6ce2e0ca58..1e94753f93 100644 --- a/test/test_transformer.py +++ b/test/test_transformer.py @@ -200,7 +200,7 @@ def fixture_runner_result( pytest.param( "examples/playbooks/4114/transform-with-missing-role-and-modules.yml", 1, - True, + False, True, id="4114", ),