Ansible SSH as one user and Sudo as another

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

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.

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