# Ansible SSH as one user and Sudo as another

**URL:** <https://forum.ansible.com/t/ansible-ssh-as-one-user-and-sudo-as-another/17281>\
**Category:** Ansible Project\
**Created:** [January 15, 2015, 4:53pm UTC](https://forum.ansible.com/t/ansible-ssh-as-one-user-and-sudo-as-another/17281 "2015-01-15T16:53:04Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![mahendravarma.yadava](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@mahendravarma.yadava](https://forum.ansible.com/u/mahendravarma.yadava)\
**Post date:** [August 5, 2016, 9:21pm UTC](https://forum.ansible.com/t/ansible-ssh-as-one-user-and-sudo-as-another/17281/21 "2016-08-05T21:21:31Z")

</div>

Hi guys

need some help here, my config is little different ,

foo ALL=/bin/su - root

If use as below using the above suggestion I am getting success for non root related tasks

ansible client -m raw -a “touch /tmp/testfile” --ask-sudo-pass  
SUDO password:  
vm-dev3-wol01 | SUCCESS | rc=0 \>\>

but for some activities I need root permission to copy and rename the files, I get a below error where I need root permissions

ansible client1 -m raw -a “touch /opt/testfile” --ask-sudo-pass  
SUDO password:  
vm-dev3-wol01 | FAILED | rc=1 \>\>  
touch: cannot touch ‘/opt/testfile’: Permission denied

Thanks Mahendra

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 9, 2016, 10:17am UTC](https://forum.ansible.com/t/ansible-ssh-as-one-user-and-sudo-as-another/17281/22 "2016-08-09T10:17:44Z")

</div>

Since you are only permitted to run "/bin/su - root" with sudo you are out of luck if you can't change it, Ansible need to run different commands as sudo.

---

<div class="post-metadata">

**Author:** ![prakash\_ranjan](https://avatars.discourse-cdn.com/v4/letter/p/f6c823/32.png) [@prakash\_ranjan](https://forum.ansible.com/u/prakash_ranjan)\
**Post date:** [September 5, 2017, 6:28pm UTC](https://forum.ansible.com/t/ansible-ssh-as-one-user-and-sudo-as-another/17281/23 "2017-09-05T18:28:36Z")

</div>

Hi,

This is what I’m getting:-

Working without sudo. But not with sudo option.

ansibledir$ ansible all -m command -a ‘whoami’  
 | SUCCESS | rc=0 \>\>  
pranjan

ansibledir$ ansible all -m command -a ‘whoami’ --sudo -K  
SUDO password:  
 | FAILED! =\> {  
“changed”: false,  
“failed”: true,  
“module\_stderr”: “Shared connection to dc1-io-new closed.\r\n”,  
“module\_stdout”: “\r\nSorry, user pranjan is not allowed to execute ‘/bin/sh -c echo BECOME-SUCCESS-bgclrmmybsvnbasemntshqvjavcnqvjf; /usr/bin/python /home/pranjan/.ansible/tmp/ansible-tmp-1504636239.12-238251240956861/command.py; rm -rf "/home/pranjan/.ansible/tmp/ansible-tmp-1504636239.12-238251240956861/" \> /dev/null 2\>&1’ as root on \r\n”,  
“msg”: “MODULE FAILURE”,  
“rc”: 1  
}

Please help on this.

Thanks  
Prakash

[Previous page](https://forum.ansible.com/t/ansible-ssh-as-one-user-and-sudo-as-another/17281.md?page=1)
