# Hacky solution for running synchronize with sudo

**URL:** <https://forum.ansible.com/t/hacky-solution-for-running-synchronize-with-sudo/17659>\
**Category:** Ansible Project\
**Created:** [March 3, 2015, 4:32pm UTC](https://forum.ansible.com/t/hacky-solution-for-running-synchronize-with-sudo/17659 "2015-03-03T16:32:49Z")\
**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 3, 2015, 4:32pm UTC](https://forum.ansible.com/t/hacky-solution-for-running-synchronize-with-sudo/17659/1 "2015-03-03T16:32:49Z")

</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  
&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ansible\_env.PWD}}/pw"  
&nbsp;&nbsp;&nbsp;&nbsp;owner: "{{ansible\_env.SUDO\_USER}}"  
&nbsp;&nbsp;&nbsp;&nbsp;mode: 0700  
&nbsp;&nbsp;&nbsp;&nbsp;content: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#!/bin/sh  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo {{hostvars[inventory\_hostname]['ansible\_sudo\_pass']}}  
&nbsp;&nbsp;&nbsp;changed\_when: false

- name: Copy directories.  
&nbsp;&nbsp;synchronize:  
&nbsp;&nbsp;&nbsp;&nbsp;src: "{{item.src}}/"  
&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/gsc/{{item.dest}}  
&nbsp;&nbsp;&nbsp;&nbsp;archive: no  
&nbsp;&nbsp;&nbsp;&nbsp;recursive: yes  
&nbsp;&nbsp;&nbsp;&nbsp;times: yes  
&nbsp;&nbsp;&nbsp;&nbsp;rsync\_path: "SUDO\_ASKPASS={{ansible\_env.PWD}}/pw sudo -A rsync"  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- { src: "src1", dest: "bar" }

- name: Delete sudo\_askpass  
&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ansible\_env.PWD}}/pw"  
&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
&nbsp;&nbsp;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?

Regards Mirko
