From 335a9acb7f20c28266163ffc8d5d0ba077d7f8b6 Mon Sep 17 00:00:00 2001 From: pollenjp Date: Mon, 4 Dec 2023 03:34:03 +0900 Subject: [PATCH 1/8] Fix workflow name --- .github/workflows/release.yml | 6 +++--- .github/workflows/release_test.yml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 21685eb..8cb56fc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: Release Drafter +name: Release & PyPI Publish on: workflow_dispatch: push: @@ -55,8 +55,8 @@ jobs: with: name: release-dists path: dist/ - test-pypi-publish: # https://github.com/pypa/gh-action-pypi-publish - name: Upload release to TestPyPI + pypi-publish: # https://github.com/pypa/gh-action-pypi-publish + name: Upload release to PyPI runs-on: ubuntu-latest needs: - release-build diff --git a/.github/workflows/release_test.yml b/.github/workflows/release_test.yml index fd2e094..1933993 100644 --- a/.github/workflows/release_test.yml +++ b/.github/workflows/release_test.yml @@ -1,5 +1,5 @@ --- -name: Release Drafter (autolabeler & check release tag) +name: TestPyPI Publish on: workflow_dispatch: push: From c35558be0883688ddc616ff6623efe8b601005ca Mon Sep 17 00:00:00 2001 From: pollenjp Date: Mon, 4 Dec 2023 03:35:46 +0900 Subject: [PATCH 2/8] update version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 69db7bc..c3a8ea7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "ansible-lint-custom-strict-naming" -version = "0.1.5" +version = "0.1.6" description = "Add your description here" authors = [{ name = "pollenjp", email = "polleninjp@gmail.com" }] dependencies = ["ansible-lint>=6.21.1"] From 3c1cee4d1f6038ad61507ddcc013fd0596bd8688 Mon Sep 17 00:00:00 2001 From: pollenjp Date: Thu, 7 Dec 2023 15:26:24 +0900 Subject: [PATCH 3/8] Add an external article --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index b85b067..6371a32 100644 --- a/README.md +++ b/README.md @@ -68,3 +68,9 @@ Strict naming rule is useful to avoid name collision and to search defined posit - `some_role__const__app_config__name` - `some_role__const__app_config__token` - `some_role__const__app_config__version` + +## Docs + +### Articles + +- [ansible-lint のカスタムルールを利用して Ansible 内での変数命名規則を縛ってみた話](https://zenn.dev/pollenjp/articles/2023-12-03-ansible-lint-custom-strict-naming) From e01042a8ce4bb3d453f1df20f7656eaea3ee56a8 Mon Sep 17 00:00:00 2001 From: pollenjp Date: Sat, 9 Dec 2023 03:19:21 +0900 Subject: [PATCH 4/8] role__args, tasks__args support --- src/rules/var_name_prefix.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/rules/var_name_prefix.py b/src/rules/var_name_prefix.py index e692ef8..eff7f23 100644 --- a/src/rules/var_name_prefix.py +++ b/src/rules/var_name_prefix.py @@ -88,14 +88,25 @@ def match_task_for_include_role_module(self, task: Task, file: Lintable | None = # check vars prefix = f"{role_name}_role__arg__" + completely_matched_name = f"{role_name}_role__args" + def validate_key_name(key: str): + """keyが条件を満たすか""" + if is_registered_key(key): + return True + if key.startswith(f"{prefix}"): + return True + if key == completely_matched_name: + return True + return False + return [ self.create_matcherror( - message=f"Variables in 'include_role' should have a '{prefix}' prefix.", + message=f"Variable name in 'include_role' should have a '{prefix}' prefix or '{completely_matched_name}' as dict.", lineno=task_vars.get(LINE_NUMBER_KEY), filename=file, ) for key in task_vars.keys() - if not is_registered_key(key) and not key.startswith(f"{prefix}") + if not validate_key_name(key) ] def match_task_for_include_tasks_module(self, task: Task, file: Lintable | None = None) -> bool | list[MatchError]: @@ -107,13 +118,25 @@ def match_task_for_include_tasks_module(self, task: Task, file: Lintable | None return False # check vars + prefix = f"{role_name}_role__arg__" prefix = f"{role_name}_tasks__arg__" + completely_matched_name = f"{role_name}_tasks__args" + def validate_key_name(key: str): + """keyが条件を満たすか""" + if is_registered_key(key): + return True + if key.startswith(f"{prefix}"): + return True + if key == completely_matched_name: + return True + return False + return [ self.create_matcherror( - message=f"Variables in 'include_tasks' should have a '{prefix}' prefix.", + message=f"Variable name in 'include_tasks' should have a '{prefix}' prefix or '{completely_matched_name}' as dict.", lineno=task_vars.get(LINE_NUMBER_KEY), filename=file, ) for key in task_vars.keys() - if not is_registered_key(key) and not key.startswith(f"{prefix}") + if not validate_key_name(key) ] From 0bcc0a85aa514e6fcb9e6a9a920d5e8f49654e7d Mon Sep 17 00:00:00 2001 From: pollenjp Date: Sat, 9 Dec 2023 03:20:35 +0900 Subject: [PATCH 5/8] fix --- src/rules/var_name_prefix.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/rules/var_name_prefix.py b/src/rules/var_name_prefix.py index eff7f23..91de0af 100644 --- a/src/rules/var_name_prefix.py +++ b/src/rules/var_name_prefix.py @@ -118,7 +118,6 @@ def match_task_for_include_tasks_module(self, task: Task, file: Lintable | None return False # check vars - prefix = f"{role_name}_role__arg__" prefix = f"{role_name}_tasks__arg__" completely_matched_name = f"{role_name}_tasks__args" def validate_key_name(key: str): From 5247cfe9996b6c2229768624a496658159a144f4 Mon Sep 17 00:00:00 2001 From: pollenjp Date: Sat, 9 Dec 2023 03:22:53 +0900 Subject: [PATCH 6/8] update readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b85b067..e184dbe 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Strict naming rule is useful to avoid name collision and to search defined posit | `var__fizz` | defined in playbook by `ansible.builtin.set_fact` or `register` | | `some_role__var__fizz` | defined in `roles//tasks` by `ansible.builtin.set_fact` or `register` | | `some_role__arg__fizz` | defined by `ansible.builtin.include_role`'s `vars` key and shouldn't changed in `roles//tasks` | + | `some_role__args` | defined by `ansible.builtin.include_role`'s `vars` key and shouldn't changed in `roles//tasks` | | `some_role__const__fizz` | defined only in `roles//vars/`. | | `some_tasks__var__fizz` | defined in `tasks` by `ansible.builtin.set_fact` or `register` | | `some_tasks__const__fizz` | defined by `ansible.builtin.include_role`'s vars key and not changed in `tasks` | From 1aa58fec594184c8ef09e6f6e49ef01d798950c8 Mon Sep 17 00:00:00 2001 From: pollenjp Date: Sat, 9 Dec 2023 15:23:19 +0900 Subject: [PATCH 7/8] fix format --- src/rules/var_name_prefix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/var_name_prefix.py b/src/rules/var_name_prefix.py index 91de0af..0cfcb59 100644 --- a/src/rules/var_name_prefix.py +++ b/src/rules/var_name_prefix.py @@ -137,5 +137,5 @@ def validate_key_name(key: str): filename=file, ) for key in task_vars.keys() - if not validate_key_name(key) + if not validate_key_name(key) ] From 8a42df9f5695e587c3160f65c3f7ea6194669d35 Mon Sep 17 00:00:00 2001 From: pollenjp Date: Sat, 9 Dec 2023 15:38:20 +0900 Subject: [PATCH 8/8] update readme --- README.md | 89 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index e184dbe..111a86d 100644 --- a/README.md +++ b/README.md @@ -15,49 +15,60 @@ Strict naming rule is useful to avoid name collision and to search defined posit ## Rules -### var_name_prefix - -- [x] `_role__` , `_tasks__` - - - | prefix | Variables defined in | - | :-------------------- | :------------------------- | - | `_role__` | `roles//tasks/` | - | `_tasks__` | `/**/tasks/` | - - - In ansible-lint, `var-naming[no-role-prefix]` require to use `_` as prefix. But it is not enough to avoid name collision or search defined position. So, I add `_role__` or `_tasks__` to the prefix. - -- [ ] `var__`, `const__` - - | prefix | description | - | :-------- | :-------------------------------------------------------------------------------------- | - | `var__` | Variables dynamically defined by `ansible.builtin.set_fact` or `register` | - | `const__` | Variables statistically defined in such like inventory's vars, group_vars and host_vars | -- [ ] prefix precedence - - - descending order - - role or task prefix - - var or const prefix - - examples - - | var | description | - | :------------------------ | :-------------------------------------------------------------------------------------------------------- | - | `var__fizz` | defined in playbook by `ansible.builtin.set_fact` or `register` | - | `some_role__var__fizz` | defined in `roles//tasks` by `ansible.builtin.set_fact` or `register` | - | `some_role__arg__fizz` | defined by `ansible.builtin.include_role`'s `vars` key and shouldn't changed in `roles//tasks` | - | `some_role__args` | defined by `ansible.builtin.include_role`'s `vars` key and shouldn't changed in `roles//tasks` | - | `some_role__const__fizz` | defined only in `roles//vars/`. | - | `some_tasks__var__fizz` | defined in `tasks` by `ansible.builtin.set_fact` or `register` | - | `some_tasks__const__fizz` | defined by `ansible.builtin.include_role`'s vars key and not changed in `tasks` | +## var_name_prefix + +### `_role__` , `_tasks__` + +- | prefix | Variables defined in | + | :-------------------- | :------------------------- | + | `_role__` | `roles//tasks/` | + | `_tasks__` | `/**/tasks/` | + +- In ansible-lint, `var-naming[no-role-prefix]` require to use `_` as prefix. But it is not enough to avoid name collision or search defined position. So, I add `_role__` or `_tasks__` to the prefix. + +### `var__`, `const__` + +- `var__` prefix + - Variables dynamically defined by `ansible.builtin.set_fact` or `register` +- `const__` prefix + - Variables dynamically defined by `ansible.builtin.set_fact` or `register` + - Variables statically defined in such like inventory's vars, group_vars, host_vars and etc. + +### Vars in `tasks/.yml` or `roles//tasks/main.yml` + +- `_role__var__` prefix + - These variables are dynamically defined in `roles//tasks/main.yml`. +- `_role__const__` prefix + - These variables are defined in `roles//vars/main.yml` and shouldn't be changed dynamically. +- `some_role__arg__` prefix + - These variables are defined by `ansible.builtin.include_role`'s `vars` key and shouldn't be changed dynamically. +- `some_role__args` + + - These variables are defined by `ansible.builtin.include_role`'s `vars` key and shouldn't be changed dynamically. + - This is useful when you want to send vars as dict. ```yaml - tasks: - - name: Some task - ansible.builtin.include_role: - name: - vars: - some_role__const__one: value1 - some_role__const__two: value2 + - name: Sample + ansible.builtin.include_role: + name: some_role + vars: + some_role__args: + key1: value1 + key2: value2 ``` +### examples + +```yaml +tasks: + - name: Some task + ansible.builtin.include_role: + name: + vars: + some_role__const__one: value1 + some_role__const__two: value2 +``` + ## Others ### Double underscores?