Thanks for your great advice, but maybe there were some syntax error, hence I couldn't update suoders file.but i figured it out the syntax error too.
here's an update for my testing, and it seems to work as i wanted to..but i will test in my main prod hosts where i faced the problem. here's what i did:
I have 2 vms: vm1 & vm2
1. vm1 is my ansible control machine and vm2 is target node.
2. vm1 has user harry
3. vm2 has user tom (non-admin) and admin (admin user)
4. in vm2, i added tom & admin to sudoers file as:
# ansible testing
admin ALL=(ALL) NOPASSWD:ALL
tom ALL = (admin) NOPASSWD: ALL
so, in vm2, i can do this as tom:
[tom@centos7vm2 ~]$ sudo -u admin -i sudo touch /root/new_file --> successful
[tom@centos7vm2 ~]$ sudo -u admin -i sudo ls -l /root/ --> successful
total 4
-rw-------. 1 root root 1519 May 7 22:58 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 May 14 20:31 new_file
[tom@centos7vm2 ~]$ sudo -u admin -i sudo whoami --> successful
root
5. in vm2, i can switch to admin user from tom user without password too:
[tom@centos7vm2 ~]$ sudo -u admin -i --> successful
[admin@centos7vm2 ~]$ whoami
admin --> became admin user from tom
[admin@centos7vm2 ~]$ exit
logout
[tom@centos7vm2 ~]$ --> back to tom user
6. but this fails in vm2 as tom, as expected:
[tom@centos7vm2 ~]$ sudo ls -l /root
[sudo] password for tom:
Sorry, user tom is not allowed to execute '/bin/ls -l /root' as root on centos7vm2. --> failed, as expected
[tom@centos7vm2 ~]$
7. in vm1, i installed ansible (v2.9.9), and ensured connectivity from vm1 to vm2. in vm1, i connect to vm2 as tom for ansible. this works fine:
--> my host file
[harry@centos7vm1 ansible]$ cat hosts
vm2 ansible_ssh_host=192.168.10.2 ansible_ssh_user=tom
--> run ping to vm2 from vm1 using ansible
[harry@centos7vm1 ansible]$ ansible -i hosts all -m ping --> successful, connectivity check
vm2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[harry@centos7vm1 ansible]$ ansible -i hosts all -m shell -a "whoami" --> successful
vm2 | CHANGED | rc=0 >>
tom --> at remote host, it's tom user
[harry@centos7vm1 ansible]$
8. Now, from vm1 i tried this, and it worked:
--> run command at vm2 as admin user while establishing ssh connection to vm2 as tom user from vm1
[harry@centos7vm1 ansible]$ ansible -i hosts all -m shell -a "sudo ls -l /root" --become-method sudo --become-user admin -b -K
BECOME password: (entered tom's password)
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
vm2 | CHANGED | rc=0 >>
total 4
-rw-------. 1 root root 1519 May 7 22:58 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 May 14 20:31 new_file --> successful, i can list root user's file using sudo by becoming admin user
--> use 'become' but without ask password flag (-K)
[harry@centos7vm1 ansible]$ ansible -i hosts all -m shell -a "ls -l /home/admin" --become-method sudo --become-user admin -b
vm2 | CHANGED | rc=0 >>
total 0
-rw-rw-r--. 1 admin admin 0 May 14 20:40 admin-secret-file --> i can list admin's home folder file's while connecting to vm2 as tom user
9. So, i can connect to vm2 as tom (non-admin user) and run commands as admin (without sudo).
and for those commands that admin needs sudo, i can also run them as tom in vm2 using ansible from vm1, with sudo prefix, although ansible shows warning like below:
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
so in this test, as harry user i'm able to run commands as admin user without pass, and admin has full sudo access without pass (except few commands like shutdown) and ansible showing warning not to use sudo prefix in shell module.
example of running sudo command as admin while ssh-ing to vm2 as tom (non-admin) from vm1:
[harry@centos7vm1 ansible]$ ansible -i hosts all -m shell -a "sudo ls -l /root" --become-method sudo --become-user admin -b
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
vm2 | CHANGED | rc=0 >>
total 4
-rw-------. 1 root root 1519 May 7 22:58 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 May 14 20:31 new_file
[harry@centos7vm1 ansible]$
Thank you very much for pointing in the right direction. i didn't know about ansible's mechanism of sudo-ing internally.
---