Skip to content

Commit

Permalink
Add Ansible check mode exception to IgnoreErrorsRule (#1548)
Browse files Browse the repository at this point in the history
* Add Ansible check mode exception to IgnoreErrorsRule

* Update rule description
  • Loading branch information
alessfg committed May 14, 2021
1 parent 58f57e3 commit 9a2c156
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/ansiblelint/rules/IgnoreErrorsRule.py
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

0 comments on commit 9a2c156

Please sign in to comment.