From 8bb61047f891e31f22e9625db99826a196a9d12e Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Sun, 11 Jul 2021 22:39:10 -0700 Subject: [PATCH 01/14] Adding parameter to synchronize module to suppress verbose output and print error only --- plugins/modules/synchronize.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index 3d8d0fe..f6b028a 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -437,6 +437,8 @@ def main(): delay_updates=dict(type='bool', default=True), mode=dict(type='str', default='push', choices=['pull', 'push']), link_dest=dict(type='list', elements='path'), + link_dest=dict(type='list', elements='str'), + print_err_only=dict(type='bool',default=False) ), supports_check_mode=True, ) @@ -477,6 +479,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) @@ -620,6 +623,10 @@ def main(): else: (rc, out, err) = module.run_command(cmdstr) + # 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 1a4c2051e85eb80c6e05bf2c0b32e05714cbb217 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 12 Jul 2021 09:00:16 -0700 Subject: [PATCH 02/14] 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 f6b028a..452dc48 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -438,7 +438,7 @@ def main(): mode=dict(type='str', default='push', choices=['pull', 'push']), link_dest=dict(type='list', elements='path'), 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, ) @@ -479,7 +479,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) @@ -623,9 +623,9 @@ def main(): else: (rc, out, err) = module.run_command(cmdstr) - # 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 fe7dd71bdaed3922a4ee36bfabe3fbcef6a4e1d5 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 12 Jul 2021 09:13:16 -0700 Subject: [PATCH 03/14] 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 452dc48..8b57cee 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -203,6 +203,11 @@ options: description: Internal use only. See C(use_ssh_args) for ssh arg settings. type: str required: false + 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. @@ -360,6 +365,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 ''' @@ -623,7 +634,6 @@ def main(): else: (rc, out, err) = module.run_command(cmdstr) - # If quiet is true, supress the verbose output by suppressing changes but allowing errors if quiet: cmd.append('--quiet') From 1e0654f8b652c5fe77188670fe6b04b6d203f5a2 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 12 Jul 2021 10:21:36 -0700 Subject: [PATCH 04/14] 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 8b57cee..c856354 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -208,6 +208,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. @@ -615,6 +616,9 @@ def main(): cmd.append(shlex_quote(source)) cmd.append(shlex_quote(dest)) + if quiet: + cmd.append('--quiet') + cmdstr = ' '.join(cmd) # If we are using password authentication, write the password into the pipe @@ -634,9 +638,6 @@ def main(): else: (rc, out, err) = module.run_command(cmdstr) - if quiet: - cmd.append('--quiet') - if rc: return module.fail_json(msg=err, rc=rc, cmd=cmdstr) From ee7748732c5bb27b79f6c0fb1d8c1b371a9435cb Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 12 Jul 2021 12:00:24 -0700 Subject: [PATCH 05/14] 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 6afd4cdcf1f9ed78a8b926f58cd82776f1eb59db Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 27 Jul 2021 15:16:58 -0700 Subject: [PATCH 06/14] Modifying based on feedback --- plugins/modules/synchronize.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index c856354..c526bb5 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -205,7 +205,7 @@ options: required: false 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' @@ -367,7 +367,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 @@ -651,15 +651,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 e72424ae50047fe9f0aa11b4712c0c40549e13c6 Mon Sep 17 00:00:00 2001 From: mandar Date: Tue, 13 Jul 2021 18:08:26 -0400 Subject: [PATCH 07/14] Adding tests, corrections --- .../220_synchronize_add_quiet_option.yml | 2 +- plugins/modules/synchronize.py | 4 ++-- tests/integration/inventory | 2 ++ .../targets/synchronize/tasks/main.yml | 23 +++++++++++++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 tests/integration/inventory 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 c526bb5..bc26692 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -450,7 +450,7 @@ def main(): mode=dict(type='str', default='push', choices=['pull', 'push']), link_dest=dict(type='list', elements='path'), link_dest=dict(type='list', elements='str'), - quiet=dict(type='bool',default=False) + quiet=dict(type='bool', default=False) ), supports_check_mode=True, ) @@ -617,7 +617,7 @@ def main(): cmd.append(shlex_quote(source)) cmd.append(shlex_quote(dest)) if quiet: - cmd.append('--quiet') + cmd.append('--quiet') cmdstr = ' '.join(cmd) diff --git a/tests/integration/inventory b/tests/integration/inventory new file mode 100644 index 0000000..5d6e706 --- /dev/null +++ b/tests/integration/inventory @@ -0,0 +1,2 @@ +[testgroup] +testhost ansible_connection="local" ansible_pipelining="yes" ansible_python_interpreter="/Users/mandkulk/venv3.9/bin/python" diff --git a/tests/integration/targets/synchronize/tasks/main.yml b/tests/integration/targets/synchronize/tasks/main.yml index d6dcdad..03212b3 100644 --- a/tests/integration/targets/synchronize/tasks/main.yml +++ b/tests/integration/targets/synchronize/tasks/main.yml @@ -342,9 +342,32 @@ - name: Cleanup ansible.builtin.file: state: absent +<<<<<<< HEAD path: "{{ output_dir }}/{{ item }}" loop: - directory b/foo.txt - directory a/foo.txt - directory a - directory b +======= + 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 +>>>>>>> 9dbedb6 (Adding tests, corrections) From b4fe18e6ff52c6753e5ad84646a98fa186d56c2f Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Wed, 28 Jul 2021 15:11:25 -0700 Subject: [PATCH 08/14] Modifying output to be displayed based on feedback --- plugins/modules/synchronize.py | 13 ++++------- .../targets/synchronize/tasks/main.yml | 23 ------------------- 2 files changed, 5 insertions(+), 31 deletions(-) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index bc26692..54065f3 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 @@ -652,19 +651,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 03212b3..d6dcdad 100644 --- a/tests/integration/targets/synchronize/tasks/main.yml +++ b/tests/integration/targets/synchronize/tasks/main.yml @@ -342,32 +342,9 @@ - name: Cleanup ansible.builtin.file: state: absent -<<<<<<< HEAD path: "{{ output_dir }}/{{ item }}" loop: - directory b/foo.txt - directory a/foo.txt - directory a - directory b -======= - 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 ->>>>>>> 9dbedb6 (Adding tests, corrections) From 793518be3c4eb63aecdd0a6854429e326e0fa470 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Thu, 5 Aug 2021 16:15:45 -0700 Subject: [PATCH 09/14] Modify based on feedback --- plugins/modules/synchronize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index 54065f3..e651e5c 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -654,7 +654,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: From 892c045679303715f25e2155c9f2c3706deaea22 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 7 Jun 2024 14:45:58 -0700 Subject: [PATCH 10/14] rebase --- plugins/modules/synchronize.py | 1 - .../targets/synchronize/tasks/main.yml | 29 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index e651e5c..819df22 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -448,7 +448,6 @@ def main(): delay_updates=dict(type='bool', default=True), mode=dict(type='str', default='push', choices=['pull', 'push']), link_dest=dict(type='list', elements='path'), - link_dest=dict(type='list', elements='str'), quiet=dict(type='bool', default=False) ), supports_check_mode=True, diff --git a/tests/integration/targets/synchronize/tasks/main.yml b/tests/integration/targets/synchronize/tasks/main.yml index d6dcdad..7ffba18 100644 --- a/tests/integration/targets/synchronize/tasks/main.yml +++ b/tests/integration/targets/synchronize/tasks/main.yml @@ -339,6 +339,35 @@ - stat_result_b.stat.exists == True - stat_result_b.stat.checksum == '2aae6c35c94fcfb415dbe95f408b9ce91ee846ed' +- name: synchronize files with quiet option + synchronize: + src: '{{ output_dir }}/foo.txt' + dest: '{{ output_dir }}/foo.result' + quiet: true + register: sync_result + +- assert: + that: + - '''files/directories have been synchronized'' in sync_result.msg' + +- name: Cleanup + file: + state: absent + 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' + register: sync_result + +- assert: + that: + - '''files/directories have been synchronized'' not in sync_result.msg' + - name: Cleanup ansible.builtin.file: state: absent From 0a58f59906e8e358429651b771e460adcc6a60e6 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 7 Jun 2024 14:48:33 -0700 Subject: [PATCH 11/14] modified based on feedback --- plugins/modules/synchronize.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/modules/synchronize.py b/plugins/modules/synchronize.py index 819df22..cfa6ec2 100644 --- a/plugins/modules/synchronize.py +++ b/plugins/modules/synchronize.py @@ -206,8 +206,8 @@ options: description: - This option specifies quiet option which on true suppresses the output. type: bool - default: no - version_added: '1.3.0' + default: false + version_added: 1.6.0 notes: - rsync must be installed on both the local and remote host. @@ -370,7 +370,7 @@ EXAMPLES = r''' ansible.posix.synchronize: src: some/relative/path dest: /some/absolute/path - quiet: yes + quiet: true ''' From 48c2e9310e866ee40271330ef33ed5ee9e314b91 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 7 Jun 2024 14:52:22 -0700 Subject: [PATCH 12/14] sanity fix in tests --- tests/integration/targets/synchronize/tasks/main.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/synchronize/tasks/main.yml b/tests/integration/targets/synchronize/tasks/main.yml index 7ffba18..039c67a 100644 --- a/tests/integration/targets/synchronize/tasks/main.yml +++ b/tests/integration/targets/synchronize/tasks/main.yml @@ -346,7 +346,8 @@ quiet: true register: sync_result -- assert: +- name: Assertion for synchronize with quiet option + assert: that: - '''files/directories have been synchronized'' in sync_result.msg' @@ -364,7 +365,8 @@ dest: '{{ output_dir }}/foo.result' register: sync_result -- assert: +- name: Assertion for synchronize without quiet option + assert: that: - '''files/directories have been synchronized'' not in sync_result.msg' From 94059765b650035f907ba595afcd22dcf9b7f478 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 7 Jun 2024 14:58:52 -0700 Subject: [PATCH 13/14] sanity fix in tests --- .../targets/synchronize/tasks/main.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/integration/targets/synchronize/tasks/main.yml b/tests/integration/targets/synchronize/tasks/main.yml index 039c67a..8f98282 100644 --- a/tests/integration/targets/synchronize/tasks/main.yml +++ b/tests/integration/targets/synchronize/tasks/main.yml @@ -339,27 +339,27 @@ - stat_result_b.stat.exists == True - stat_result_b.stat.checksum == '2aae6c35c94fcfb415dbe95f408b9ce91ee846ed' -- name: synchronize files with quiet option - synchronize: +- name: Synchronize files with quiet option + ansible.posix.synchronize: src: '{{ output_dir }}/foo.txt' dest: '{{ output_dir }}/foo.result' quiet: true register: sync_result - name: Assertion for synchronize with quiet option - assert: + ansible.builtin.assert: that: - - '''files/directories have been synchronized'' in sync_result.msg' + - '''files/directories have been synchronized'' in sync_result.msg' - name: Cleanup file: state: absent path: '{{ output_dir }}/{{ item }}' loop: - - foo.result - - bar.result + - foo.result + - bar.result -- name: synchronize files without quiet option +- name: Synchronize files without quiet option synchronize: src: '{{ output_dir }}/foo.txt' dest: '{{ output_dir }}/foo.result' @@ -368,7 +368,7 @@ - name: Assertion for synchronize without quiet option assert: that: - - '''files/directories have been synchronized'' not in sync_result.msg' + - '''files/directories have been synchronized'' not in sync_result.msg' - name: Cleanup ansible.builtin.file: From c401a5d331b61d90f4e4439b6cdf96abb7a4db1d Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 7 Jun 2024 15:03:48 -0700 Subject: [PATCH 14/14] sanity fix in tests --- tests/integration/targets/synchronize/tasks/main.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integration/targets/synchronize/tasks/main.yml b/tests/integration/targets/synchronize/tasks/main.yml index 8f98282..52a677c 100644 --- a/tests/integration/targets/synchronize/tasks/main.yml +++ b/tests/integration/targets/synchronize/tasks/main.yml @@ -352,7 +352,7 @@ - '''files/directories have been synchronized'' in sync_result.msg' - name: Cleanup - file: + ansible.builtin.file: state: absent path: '{{ output_dir }}/{{ item }}' loop: @@ -360,13 +360,13 @@ - bar.result - name: Synchronize files without quiet option - synchronize: + ansible.posix.synchronize: src: '{{ output_dir }}/foo.txt' dest: '{{ output_dir }}/foo.result' register: sync_result - name: Assertion for synchronize without quiet option - assert: + ansible.builtin.assert: that: - '''files/directories have been synchronized'' not in sync_result.msg'