Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do inventory parsing non-multithreaded #4447

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 12 additions & 8 deletions src/ansiblelint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def _run(self) -> list[MatchError]:
def worker(lintable: Lintable) -> list[MatchError]:
return self._get_ansible_syntax_check_matches(
lintable=lintable,
inventory_opts=inventory_opts,
app=self.app,
)

Expand All @@ -257,6 +258,15 @@ def worker(lintable: Lintable) -> list[MatchError]:
continue
files.append(lintable)

inventory_opts = [
inventory_opt
for inventory_opts in [
("-i", inventory_file)
for inventory_file in self._get_inventory_files(self.app)
]
for inventory_opt in inventory_opts
]

# avoid resource leak warning, https://github.com/python/cpython/issues/90549
# pylint: disable=unused-variable
global_resource = multiprocessing.Semaphore() # noqa: F841
Expand Down Expand Up @@ -304,6 +314,7 @@ def worker(lintable: Lintable) -> list[MatchError]:
def _get_ansible_syntax_check_matches(
self,
lintable: Lintable,
inventory_opts: list[str],
app: App,
) -> list[MatchError]:
"""Run ansible syntax check and return a list of MatchError(s)."""
Expand Down Expand Up @@ -346,14 +357,7 @@ def _get_ansible_syntax_check_matches(
playbook_path = str(lintable.path.expanduser())
cmd = [
"ansible-playbook",
*[
inventory_opt
for inventory_opts in [
("-i", inventory_file)
for inventory_file in self._get_inventory_files(app)
]
for inventory_opt in inventory_opts
],
*inventory_opts,
"--syntax-check",
playbook_path,
]
Expand Down
23 changes: 23 additions & 0 deletions test/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,26 @@ def test_with_inventory_via_ansible_cfg(tmp_path: Path) -> None:

result = run_ansible_lint(lintable.filename, cwd=tmp_path)
assert result.returncode == RC.SUCCESS


def test_with_inventory_concurrent_syntax_checks(tmp_path: Path) -> None:
"""Validate using inventory file with concurrent syntax checks aren't faulty."""
(tmp_path / "ansible.cfg").write_text("[defaults]\ninventory = foo\n")
(tmp_path / "foo").write_text("[group_name]\nhost1\nhost2\n")
lintable1 = Lintable(tmp_path / "playbook1.yml")
lintable2 = Lintable(tmp_path / "playbook2.yml")
lintable1.content = "---\n- name: Test\n hosts:\n - group_name\n serial: \"{{ batch | default(groups['group_name'] | length) }}\"\n"
lintable2.content = "---\n- name: Test\n hosts:\n - group_name\n serial: \"{{ batch | default(groups['group_name'] | length) }}\"\n"
lintable1.kind = "playbook"
lintable2.kind = "playbook"
lintable1.write(force=True)
lintable2.write(force=True)

counter = 0
while counter < 3:
result = run_ansible_lint(lintable1.filename, lintable2.filename, cwd=tmp_path)
assert result.returncode == RC.SUCCESS
# AttributeError err is expected to look like what's reported here,
# https://github.com/ansible/ansible-lint/issues/4446.
assert "AttributeError" not in result.stderr
counter += 1
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ setenv =
PRE_COMMIT_COLOR = always
# Number of expected test passes, safety measure for accidental skip of
# tests. Update value if you add/remove tests. (tox-extra)
PYTEST_REQPASS = 901
PYTEST_REQPASS = 902
FORCE_COLOR = 1
pre: PIP_PRE = 1
allowlist_externals =
Expand Down
Loading