Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct run-once rule identifier and related examples #2952

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/playbooks/run-once-fail.yml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
16 changes: 9 additions & 7 deletions src/ansiblelint/rules/run_once.md
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/ansiblelint/rules/run_once.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
)
]

Expand All @@ -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],
)
]

Expand Down