I have had issues with sudo / normal users causing conflicts. At first I made two copies of the playbook and had one with sudo, one without, but that is terrible for maintenance.
The per-task sudo: yes can be helpful for local actions, and per-task remote_user will be handy too. But for running entire playbooks as either sudo or root...
The problem i faced was that i sometimes run as root and sometimes as a sudo user, and unfortunately, unlike a unix shell where running a sudo command as root is fine, ansible errors out with something like
"invalid output was: [sudo via ansible, key=gxdfdfcyogebthrkocompltleramynjw] password:
Sorry, try again."
My solution now is to set all tasks with
- hosts: foo
sudo: "{{use_sudo}}"
and pass in use_sudo as an extra var ala, ansible-playbook -i hosts site.yml --extra-vars="use_sudo=yes".
But it would be nice if ansible just made it possible to run sudo commands as root like normal unix.
Here is a more complete context of the problem i was trying to solve.
admin_user is the sometimes root, sometimes sudo user that can add new users. It failed until I added the "use_sudo" arg as outlined above.
ansible-playbook -i hosts playbooks/utils/sudo_useradd.yml --extra-vars="hosts=bespin_cluster user=ansible pwd=*** admin_user=root sudo_pwd=required"
in sudo_useradd.yml
- hosts: "{{hosts}}"
vars:
pwd_sudo_line: "%sudo ALL=(ALL) ALL"
no_pwd_sudo_line: "%sudo ALL=(ALL) NOPASSWD: ALL"
sudoers_line: "{{pwd_sudo_line}}" # default, can be over-ridden
user: "{{admin_user}}"
sudo: yes
tasks:
TASK: [Add the desired lines] *************************************************
failed: [bespin] => {"failed": true, "item": "", "parsed": false}
failed: [Linux-Elasticsearch] => {"failed": true, "item": "", "parsed": false}
invalid output was: [sudo via ansible, key=gxdfdfcyogebthrkocompltleramynjw] password:
Sorry, try again.