Skip to content

Commit 1d04113

Browse files
authored
Skip lookups for ansible-core>=2.19 (#4688)
1 parent e8bf1a0 commit 1d04113

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

examples/playbooks/test_lookup.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
- name: Test lookup playbook for template() function
3+
hosts: localhost
4+
gather_facts: false
5+
vars:
6+
# File lookup example
7+
config_content: "{{ lookup('env', 'HOME') }}"
8+
9+
# Environment variable lookup
10+
current_user: "{{ lookup('env', 'USER') }}"
11+
12+
# Lookup with default value
13+
optional_port: "{{ lookup('env', 'TEST_PORT', default='8080') }}"
14+
15+
tasks:
16+
- name: Display environment variable via lookup
17+
ansible.builtin.debug:
18+
msg: "Current user: {{ current_user }}"
19+
20+
- name: Use lookup in task
21+
ansible.builtin.set_fact:
22+
app_config:
23+
port: "{{ lookup('env', 'APP_PORT', default='3000') }}"
24+
home: "{{ lookup('env', 'HOME') }}"
25+
26+
- name: Conditional task with lookup
27+
ansible.builtin.debug:
28+
msg: "Debug mode enabled"
29+
when: lookup('env', 'DEBUG', default='false') | bool

src/ansiblelint/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
)
6464
from ansible.template import Templar
6565
from ansible.utils.collection_loader import AnsibleCollectionConfig
66+
from packaging.version import Version
6667
from yaml.composer import Composer
6768
from yaml.parser import ParserError
6869
from yaml.representer import RepresenterError
@@ -73,7 +74,7 @@
7374
RuntimeErrorRule,
7475
)
7576
from ansiblelint.app import App, get_app
76-
from ansiblelint.config import Options, options
77+
from ansiblelint.config import Options, get_deps_versions, options
7778
from ansiblelint.constants import (
7879
ANNOTATION_KEYS,
7980
FILENAME_KEY,
@@ -201,6 +202,13 @@ def ansible_template(
201202
re_valid_filter = re.compile(r"^\w+(\.\w+\.\w+)?$")
202203
templar = ansible_templar(basedir=basedir, templatevars=templatevars)
203204

205+
# Skip lookups for ansible-core >= 2.19; use disable_lookups for older versions
206+
if "lookup" in varname:
207+
deps = get_deps_versions()
208+
if deps["ansible-core"] and deps["ansible-core"] >= Version("2.19"):
209+
return varname
210+
kwargs["disable_lookups"] = True
211+
204212
for _i in range(10):
205213
try:
206214
if TrustedAsTemplate and not isinstance(varname, TrustedAsTemplate):

test/test_utils.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
import pytest
3131
from ansible.utils.sentinel import Sentinel
3232
from ansible_compat.runtime import Runtime
33+
from packaging.version import Version
3334

3435
from ansiblelint import cli, constants, utils
3536
from ansiblelint.__main__ import initialize_logger
3637
from ansiblelint.cli import get_rules_dirs
38+
from ansiblelint.config import get_deps_versions
3739
from ansiblelint.constants import RC
3840
from ansiblelint.file_utils import Lintable, cwd
3941
from ansiblelint.runner import Runner
@@ -279,6 +281,47 @@ def test_template(template: str, output: str) -> None:
279281
assert result == output
280282

281283

284+
@pytest.mark.parametrize(
285+
("template", "has_lookup"),
286+
(
287+
pytest.param(
288+
"{{ lookup('file', '/etc/hostname') }}",
289+
True,
290+
id="file_lookup",
291+
),
292+
pytest.param(
293+
"Welcome {{ lookup('env', 'USER', default='user') }}!",
294+
True,
295+
id="lookup_with_text",
296+
),
297+
),
298+
)
299+
def test_template_lookup_behavior(template: str, has_lookup: bool) -> None:
300+
"""Test template behavior for both ansible-core >= 2.19 and < 2.19."""
301+
result = utils.template(
302+
basedir=Path("/base/dir"),
303+
value=template,
304+
variables={"some_var": "test_value"},
305+
fail_on_error=False,
306+
)
307+
308+
# Get ansible-core version to determine expected behavior
309+
deps = get_deps_versions()
310+
ansible_version = deps.get("ansible-core")
311+
is_new_ansible = ansible_version and ansible_version >= Version("2.19")
312+
313+
if has_lookup and is_new_ansible:
314+
# For ansible-core >= 2.19: lookups should be skipped (returned unchanged)
315+
assert result == template, (
316+
f"Expected lookup to be skipped for ansible-core >= 2.19, but got: {result}"
317+
)
318+
elif not has_lookup:
319+
# Normal templates should always be processed
320+
assert result != template, (
321+
f"Expected normal template to be processed, but got unchanged: {result}"
322+
)
323+
324+
282325
def test_task_to_str_unicode() -> None:
283326
"""Ensure that extracting messages from tasks preserves Unicode."""
284327
task = utils.Task({"fail": {"msg": "unicode é ô à"}}, filename="filename.yml")

0 commit comments

Comments
 (0)