# Change user while executing ansible script.

**URL:** <https://forum.ansible.com/t/change-user-while-executing-ansible-script/22916>\
**Category:** Ansible Project\
**Created:** [October 28, 2016, 7:45pm UTC](https://forum.ansible.com/t/change-user-while-executing-ansible-script/22916 "2016-10-28T19:45:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nitin\_Mathur](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Nitin\_Mathur](https://forum.ansible.com/u/Nitin_Mathur)\
**Post date:** [October 28, 2016, 7:45pm UTC](https://forum.ansible.com/t/change-user-while-executing-ansible-script/22916/1 "2016-10-28T19:45:24Z")

</div>

Hi,

I am trying to figure out, if the below scenario is possible and if so, how I can achieve it.

My target machine and ansible host is the same machine. There are 2 users, user-1(non-sudo privilege), user-2 (sudo privilege). Both user-1 and user-2 have passwords. I am logged in to my Ansible host as a user-1(non-sudo privilege) and execute an ansible script. I need to execute this ansible script with sudo privileges meaning as user-2(with sudo privilege). Also the password entry should be non-interactive. Is there a way to do this using group\_vars or any other way?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Alexander\_H\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_H\_Laughlin](https://forum.ansible.com/u/Alexander_H_Laughlin)\
**Post date:** [October 28, 2016, 8:16pm UTC](https://forum.ansible.com/t/change-user-while-executing-ansible-script/22916/2 "2016-10-28T20:16:19Z")

</div>

If you store the password in a vaulted file I believe you can use su as the become method and accomplish this result.

---

<div class="post-metadata">

**Author:** ![Nitin\_Mathur](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@Nitin\_Mathur](https://forum.ansible.com/u/Nitin_Mathur)\
**Post date:** [October 28, 2016, 8:34pm UTC](https://forum.ansible.com/t/change-user-while-executing-ansible-script/22916/3 "2016-10-28T20:34:54Z")

</div>

Can you please provide an example or reference??

---

<div class="post-metadata">

**Author:** ![Alexander\_H\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_H\_Laughlin](https://forum.ansible.com/u/Alexander_H_Laughlin)\
**Post date:** [October 29, 2016, 1:29am UTC](https://forum.ansible.com/t/change-user-while-executing-ansible-script/22916/4 "2016-10-29T01:29:17Z")

</div>

[Become](http://docs.ansible.com/ansible/become.html#id11) documentation.

[Ansible Vault](http://docs.ansible.com/ansible/playbooks_vault.html)

It seems as though you need the root password and you should use a become method of ‘su’ to get done what you need done.

I’m not sure why you wouldn’t just add user-1 to sudoers, though.
