Skip to content

Commit 50bdeca

Browse files
authored
var-naming now prevents use of Ansible reserved names (#3460)
1 parent 02804cd commit 50bdeca

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

examples/playbooks/vars/rule_var_naming_fail.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ this_is_valid: # valid because content is a dict, not a variable
66
ALL_CAPS_ARE_BAD_TOO: ... # invalid
77
"{{ 'test_' }}var": "value" # noqa: schema
88
CamelCaseButErrorIgnored: true # noqa: var-naming
9-
assert: true # invalid due to reserved keyword
9+
assert: true # invalid due to being Python reserved keyword
1010
é: true # invalid due to non-ascii character
11+
hosts: true # invalid as being Ansible reserved name

src/ansiblelint/rules/var_naming.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Possible errors messages:
1818
- `var-naming[pattern]`: Variables names should match ... regex.
1919
- `var-naming[no-role-prefix]`: Variables names from within roles should use
2020
`role_name_` as a prefix.
21+
- `var-naming[no-reserved]`: Variables names must not be Ansible reserved names.
2122

2223
## Settings
2324

@@ -38,6 +39,7 @@ var_naming_pattern: "^[a-z_][a-z0-9_]*$"
3839
CamelCase: true # <- Contains a mix of lowercase and uppercase characters.
3940
ALL_CAPS: bar # <- Contains only uppercase characters.
4041
v@r!able: baz # <- Contains special characters.
42+
hosts: [] # <- hosts is an Ansible reserved name
4143
```
4244
4345
## Correct Code
@@ -50,6 +52,7 @@ var_naming_pattern: "^[a-z_][a-z0-9_]*$"
5052
lowercase: true # <- Contains only lowercase characters.
5153
no_caps: bar # <- Does not contains uppercase characters.
5254
variable: baz # <- Does not contain special characters.
55+
my_hosts: [] # <- Does not use a reserved names.
5356
```
5457
5558
[cop]: https://redhat-cop.github.io/automation-good-practices/#_naming_things

src/ansiblelint/rules/var_naming.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import TYPE_CHECKING, Any
88

99
from ansible.parsing.yaml.objects import AnsibleUnicode
10+
from ansible.vars.reserved import get_reserved_names
1011

1112
from ansiblelint.config import options
1213
from ansiblelint.constants import LINE_NUMBER_KEY, RC
@@ -31,8 +32,9 @@ class VariableNamingRule(AnsibleLintRule):
3132
needs_raw_task = True
3233
re_pattern_str = options.var_naming_pattern or "^[a-z_][a-z0-9_]*$"
3334
re_pattern = re.compile(re_pattern_str)
35+
reserved_names = get_reserved_names()
3436

35-
# pylint: disable=too-many-return-statements)
37+
# pylint: disable=too-many-return-statements
3638
def get_var_naming_matcherror(
3739
self,
3840
ident: str,
@@ -52,14 +54,21 @@ def get_var_naming_matcherror(
5254
except UnicodeEncodeError:
5355
return MatchError(
5456
tag="var-naming[non-ascii]",
55-
message="Variables names must be ASCII.",
57+
message=f"Variables names must be ASCII. ({ident})",
5658
rule=self,
5759
)
5860

5961
if keyword.iskeyword(ident):
6062
return MatchError(
6163
tag="var-naming[no-keyword]",
62-
message="Variables names must not be Python keywords.",
64+
message=f"Variables names must not be Python keywords. ({ident})",
65+
rule=self,
66+
)
67+
68+
if ident in self.reserved_names:
69+
return MatchError(
70+
tag="var-naming[no-reserved]",
71+
message=f"Variables names must not be Ansible reserved names. ({ident})",
6372
rule=self,
6473
)
6574

@@ -74,7 +83,7 @@ def get_var_naming_matcherror(
7483
if not bool(self.re_pattern.match(ident)):
7584
return MatchError(
7685
tag="var-naming[pattern]",
77-
message=f"Variables names should match {self.re_pattern_str} regex.",
86+
message=f"Variables names should match {self.re_pattern_str} regex. ({ident})",
7887
rule=self,
7988
)
8089

@@ -140,7 +149,6 @@ def matchtask(
140149
results.append(match_error)
141150

142151
# If the task uses the 'set_fact' module
143-
# breakpoint()
144152
ansible_module = task["action"]["__ansible_module__"]
145153
if ansible_module == "set_fact":
146154
for key in filter(
@@ -239,6 +247,7 @@ def test_invalid_var_name_varsfile(
239247
("var-naming[no-jinja]", 7),
240248
("var-naming[no-keyword]", 9),
241249
("var-naming[non-ascii]", 10),
250+
("var-naming[no-reserved]", 11),
242251
)
243252
assert len(results) == len(expected_errors)
244253
for idx, result in enumerate(results):

src/ansiblelint/runner.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ def run(self) -> list[MatchError]:
147147
)
148148
else:
149149
filename = warn.source
150-
# breakpoint()
151150
match = MatchError(
152151
message=warn.message
153152
if isinstance(warn.message, str)

0 commit comments

Comments
 (0)