From d2bdbadb0327d519c1c353bf3e28f83ba1b59485 Mon Sep 17 00:00:00 2001 From: rwagnergit Date: Thu, 28 Feb 2019 10:25:41 -0500 Subject: [PATCH] adding quiet option to assert (ansible#27124) (#52032) * adding quiet option to assert (ansible#27124) * adding doc for quiet to assert.py * fixing PEP8 failure * improving example * improving docs * adding changelog fragment * adding . at end of descriptions * removing trailing blank line * adding integration test for assert * fixing CI complaints * disabling gather_facts in quiet.yml test * rerunning to capture skip * cleaning up python 2 vs 3 * following rebase * fixing CI complaints * fixing CI complaints * fixing CI complaints * fixing CI complaints * fixing CI complaints --- .../52032-add_quiet_option_to_assert.yml | 3 + lib/ansible/modules/utilities/logic/assert.py | 23 +++++-- lib/ansible/plugins/action/assert.py | 9 ++- test/integration/targets/assert/aliases | 1 + .../assert/assert_quiet.out.quiet.stderr | 2 + .../assert/assert_quiet.out.quiet.stdout | 16 +++++ test/integration/targets/assert/inventory | 3 + test/integration/targets/assert/quiet.yml | 16 +++++ test/integration/targets/assert/runme.sh | 68 +++++++++++++++++++ 9 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/52032-add_quiet_option_to_assert.yml create mode 100644 test/integration/targets/assert/aliases create mode 100644 test/integration/targets/assert/assert_quiet.out.quiet.stderr create mode 100644 test/integration/targets/assert/assert_quiet.out.quiet.stdout create mode 100644 test/integration/targets/assert/inventory create mode 100644 test/integration/targets/assert/quiet.yml create mode 100755 test/integration/targets/assert/runme.sh diff --git a/changelogs/fragments/52032-add_quiet_option_to_assert.yml b/changelogs/fragments/52032-add_quiet_option_to_assert.yml new file mode 100644 index 00000000000..e640aea5e69 --- /dev/null +++ b/changelogs/fragments/52032-add_quiet_option_to_assert.yml @@ -0,0 +1,3 @@ +minor_changes: + - assert - added ``quiet`` option to the ``assert`` module to avoid verbose + output (https://github.com/ansible/ansible/issues/27124). diff --git a/lib/ansible/modules/utilities/logic/assert.py b/lib/ansible/modules/utilities/logic/assert.py index b45bb729870..58adb022afb 100644 --- a/lib/ansible/modules/utilities/logic/assert.py +++ b/lib/ansible/modules/utilities/logic/assert.py @@ -24,20 +24,26 @@ version_added: "1.5" options: that: description: - - "A string expression of the same form that can be passed to the 'when' statement" - - "Alternatively, a list of string expressions" + - "A string expression of the same form that can be passed to the 'when' statement." + - "Alternatively, a list of string expressions." required: true fail_msg: version_added: "2.7" description: - - "The customized message used for a failing assertion" - - "This argument was called 'msg' before version 2.7, now it's renamed to 'fail_msg' with alias 'msg'" + - "The customized message used for a failing assertion." + - "This argument was called 'msg' before version 2.7, now it's renamed to 'fail_msg' with alias 'msg'." aliases: - msg success_msg: version_added: "2.7" description: - - "The customized message used for a successful assertion" + - "The customized message used for a successful assertion." + quiet: + version_added: "2.8" + description: + - "Set this to C(true) to avoid verbose output." + type: bool + default: false notes: - This module is also supported for Windows targets. author: @@ -67,4 +73,11 @@ EXAMPLES = ''' - "my_param <= 100" - "my_param >= 0" msg: "'my_param' must be between 0 and 100" + +- name: use quiet to avoid verbose output + assert: + that: + - "my_param <= 100" + - "my_param >= 0" + quiet: true ''' diff --git a/lib/ansible/plugins/action/assert.py b/lib/ansible/plugins/action/assert.py index a3b6af811a3..1190ec984a8 100644 --- a/lib/ansible/plugins/action/assert.py +++ b/lib/ansible/plugins/action/assert.py @@ -21,13 +21,14 @@ from ansible.errors import AnsibleError from ansible.playbook.conditional import Conditional from ansible.plugins.action import ActionBase from ansible.module_utils.six import string_types +from ansible.module_utils.parsing.convert_bool import boolean class ActionModule(ActionBase): ''' Fail with custom message ''' TRANSFERS_FILES = False - _VALID_ARGS = frozenset(('fail_msg', 'msg', 'success_msg', 'that')) + _VALID_ARGS = frozenset(('fail_msg', 'msg', 'quiet', 'success_msg', 'that')) def run(self, tmp=None, task_vars=None): if task_vars is None: @@ -54,6 +55,8 @@ class ActionModule(ActionBase): elif not isinstance(success_msg, string_types): raise AnsibleError('Incorrect type for success_msg, expected string and got %s' % type(success_msg)) + quiet = boolean(self._task.args.get('quiet', False), strict=False) + # make sure the 'that' items are a list thats = self._task.args['that'] if not isinstance(thats, list): @@ -65,7 +68,9 @@ class ActionModule(ActionBase): # by this point, and is not used again, so we don't care about mangling # that value now cond = Conditional(loader=self._loader) - result['_ansible_verbose_always'] = True + if not quiet: + result['_ansible_verbose_always'] = True + for that in thats: cond.when = [that] test_result = cond.evaluate_conditional(templar=self._templar, all_vars=task_vars) diff --git a/test/integration/targets/assert/aliases b/test/integration/targets/assert/aliases new file mode 100644 index 00000000000..b59832142f2 --- /dev/null +++ b/test/integration/targets/assert/aliases @@ -0,0 +1 @@ +shippable/posix/group3 diff --git a/test/integration/targets/assert/assert_quiet.out.quiet.stderr b/test/integration/targets/assert/assert_quiet.out.quiet.stderr new file mode 100644 index 00000000000..bd973b04e02 --- /dev/null +++ b/test/integration/targets/assert/assert_quiet.out.quiet.stderr @@ -0,0 +1,2 @@ ++ ansible-playbook -i localhost, -c local quiet.yml +++ set +x diff --git a/test/integration/targets/assert/assert_quiet.out.quiet.stdout b/test/integration/targets/assert/assert_quiet.out.quiet.stdout new file mode 100644 index 00000000000..65b82a6f283 --- /dev/null +++ b/test/integration/targets/assert/assert_quiet.out.quiet.stdout @@ -0,0 +1,16 @@ + +PLAY [localhost] *************************************************************** + +TASK [assert] ****************************************************************** +ok: [localhost] => (item=item_A) + +TASK [assert] ****************************************************************** +ok: [localhost] => (item=item_A) => { + "changed": false, + "item": "item_A", + "msg": "All assertions passed" +} + +PLAY RECAP ********************************************************************* +localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 + diff --git a/test/integration/targets/assert/inventory b/test/integration/targets/assert/inventory new file mode 100644 index 00000000000..161820042a0 --- /dev/null +++ b/test/integration/targets/assert/inventory @@ -0,0 +1,3 @@ +[all] +localhost + diff --git a/test/integration/targets/assert/quiet.yml b/test/integration/targets/assert/quiet.yml new file mode 100644 index 00000000000..6834712c2c1 --- /dev/null +++ b/test/integration/targets/assert/quiet.yml @@ -0,0 +1,16 @@ +--- +- hosts: localhost + gather_facts: False + vars: + item_A: yes + tasks: + - assert: + that: "{{ item }} is defined" + quiet: True + with_items: + - item_A + - assert: + that: "{{ item }} is defined" + quiet: False + with_items: + - item_A diff --git a/test/integration/targets/assert/runme.sh b/test/integration/targets/assert/runme.sh new file mode 100755 index 00000000000..198f7be3e0d --- /dev/null +++ b/test/integration/targets/assert/runme.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +# This test compares "known good" output with various settings against output +# with the current code. It's brittle by nature, but this is probably the +# "best" approach possible. +# +# Notes: +# * options passed to this script (such as -v) are ignored, as they would change +# the output and break the test +# * the number of asterisks after a "banner" differs is forced to 79 by +# redirecting stdin from /dev/null + +set -eux + +run_test() { + # testname is playbook name + local testname=$1 + + # The shenanigans with redirection and 'tee' are to capture STDOUT and + # STDERR separately while still displaying both to the console + { ansible-playbook -i 'localhost,' -c local "${testname}.yml" \ + > >(set +x; tee "${OUTFILE}.${testname}.stdout"); } \ + 2> >(set +x; tee "${OUTFILE}.${testname}.stderr" >&2) 0