From 5149c41229cf4fa02f826823732eca482ce63cb5 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Sun, 11 Jul 2021 22:39:10 -0700 Subject: [PATCH 01/11] Adding parameter to synchronize module to suppress verbose output and print error only --- plugins/modules/synchronize.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index 2676275..82fdc3c 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -417,6 +417,7 @@ def main(): delay_updates=dict(type='bool', default=True), mode=dict(type='str', default='push', choices=['pull', 'push']), link_dest=dict(type='list', elements='str'), + print_err_only=dict(type='bool',default=False) ), supports_check_mode=True, ) @@ -457,6 +458,7 @@ def main(): verify_host = module.params['verify_host'] link_dest = module.params['link_dest'] delay_updates = module.params['delay_updates'] + print_err_only = module.params['print_err_only'] if '/' not in rsync: rsync = module.get_bin_path(rsync, required=True) @@ -606,6 +608,10 @@ def main(): else: (rc, out, err) = module.run_command(cmd) + # If print_errors_only is true, supress the verbose output by suppressing changes but allowing errors + if print_err_only: + return module.fail_json(msg=err) + if rc: return module.fail_json(msg=err, rc=rc, cmd=cmdstr) From 6e96120b145841047886499cd636d14ba1fa1a42 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 12 Jul 2021 09:00:16 -0700 Subject: [PATCH 02/11] Adding rsync parameter 'quiet' to synchronize --- plugins/modules/synchronize.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index 82fdc3c..cac441f 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -417,7 +417,7 @@ def main(): delay_updates=dict(type='bool', default=True), mode=dict(type='str', default='push', choices=['pull', 'push']), link_dest=dict(type='list', elements='str'), - print_err_only=dict(type='bool',default=False) + quiet=dict(type='bool',default=False) ), supports_check_mode=True, ) @@ -458,7 +458,7 @@ def main(): verify_host = module.params['verify_host'] link_dest = module.params['link_dest'] delay_updates = module.params['delay_updates'] - print_err_only = module.params['print_err_only'] + quiet = module.params['quiet'] if '/' not in rsync: rsync = module.get_bin_path(rsync, required=True) @@ -608,9 +608,9 @@ def main(): else: (rc, out, err) = module.run_command(cmd) - # If print_errors_only is true, supress the verbose output by suppressing changes but allowing errors - if print_err_only: - return module.fail_json(msg=err) + # If quiet is true, supress the verbose output by suppressing changes but allowing errors + if quiet: + cmd.append('--quiet') if rc: return module.fail_json(msg=err, rc=rc, cmd=cmdstr) From 112fe9e9d0fbfcb7d604e3654899d687a0a0167f Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 12 Jul 2021 09:13:16 -0700 Subject: [PATCH 03/11] Added documentation and example for quiet option --- plugins/modules/synchronize.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index cac441f..9217c91 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -184,6 +184,11 @@ options: type: bool default: yes version_added: '1.3.0' + quiet: + description: + - This specifies rsync quiet option which on yes/true suppresses the non-error messages + type: bool + default: no notes: - rsync must be installed on both the local and remote host. @@ -341,6 +346,12 @@ EXAMPLES = r''' src: /tmp/localpath/ dest: /tmp/remotepath rsync_path: /usr/gnu/bin/rsync + +- name: Synchronization with --quiet option enabled + ansible.posix.synchronize: + src: some/relative/path + dest: /some/absolute/path + quiet: yes ''' @@ -608,7 +619,6 @@ def main(): else: (rc, out, err) = module.run_command(cmd) - # If quiet is true, supress the verbose output by suppressing changes but allowing errors if quiet: cmd.append('--quiet') From afe4ec833479e140b3e417282cf47100944dce0b Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 12 Jul 2021 10:21:36 -0700 Subject: [PATCH 04/11] Fix: moving conditional to correct place --- plugins/modules/synchronize.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index 9217c91..9cc5fdc 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -189,6 +189,7 @@ options: - This specifies rsync quiet option which on yes/true suppresses the non-error messages type: bool default: no + version_added: '1.3.0' notes: - rsync must be installed on both the local and remote host. @@ -598,6 +599,9 @@ def main(): if '@' not in dest: dest = os.path.expanduser(dest) + if quiet: + cmd.append('--quiet') + cmd.append(source) cmd.append(dest) cmdstr = ' '.join(cmd) @@ -619,9 +623,6 @@ def main(): else: (rc, out, err) = module.run_command(cmd) - if quiet: - cmd.append('--quiet') - if rc: return module.fail_json(msg=err, rc=rc, cmd=cmdstr) From 8ceac4953464c838adcce85e4ac3f4d23ff58b41 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 12 Jul 2021 12:00:24 -0700 Subject: [PATCH 05/11] Added Changelog fragment --- changelogs/fragments/220_synchronize_add_quiet_option.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/220_synchronize_add_quiet_option.yml diff --git a/changelogs/fragments/220_synchronize_add_quiet_option.yml b/changelogs/fragments/220_synchronize_add_quiet_option.yml new file mode 100644 index 0000000..a9a7cb1 --- /dev/null +++ b/changelogs/fragments/220_synchronize_add_quiet_option.yml @@ -0,0 +1,2 @@ +minor_changes: +- synchronize - add the ``quiet`` option to supress non-error messages (https://github.com/ansible-collections/ansible.posix/issues/171). From 39930cac21fbbc2b1891a41dd523980b8a7242e9 Mon Sep 17 00:00:00 2001 From: mandar Date: Tue, 13 Jul 2021 18:08:26 -0400 Subject: [PATCH 06/11] Adding tests, corrections --- .../220_synchronize_add_quiet_option.yml | 2 +- plugins/modules/synchronize.py | 6 ++-- .../targets/synchronize/tasks/main.yml | 33 +++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/changelogs/fragments/220_synchronize_add_quiet_option.yml b/changelogs/fragments/220_synchronize_add_quiet_option.yml index a9a7cb1..e64a50c 100644 --- a/changelogs/fragments/220_synchronize_add_quiet_option.yml +++ b/changelogs/fragments/220_synchronize_add_quiet_option.yml @@ -1,2 +1,2 @@ minor_changes: -- synchronize - add the ``quiet`` option to supress non-error messages (https://github.com/ansible-collections/ansible.posix/issues/171). +- synchronize - add the ``quiet`` option to suppress non-error messages (https://github.com/ansible-collections/ansible.posix/issues/171). diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index 9cc5fdc..71bbe91 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -186,7 +186,7 @@ options: version_added: '1.3.0' quiet: description: - - This specifies rsync quiet option which on yes/true suppresses the non-error messages + - This specifies rsync quiet option which on yes/true suppresses the non-error messages. type: bool default: no version_added: '1.3.0' @@ -429,7 +429,7 @@ def main(): delay_updates=dict(type='bool', default=True), mode=dict(type='str', default='push', choices=['pull', 'push']), link_dest=dict(type='list', elements='str'), - quiet=dict(type='bool',default=False) + quiet=dict(type='bool', default=False) ), supports_check_mode=True, ) @@ -600,7 +600,7 @@ def main(): dest = os.path.expanduser(dest) if quiet: - cmd.append('--quiet') + cmd.append('--quiet') cmd.append(source) cmd.append(dest) diff --git a/tests/integration/targets/synchronize/tasks/main.yml b/tests/integration/targets/synchronize/tasks/main.yml index ac1aa03..0814728 100644 --- a/tests/integration/targets/synchronize/tasks/main.yml +++ b/tests/integration/targets/synchronize/tasks/main.yml @@ -227,3 +227,36 @@ - directory_a/foo.txt - directory_a - directory_b +- name: synchronize files with quiet option + synchronize: + src: '{{output_dir}}/foo.txt' + dest: '{{output_dir}}/foo.result' + quiet: true + register: sync_result + ignore_errors: true +- assert: + that: + - '''--quiet'' in sync_result.cmd' +- name: Cleanup + file: + state: absent + path: '{{output_dir}}/{{item}}' + with_items: + - foo.result + - bar.result +- name: synchronize files without quiet option + synchronize: + src: '{{output_dir}}/foo.txt' + dest: '{{output_dir}}/foo.result' + register: sync_result + ignore_errors: true +- assert: + that: + - '''--quiet'' not in sync_result.cmd' +- name: Cleanup + file: + state: absent + path: '{{output_dir}}/{{item}}' + with_items: + - foo.result + - bar.result From f5088453d27ab739afa549717d7e3fc837346f6c Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 27 Jul 2021 15:16:58 -0700 Subject: [PATCH 07/11] Modifying based on feedback --- plugins/modules/synchronize.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index 9cc5fdc..3a427f9 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -186,7 +186,7 @@ options: version_added: '1.3.0' quiet: description: - - This specifies rsync quiet option which on yes/true suppresses the non-error messages + - This option specifies quiet option which on true suppresses the output. type: bool default: no version_added: '1.3.0' @@ -348,7 +348,7 @@ EXAMPLES = r''' dest: /tmp/remotepath rsync_path: /usr/gnu/bin/rsync -- name: Synchronization with --quiet option enabled +- name: Synchronization with quiet option enabled ansible.posix.synchronize: src: some/relative/path dest: /some/absolute/path @@ -599,9 +599,6 @@ def main(): if '@' not in dest: dest = os.path.expanduser(dest) - if quiet: - cmd.append('--quiet') - cmd.append(source) cmd.append(dest) cmdstr = ' '.join(cmd) @@ -636,15 +633,20 @@ def main(): out_lines = out_clean.split('\n') while '' in out_lines: out_lines.remove('') + + result = dict(changed=changed, rc=rc, cmd=cmdstr) + + if quiet: + result['msg'] = "OUTPUT IS HIDDEN DUE TO 'quiet=true'" + result['stdout_lines'] = [] + else: + result['msg'] = out_clean + result['std_out'] = out_lines + if module._diff: - diff = {'prepared': out_clean} - return module.exit_json(changed=changed, msg=out_clean, - rc=rc, cmd=cmdstr, stdout_lines=out_lines, - diff=diff) - - return module.exit_json(changed=changed, msg=out_clean, - rc=rc, cmd=cmdstr, stdout_lines=out_lines) + result['diff'] = {'prepare': out_clean} + return module.exit_json(**result) if __name__ == '__main__': main() From 01f599cd7ae9b7a87f55cff0e9c9301de833e2dc Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Wed, 28 Jul 2021 15:11:25 -0700 Subject: [PATCH 08/11] Modifying output to be displayed based on feedback --- plugins/modules/synchronize.py | 13 +++++-------- .../integration/targets/synchronize/tasks/main.yml | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index e03124f..a96488d 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -8,7 +8,6 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type - DOCUMENTATION = r''' --- module: synchronize @@ -634,19 +633,17 @@ def main(): while '' in out_lines: out_lines.remove('') - result = dict(changed=changed, rc=rc, cmd=cmdstr) + result = dict(changed=changed, rc=rc, cmd=cmdstr, stdout_lines=out_lines, msg=out_clean) if quiet: - result['msg'] = "OUTPUT IS HIDDEN DUE TO 'quiet=true'" - result['stdout_lines'] = [] - else: - result['msg'] = out_clean - result['std_out'] = out_lines + changes = len(out_lines) - 1 if len(out_lines) >= 1 else 0 + result['msg'] = "%s files/directories have been synchronized" % changes if module._diff: - result['diff'] = {'prepare': out_clean} + result['diff'] = {'prepared': out_clean} return module.exit_json(**result) + if __name__ == '__main__': main() diff --git a/tests/integration/targets/synchronize/tasks/main.yml b/tests/integration/targets/synchronize/tasks/main.yml index 0814728..a5153d8 100644 --- a/tests/integration/targets/synchronize/tasks/main.yml +++ b/tests/integration/targets/synchronize/tasks/main.yml @@ -236,7 +236,7 @@ ignore_errors: true - assert: that: - - '''--quiet'' in sync_result.cmd' + - '''files/directories have been synchronized'' in sync_result.msg' - name: Cleanup file: state: absent @@ -252,7 +252,7 @@ ignore_errors: true - assert: that: - - '''--quiet'' not in sync_result.cmd' + - '''files/directories have been synchronized'' not in sync_result.msg' - name: Cleanup file: state: absent From d5f7cf0b22b7ec72f46117a66e71ce92338820cd Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Thu, 5 Aug 2021 16:15:45 -0700 Subject: [PATCH 09/11] Modify based on feedback --- plugins/modules/synchronize.py | 2 +- .../integration/targets/synchronize/tasks/main.yml | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index a96488d..729b374 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -636,7 +636,7 @@ def main(): result = dict(changed=changed, rc=rc, cmd=cmdstr, stdout_lines=out_lines, msg=out_clean) if quiet: - changes = len(out_lines) - 1 if len(out_lines) >= 1 else 0 + changes = out.count(changed_marker) if changed else 0 result['msg'] = "%s files/directories have been synchronized" % changes if module._diff: diff --git a/tests/integration/targets/synchronize/tasks/main.yml b/tests/integration/targets/synchronize/tasks/main.yml index a5153d8..4b521f4 100644 --- a/tests/integration/targets/synchronize/tasks/main.yml +++ b/tests/integration/targets/synchronize/tasks/main.yml @@ -229,8 +229,8 @@ - directory_b - name: synchronize files with quiet option synchronize: - src: '{{output_dir}}/foo.txt' - dest: '{{output_dir}}/foo.result' + src: '{{ output_dir }}/foo.txt' + dest: '{{ output_dir }}/foo.result' quiet: true register: sync_result ignore_errors: true @@ -240,14 +240,14 @@ - name: Cleanup file: state: absent - path: '{{output_dir}}/{{item}}' - with_items: + path: '{{ output_dir }}/{{ item }}' + loop: - foo.result - bar.result - name: synchronize files without quiet option synchronize: - src: '{{output_dir}}/foo.txt' - dest: '{{output_dir}}/foo.result' + src: '{{ output_dir }}/foo.txt' + dest: '{{ output_dir }}/foo.result' register: sync_result ignore_errors: true - assert: @@ -256,7 +256,7 @@ - name: Cleanup file: state: absent - path: '{{output_dir}}/{{item}}' + path: '{{ output_dir }}/{{ item }}' with_items: - foo.result - bar.result From c8790770da3c0c864b602aac3ee800e4a12d4972 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Thu, 5 Aug 2021 16:58:51 -0700 Subject: [PATCH 10/11] Minor fix, remove ignore_errors --- tests/integration/targets/synchronize/tasks/main.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/targets/synchronize/tasks/main.yml b/tests/integration/targets/synchronize/tasks/main.yml index 4b521f4..0a2b056 100644 --- a/tests/integration/targets/synchronize/tasks/main.yml +++ b/tests/integration/targets/synchronize/tasks/main.yml @@ -233,7 +233,6 @@ dest: '{{ output_dir }}/foo.result' quiet: true register: sync_result - ignore_errors: true - assert: that: - '''files/directories have been synchronized'' in sync_result.msg' @@ -249,7 +248,6 @@ src: '{{ output_dir }}/foo.txt' dest: '{{ output_dir }}/foo.result' register: sync_result - ignore_errors: true - assert: that: - '''files/directories have been synchronized'' not in sync_result.msg' From a072f0b2ef3421f37401cbe72ff99ff8180b7d3e Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Thu, 5 Aug 2021 17:00:03 -0700 Subject: [PATCH 11/11] Minor fix, with_items --- tests/integration/targets/synchronize/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/synchronize/tasks/main.yml b/tests/integration/targets/synchronize/tasks/main.yml index 0a2b056..cc90783 100644 --- a/tests/integration/targets/synchronize/tasks/main.yml +++ b/tests/integration/targets/synchronize/tasks/main.yml @@ -255,6 +255,6 @@ file: state: absent path: '{{ output_dir }}/{{ item }}' - with_items: + loop: - foo.result - bar.result