The answer to your first question ("Is it possible to know exactly what command the following Ansible code will execute on the remote server?") is generally "only by inspecting the source for the corresponding module". A given module may run multiple commands in order to accomplish it's action.
The error message you are seeing ("sudo: a password is required.") does not suggest that the remote user does not have appropriate sudo rights. It only suggests that the remote user is not configured for passwordless sudo. Your two options are:
Provide a password to Ansible:
ansible-playbook -K secretpassword ...
Modify the sudoers configuration on the remote host to allow passwordless sudo:
remoteuser ALL=(ALL) NOPASSWD:ALL
Sudo configuration that involve a limited set of commands probably won't work, because Ansible is running a script using sudo. For example, if I run ansible-playbook -vvv against the following playbook:
- hosts: localhost
gather_facts: false
tasks:
- ping:
become: true
I will see:
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/lars/.ansible/cp/8a5a4c6a60 -tt localhost '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-rebjujbhceobxvfuylirykxzgdonillt; /usr/bin/python /home/lars/.ansible/tmp/ansible-tmp-1505503292.11-47458712165303/ping.py; rm -rf "/home/lars/.ansible/tmp/ansible-tmp-1505503292.11-47458712165303/" > /dev/null 2>&1'"'"'"'"'"'"'"'"' && sleep 0'"'"''
In other words, ansible is running:
sudo -H -S -n -u root /bin/sh -c '...embedded script here...'
The only command that sudo ever sees is /bin/sh, which means that a sudo configuration that limits you to only certain commands is doomed to fail.
If you're unable to fix the remote sudo configuration, you may want to investigate ansible's raw module.
ansible-playbook -vvvwill show you exact command to launch module.-vvvvactually, and noticed that it was running a .tmp file usingsudo -H -S -n -u root.... I also enabled the environment variableANSIBLE_KEEP_REMOTE_FILESso that I could view these files. However, once I opened a .tmp file, I wasn't able to determine exactly what was happening.sudoon the remote host without a password? Can you show us yoursudoersconfiguration? Do other tasks fail, or justservicetasks?