# Switching User Confusion

**URL:** <https://forum.ansible.com/t/switching-user-confusion/14758>\
**Category:** Ansible Project\
**Created:** [March 12, 2014, 2:50am UTC](https://forum.ansible.com/t/switching-user-confusion/14758 "2014-03-12T02:50:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Goodhouse](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@James\_Goodhouse](https://forum.ansible.com/u/James_Goodhouse)\
**Post date:** [March 12, 2014, 2:50am UTC](https://forum.ansible.com/t/switching-user-confusion/14758/1 "2014-03-12T02:50:47Z")

</div>

I’m struggling getting Ansible to run commands under the proper user and I have a feeling it’s due to my misunderstanding of how things are working.

For example, I’m provisioning a vagrant box, and in my inventory file I have declared ansible\_ssh\_user=vagrant as well as ansible\_ssh\_private\_key to use vagrant insecure\_private\_key. All has been working fine, but for a particular command (synchronize) I want it to run as a different user altogether. I only seem to be able to run commands as the vagrant user or as root. The user I want to run the command as does not have a ssh key nor does it have sudo privileges.

Am I doing things all backwards or missing something simple here?

Thanks so much in advance!

James

---

<div class="post-metadata">

**Author:** ![Erik\_Anderson](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@Erik\_Anderson](https://forum.ansible.com/u/Erik_Anderson)\
**Post date:** [March 12, 2014, 2:59am UTC](https://forum.ansible.com/t/switching-user-confusion/14758/2 "2014-03-12T02:59:26Z")

</div>

The user you’re running ansible as needs to have privileges to run commands as your target user. In your use case, this is likely taken care of if you’re using one of the standard vagrant boxes.

If you need to override the default sudo user for a command, you can do this:

```auto
- name: run this command as the foo user
  sudo_user: foo
  command: /path/to/command

```

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [March 12, 2014, 3:40am UTC](https://forum.ansible.com/t/switching-user-confusion/14758/3 "2014-03-12T03:40:13Z")

</div>

Synchronize is a very special module in this context. It tries to be “smart” about determining the remote user based on an order of preference and other factors such as sudo. Could you show us the inventory file, the playbook and the ansible-playbook -vvvv output ? Setting “remote\_user: bob” on the synchronize task may fix it, but I’d really need to see what you have first to be sure.

---

<div class="post-metadata">

**Author:** ![James\_Goodhouse](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@James\_Goodhouse](https://forum.ansible.com/u/James_Goodhouse)\
**Post date:** [March 12, 2014, 3:56am UTC](https://forum.ansible.com/t/switching-user-confusion/14758/4 "2014-03-12T03:56:02Z")

</div>

What happens if I want to run a command on the server as a user that has no sudo privileges?

---

<div class="post-metadata">

**Author:** ![Erik\_Anderson](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@Erik\_Anderson](https://forum.ansible.com/u/Erik_Anderson)\
**Post date:** [March 12, 2014, 3:58am UTC](https://forum.ansible.com/t/switching-user-confusion/14758/5 "2014-03-12T03:58:49Z")

</div>

You're not telling ansible to connect as that user \*and then\* sudo. Rather,  
you're telling ansible to use the vagrant user (as always), and then issue  
something equivalent to this:

$ sudo -u foo /path/to/command

This runs the specified command as "foo".

-Erik

---

<div class="post-metadata">

**Author:** ![James\_Goodhouse](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@James\_Goodhouse](https://forum.ansible.com/u/James_Goodhouse)\
**Post date:** [March 14, 2014, 4:34pm UTC](https://forum.ansible.com/t/switching-user-confusion/14758/6 "2014-03-14T16:34:13Z")

</div>

I believe my issue was just a misunderstanding overall of rsync, so I’ll hold off on posting the stuff you asked about. Thank you for your help!
