# Invoking/aggregating modules - synchronize with sudo support

**URL:** <https://forum.ansible.com/t/invoking-aggregating-modules-synchronize-with-sudo-support/17683>\
**Category:** Ansible Developer\
**Created:** [March 5, 2015, 8:28pm UTC](https://forum.ansible.com/t/invoking-aggregating-modules-synchronize-with-sudo-support/17683 "2015-03-05T20:28:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mirko\_Friedenhagen](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@Mirko\_Friedenhagen](https://forum.ansible.com/u/Mirko_Friedenhagen)\
**Post date:** [March 5, 2015, 8:28pm UTC](https://forum.ansible.com/t/invoking-aggregating-modules-synchronize-with-sudo-support/17683/1 "2015-03-05T20:28:57Z")

</div>

Hello,

I need to run a synchronize task as user root to copy some files while  
preserving permissions etc. My first solution was to create an  
exception so the ssh user is allowed to run rsync via a sudoers rule  
with NOPASSWD in a pre\_task and in a post\_task I cleaned this up and  
removed the sudoers rule again. This feels hack and I do not like the  
idea to have something like NOPASSWD.

I now have found a solution which goes like this:

- name: Create sudo\_askpass  
copy:  
dest: “{{ansible\_env.PWD}}/pw”  
owner: “{{ansible\_env.SUDO\_USER}}”  
mode: 0700  
content: |  
#!/bin/sh  
echo {{hostvars[inventory\_hostname][‘ansible\_sudo\_pass’]}}  
changed\_when: false

- name: Copy directories.  
synchronize:  
src: “{{item.src}}/”  
dest: /etc/gsc/{{item.dest}}  
archive: no  
recursive: yes  
times: yes  
rsync\_path: “SUDO\_ASKPASS={{ansible\_env.PWD}}/pw sudo -A rsync”  
with\_items:

- { src: “src1”, dest: “bar” }

- name: Delete sudo\_askpass  
file:  
path: “{{ansible\_env.PWD}}/pw”  
state: absent  
changed\_when: false

However, when synchronize fails, this will no clean up my simple pw file.

Would this be worth to be implemented in the synchronize task?

Or is it possible to aggregate/invoke the existing modules from a new task sudosynchronize?
