From 91f884f3ae8464d7c02b63b2a33891ac3a123a67 Mon Sep 17 00:00:00 2001 From: alessfg Date: Thu, 6 May 2021 16:46:38 +0200 Subject: [PATCH 1/2] Add Ansible check mode exception to IgnoreErrorsRule --- src/ansiblelint/rules/IgnoreErrorsRule.py | 24 +++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/ansiblelint/rules/IgnoreErrorsRule.py b/src/ansiblelint/rules/IgnoreErrorsRule.py index 6c589fae8b..f3286f1567 100644 --- a/src/ansiblelint/rules/IgnoreErrorsRule.py +++ b/src/ansiblelint/rules/IgnoreErrorsRule.py @@ -29,8 +29,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 @@ -55,6 +58,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: @@ -94,6 +105,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'] ) From 91d1046d3c28adda1a643f1ca695fabe939cd43e Mon Sep 17 00:00:00 2001 From: alessfg Date: Thu, 6 May 2021 17:10:46 +0200 Subject: [PATCH 2/2] Update rule description --- src/ansiblelint/rules/IgnoreErrorsRule.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ansiblelint/rules/IgnoreErrorsRule.py b/src/ansiblelint/rules/IgnoreErrorsRule.py index f3286f1567..c1a00ae4e6 100644 --- a/src/ansiblelint/rules/IgnoreErrorsRule.py +++ b/src/ansiblelint/rules/IgnoreErrorsRule.py @@ -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']