# Execute shell command as a different user?

**URL:** <https://forum.ansible.com/t/execute-shell-command-as-a-different-user/13404>\
**Category:** Ansible Project\
**Created:** [September 2, 2013, 4:15pm UTC](https://forum.ansible.com/t/execute-shell-command-as-a-different-user/13404 "2013-09-02T16:15:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Harris](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@Jason\_Harris](https://forum.ansible.com/u/Jason_Harris)\
**Post date:** [September 2, 2013, 4:15pm UTC](https://forum.ansible.com/t/execute-shell-command-as-a-different-user/13404/1 "2013-09-02T16:15:59Z")

</div>

I am probably missing something somewhere in the documentation but it is not clear to me how to execute a shell command as a different user.

Eg I would like to do something like:

- name: Upload the compressed terminal preferences  
copy: src=dot\_gconf.zip dest={{home\_dir}}/dot\_gconf.zip
- name: Uncompress the terminal preferences  
shell: unzip -u {{home\_dir}}/dot\_gconf.zip
- name: Remove the compressed terminal preferences  
file: path={{home\_dir}}/dot\_gconf.zip state=absent

But I need the unzip to run from user {{user\_name}} Ie I would like to say something like:

- name: Uncompress the terminal preferences

shell: unzip -u {{home\_dir}}/dot\_gconf.zip  
user: {{user\_name}}

What is the best way to do this? (Everything else should be executed , copied, evaluated from a different setup admin user)

Thanks!  
Jason

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 2, 2013, 9:36pm UTC](https://forum.ansible.com/t/execute-shell-command-as-a-different-user/13404/2 "2013-09-02T21:36:19Z")

</div>

You will need to start a new play below the current one.

You can specify “sudo\_user” on a per task basis in Ansible 1.3, however.

---

<div class="post-metadata">

**Author:** ![Jason\_Harris](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@Jason\_Harris](https://forum.ansible.com/u/Jason_Harris)\
**Post date:** [September 4, 2013, 8:43am UTC](https://forum.ansible.com/t/execute-shell-command-as-a-different-user/13404/3 "2013-09-04T08:43:29Z")

</div>

Thanks!
