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

Add Ansible check mode exception to IgnoreErrorsRule #1548

Merged
merged 2 commits into from
May 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/ansiblelint/rules/IgnoreErrorsRule.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class IgnoreErrorsRule(AnsibleLintRule):
'Use failed_when and specify error conditions instead of using ignore_errors'
)
description = (
'Instead of ignoring all errors, use ``failed_when:`` '
'and specify acceptable error conditions '
'to reduce the risk of ignoring important failures'
'Instead of ignoring all errors, ignore the errors only when using ``{{ ansible_check_mode }}``, '
'register the errors using ``register``, '
'or use ``failed_when:`` and specify acceptable error conditions '
'to reduce the risk of ignoring important failures.'
)
severity = 'LOW'
tags = ['unpredictability', 'experimental']
Expand All @@ -29,8 +30,11 @@ class IgnoreErrorsRule(AnsibleLintRule):
def matchtask(
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
) -> Union[bool, str]:

if task.get("ignore_errors") and not task.get("register"):
if (
task.get("ignore_errors")
and task.get("ignore_errors") != "{{ ansible_check_mode }}"
and not task.get("register")
):
return True

return False
Expand All @@ -55,6 +59,14 @@ def matchtask(
ignore_errors: false
'''

IGNORE_ERRORS_CHECK_MODE = '''
- hosts: all
tasks:
- name: run apt-get update
command: apt-get update
ignore_errors: "{{ ansible_check_mode }}"
'''

IGNORE_ERRORS_REGISTER = '''
- hosts: all
tasks:
Expand Down Expand Up @@ -94,6 +106,15 @@ def test_ignore_errors_false(rule_runner: Any) -> None:
results = rule_runner.run_playbook(IGNORE_ERRORS_FALSE)
assert len(results) == 0

@pytest.mark.parametrize(
'rule_runner', (IgnoreErrorsRule,), indirect=['rule_runner']
)
def test_ignore_errors_check_mode(rule_runner: Any) -> None:
"""The task uses ignore_errors: "{{ ansible_check_mode }}"."""
results = rule_runner.run_playbook(IGNORE_ERRORS_CHECK_MODE)
print(results)
assert len(results) == 0

@pytest.mark.parametrize(
'rule_runner', (IgnoreErrorsRule,), indirect=['rule_runner']
)
Expand Down