Skip to content

Commit b0b1e69

Browse files
authored
Avoid stacktrace with broken ansible.cfg files (#3916)
1 parent 58e4ba5 commit b0b1e69

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

.github/workflows/tox.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ jobs:
7070
env:
7171
# Number of expected test passes, safety measure for accidental skip of
7272
# tests. Update value if you add/remove tests.
73-
PYTEST_REQPASS: 847
73+
PYTEST_REQPASS: 848
7474
steps:
7575
- uses: actions/checkout@v4
7676
with:

src/ansiblelint/__main__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@
3636
from filelock import FileLock, Timeout
3737
from rich.markup import escape
3838

39+
from ansiblelint.constants import RC, SKIP_SCHEMA_UPDATE
40+
41+
# safety check for broken ansible core, needs to happen first
42+
try:
43+
# pylint: disable=unused-import
44+
from ansible.parsing.dataloader import DataLoader # noqa: F401
45+
46+
except Exception as _exc: # pylint: disable=broad-exception-caught # noqa: BLE001
47+
logging.fatal(_exc)
48+
sys.exit(RC.INVALID_CONFIG)
49+
# pylint: disable=ungrouped-imports
3950
from ansiblelint import cli
4051
from ansiblelint._mockings import _perform_mockings_cleanup
4152
from ansiblelint.app import get_app
@@ -53,7 +64,6 @@
5364
log_entries,
5465
options,
5566
)
56-
from ansiblelint.constants import RC, SKIP_SCHEMA_UPDATE
5767
from ansiblelint.loaders import load_ignore_txt
5868
from ansiblelint.runner import get_matches
5969
from ansiblelint.skip_utils import normalize_tag

src/ansiblelint/schemas/__store__.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/molecule.json"
3737
},
3838
"playbook": {
39-
"etag": "8ae42e48318ff6da41f6d383dc19b643221258ea6521886f834e31cb8d3a7322",
39+
"etag": "b86e7f78281e33eb16de9c5c066da0f88798243b647cc195573441d92e1e78a5",
4040
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/playbook.json"
4141
},
4242
"requirements": {
@@ -52,7 +52,7 @@
5252
"url": "https://raw.githubusercontent.com/ansible/ansible-rulebook/main/ansible_rulebook/schema/ruleset_schema.json"
5353
},
5454
"tasks": {
55-
"etag": "f9fbc0855680d1321fa3902181131d73838d922362d8dfb85a4f59402240cc07",
55+
"etag": "495ec0c6cb49d60feed7e2bc7c0ef0135bab473a28c3c8d4a7be4f210e8c53fc",
5656
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/tasks.json"
5757
},
5858
"vars": {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[defaults]
2+
3+
# This breaks ansible-core, because it loads the value as null and only
4+
# integers are accepted.
5+
gather_timeout=

test/test_main.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pytest_mock import MockerFixture
1111

1212
from ansiblelint.config import get_version_warning
13+
from ansiblelint.constants import RC
1314

1415

1516
@pytest.mark.parametrize(
@@ -104,3 +105,20 @@ def test_nodeps(lintable: str) -> None:
104105
env=env,
105106
)
106107
assert proc.returncode == 0, proc
108+
109+
110+
def test_broken_ansible_cfg() -> None:
111+
"""Asserts behavior when encountering broken ansible.cfg files."""
112+
py_path = Path(sys.executable).parent
113+
proc = subprocess.run(
114+
[str(py_path / "ansible-lint"), "--version"],
115+
check=False,
116+
capture_output=True,
117+
text=True,
118+
cwd="test/fixtures/broken-ansible.cfg",
119+
)
120+
assert proc.returncode == RC.INVALID_CONFIG, proc
121+
assert (
122+
"Invalid type for configuration option setting: DEFAULT_GATHER_TIMEOUT"
123+
in proc.stderr
124+
)

0 commit comments

Comments
 (0)