Skip to content

Commit

Permalink
Do inventory parsing non-multithreaded (#4447)
Browse files Browse the repository at this point in the history
Co-authored-by: Ajinkya Udgirkar <ajinkyaudgirkar@gmail.com>
  • Loading branch information
cavcrosby and audgirka authored Dec 11, 2024
1 parent c6d9660 commit 4d295e8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
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 = 905
PYTEST_REQPASS = 906
FORCE_COLOR = 1
pre: PIP_PRE = 1
allowlist_externals =
Expand Down

0 comments on commit 4d295e8

Please sign in to comment.