I have just started looking into Ansible and have built a server on Centos 7. I generally don’t run anything as root so I created an ansible account and group and chown -R ansible:ansible /etc/ansible.
I assumed that this would be OK, but as I start experimenting with commands, it does not seem to use sudo the way I had expected.
As expected this fails because the ansible user does not have access to read /etc/shadow.
I found the problem by using the -m service instead of -a. The root user does not have permission to read the ansible tmp files because the home directory is an nfs mount. I’ll move the ansible user out of the NFS directory and that should fix things.
I have just started looking into Ansible and have built a server on Centos
7. I generally don't run anything as root so I created an ansible account
and group and chown -R ansible:ansible /etc/ansible.
I assumed that this would be OK, but as I start experimenting with
commands, it does not seem to use sudo the way I had expected.
It's perfectly OK to create a user, Ansible work great with sudo.
As expected this fails because the ansible user does not have access to
read /etc/shadow.
When I try -b and --become-user root, I get this.
-sh-4.2$ ansible localhost -a '/bin/cat /etc/shadow' -b --become-user root
localhost | FAILED | rc=0 >>
MODULE FAILURE
[snip]
Am I using --become-user incorrectly?
I would not say so. What you are missng is the sudo password.
The become user is default root, so you do not need to specify that.
And you are using -b for become.
But you must provide the sudo password if you don't have NOPASSWORD in sudoers.
So this should work for you:
ansible localhost -a 'cat /etc/shadow' -b --ask-sudo-pass