From 44b771895fcf6c491d466931582765ea93a9b0ef Mon Sep 17 00:00:00 2001 From: Ajinkya Udgirkar Date: Mon, 30 Jan 2023 14:34:07 +0530 Subject: [PATCH] Updated run-once and related example --- examples/playbooks/run-once-fail.yml | 4 +++- src/ansiblelint/rules/run_once.md | 16 +++++++++------- src/ansiblelint/rules/run_once.py | 8 ++++++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/examples/playbooks/run-once-fail.yml b/examples/playbooks/run-once-fail.yml index cb7621812d..f24d52cb4e 100644 --- a/examples/playbooks/run-once-fail.yml +++ b/examples/playbooks/run-once-fail.yml @@ -1,9 +1,11 @@ --- - name: "Example with run_once" hosts: all - strategy: free # <-- avoid use of strategy as free + # strategy: free # noqa: run-once[play] (Corrected code example) + strategy: free gather_facts: false tasks: + # - name: Task with run_once # noqa run-once[task] (Corrected code example) - name: Task with run_once ansible.builtin.debug: msg: "Test" diff --git a/src/ansiblelint/rules/run_once.md b/src/ansiblelint/rules/run_once.md index 6b14703f89..b7df02c499 100644 --- a/src/ansiblelint/rules/run_once.md +++ b/src/ansiblelint/rules/run_once.md @@ -1,12 +1,13 @@ # run-once -This rule warns against the use of `run_once` when `strategy` is set to `free`. +This rule warns against the use of `run_once` when the `strategy` is set to +`free`. This rule can produce the following messages: -- `run_once[play]`: Play uses `strategy: free`. -- `run_once[task]`: Using `run_once` may behave differently if `strategy` is set - to `free`. +- `run-once[play]`: Play uses `strategy: free`. +- `run-once[task]`: Using `run_once` may behave differently if the `strategy` is + set to `free`. For more information see the following topics in Ansible documentation: @@ -16,9 +17,9 @@ For more information see the following topics in Ansible documentation: !!! warning - This rule will always trigger regardless of the value configured inside 'strategy' field. That is because the effective value used at runtime can be different than the value inside the file. For example, ansible command line arguments can alter it. + This rule will always trigger regardless of the value configured inside the 'strategy' field. That is because the effective value used at runtime can be different than the value inside the file. For example, ansible command line arguments can alter it. -It is perfectly fine to add `# noqa: run_once[task]` to mark the warning as +It is perfectly fine to add `# noqa: run-once[task]` to mark the warning as acknowledged and ignored. ## Problematic Code @@ -52,9 +53,10 @@ acknowledged and ignored. - name: "Example of using run_once with strategy other than free" hosts: all strategy: linear + # strategy: free # noqa: run-once[play] (if using strategy: free can skip it this way) gather_facts: false tasks: # <-- use noqa to disable rule violations for specific tasks - - name: Task with run_once # noqa: run_once[task] + - name: Task with run_once # noqa: run-once[task] ansible.builtin.debug: msg: "Test" run_once: true diff --git a/src/ansiblelint/rules/run_once.py b/src/ansiblelint/rules/run_once.py index ea49654bee..2503803353 100644 --- a/src/ansiblelint/rules/run_once.py +++ b/src/ansiblelint/rules/run_once.py @@ -4,6 +4,7 @@ import sys from typing import TYPE_CHECKING, Any +from ansiblelint.constants import LINE_NUMBER_KEY from ansiblelint.errors import MatchError from ansiblelint.rules import AnsibleLintRule @@ -36,7 +37,9 @@ def matchplay(self, file: Lintable, data: dict[str, Any]) -> list[MatchError]: self.create_matcherror( message="Play uses strategy: free", filename=file, - tag="run_once[play]", + tag=f"{self.id}[play]", + # pylint: disable=protected-access + linenumber=strategy._line_number, ) ] @@ -54,7 +57,8 @@ def matchtask( self.create_matcherror( message="Using run_once may behave differently if strategy is set to free.", filename=file, - tag="run_once[task]", + tag=f"{self.id}[task]", + linenumber=task[LINE_NUMBER_KEY], ) ]