From a02e008e5eebf0e32856108d0b73657b0498a37b Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Fri, 29 Sep 2023 14:11:02 +0100 Subject: [PATCH] Accept underline prefix when checking var-naming (#3789) --- examples/playbooks/role_vars_prefix_detection.yml | 4 +++- .../roles/var_naming_pattern/tasks/include_task_with_vars.yml | 4 +++- src/ansiblelint/rules/var_naming.md | 2 +- src/ansiblelint/rules/var_naming.py | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/playbooks/role_vars_prefix_detection.yml b/examples/playbooks/role_vars_prefix_detection.yml index 823f6641f0..fee163faf1 100644 --- a/examples/playbooks/role_vars_prefix_detection.yml +++ b/examples/playbooks/role_vars_prefix_detection.yml @@ -48,4 +48,6 @@ ansible.builtin.include_role: name: role_vars_prefix_detection vars: - role_vars_prefix_detection_var2: val2 + role_vars_prefix_detection_var1: val1 + _role_vars_prefix_detection_var2: val2 + __role_vars_prefix_detection_var3: val3 diff --git a/examples/roles/var_naming_pattern/tasks/include_task_with_vars.yml b/examples/roles/var_naming_pattern/tasks/include_task_with_vars.yml index 5151cd3142..49822279c9 100644 --- a/examples/roles/var_naming_pattern/tasks/include_task_with_vars.yml +++ b/examples/roles/var_naming_pattern/tasks/include_task_with_vars.yml @@ -2,7 +2,9 @@ - name: include_task_with_vars | Foo ansible.builtin.include_tasks: ../tasks/included-task-with-vars.yml vars: - var_naming_pattern_foo: bar + var_naming_pattern_1: bar + _var_naming_pattern_2: ... # we allow _ before the prefix + __var_naming_pattern_3: ... # we allow __ before the prefix - name: include_task_with_vars | Foo ansible.builtin.include_role: diff --git a/src/ansiblelint/rules/var_naming.md b/src/ansiblelint/rules/var_naming.md index 3386a0cbc2..e4034f02f6 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. + `role_name_` 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. diff --git a/src/ansiblelint/rules/var_naming.py b/src/ansiblelint/rules/var_naming.py index ac0f314ed2..a73394b5ef 100644 --- a/src/ansiblelint/rules/var_naming.py +++ b/src/ansiblelint/rules/var_naming.py @@ -168,7 +168,7 @@ def get_var_naming_matcherror( if ( prefix - and not ident.startswith(f"{prefix}_") + and not ident.lstrip("_").startswith(f"{prefix}_") and not has_jinja(prefix) and is_fqcn_or_name(prefix) ):