Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ loop_var_prefix: "^(__|{role}_)"
# to skip_list.
var_naming_pattern: "^[a-z_][a-z0-9_]*$"

# Use an underscore as separator between the role and variable name.
# Default: "_"
# Example with role_var_separator="__": role__some_var
role_var_separator: "_"

use_default_rules: true
# Load custom rules from this specific folder
# rulesdir:
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class Options: # pylint: disable=too-many-instance-attributes
only_builtins_allow_collections: list[str] = field(default_factory=list)
only_builtins_allow_modules: list[str] = field(default_factory=list)
var_naming_pattern: str | None = None
role_var_separator: str | None = None
offline: bool = False
project_dir: str = "." # default should be valid folder (do not use None here)
extra_vars: dict[str, Any] | None = None
Expand Down
4 changes: 3 additions & 1 deletion src/ansiblelint/rules/var_naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Possible errors messages:
- `var-naming[no-jinja]`: Variables names must not contain jinja2 templating.
- `var-naming[pattern]`: Variables names should match ... regex.
- `var-naming[no-role-prefix]`: Variables names from within roles should use
`role_name_` as a prefix. Underlines are accepted before the prefix.
`role_name{role_var_separator}` as a prefix. Underlines are accepted before the prefix.
- `var-naming[no-reserved]`: Variables names must not be Ansible reserved names.
- `var-naming[read-only]`: This special variable is read-only.

Expand All @@ -43,6 +43,8 @@ This rule behavior can be changed by altering the below settings:
```yaml
# .ansible-lint
var_naming_pattern: "^[a-z_][a-z0-9_]*$"

role_var_separator: "_"
```

## Problematic Code
Expand Down
7 changes: 5 additions & 2 deletions src/ansiblelint/rules/var_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class VariableNamingRule(AnsibleLintRule):
needs_raw_task = True
re_pattern_str = options.var_naming_pattern or "^[a-z_][a-z0-9_]*$"
re_pattern = re.compile(re_pattern_str)
role_var_separator = options.role_var_separator or "_"
reserved_names = get_reserved_names()
# List of special variables that should be treated as read-only. This list
# does not include connection variables, which we expect users to tune in
Expand Down Expand Up @@ -178,13 +179,15 @@ def get_var_naming_matcherror(

if (
prefix
and not ident.lstrip("_").startswith(f"{prefix.value}_")
and not ident.lstrip("_").startswith(
f"{prefix.value}{self.role_var_separator}"
)
and not has_jinja(prefix.value)
and is_fqcn_or_name(prefix.value)
):
return self.create_matcherror(
tag="var-naming[no-role-prefix]",
message=f"Variables names from within roles should use {prefix.value}_ as a prefix.",
message=f"Variables names from within roles should use {prefix.value}{self.role_var_separator} as a prefix.",
filename=file,
data=ident,
)
Expand Down
6 changes: 6 additions & 0 deletions src/ansiblelint/schemas/ansible-lint-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@
"title": "Quiet",
"type": "boolean"
},
"role_var_separator": {
"default": "_",
"title": "Role Variable Namespace Separator",
"description": "Defines the separator used to distinguish between role namespace and variable name in Ansible role variables.",
"type": "string"
},
"rules": {
"additionalProperties": {
"$ref": "#/$defs/rule"
Expand Down
Loading