From 946373e79c8c97fb4700ff29297d9ba73e95dbbe Mon Sep 17 00:00:00 2001 From: NocDerEchte <147306991+NocDerEchte@users.noreply.github.com> Date: Wed, 24 Sep 2025 23:18:42 +0200 Subject: [PATCH 1/2] feat: Add role_var_separator option --- src/ansiblelint/config.py | 1 + src/ansiblelint/rules/var_naming.py | 7 +++++-- src/ansiblelint/schemas/ansible-lint-config.json | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/ansiblelint/config.py b/src/ansiblelint/config.py index 559808450d..ee52ea7764 100644 --- a/src/ansiblelint/config.py +++ b/src/ansiblelint/config.py @@ -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 diff --git a/src/ansiblelint/rules/var_naming.py b/src/ansiblelint/rules/var_naming.py index e5d265abc2..4b6192cf52 100644 --- a/src/ansiblelint/rules/var_naming.py +++ b/src/ansiblelint/rules/var_naming.py @@ -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 @@ -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, ) diff --git a/src/ansiblelint/schemas/ansible-lint-config.json b/src/ansiblelint/schemas/ansible-lint-config.json index f205a00f78..2fc2b8b5f0 100644 --- a/src/ansiblelint/schemas/ansible-lint-config.json +++ b/src/ansiblelint/schemas/ansible-lint-config.json @@ -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" From c3fcd371c0fa7bb07ae874b838b56f9e4c9034e4 Mon Sep 17 00:00:00 2001 From: NocDerEchte <147306991+NocDerEchte@users.noreply.github.com> Date: Wed, 24 Sep 2025 23:19:05 +0200 Subject: [PATCH 2/2] doc: Add documentation for role_var_sparator option --- .ansible-lint | 5 +++++ src/ansiblelint/rules/var_naming.md | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.ansible-lint b/.ansible-lint index 099fe016c3..145f474296 100644 --- a/.ansible-lint +++ b/.ansible-lint @@ -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: diff --git a/src/ansiblelint/rules/var_naming.md b/src/ansiblelint/rules/var_naming.md index a9a4b1182e..e0c970b00c 100644 --- a/src/ansiblelint/rules/var_naming.md +++ b/src/ansiblelint/rules/var_naming.md @@ -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. @@ -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