Ansible security: Is a user with password-less sudo permissions the way to go?

I'm trying to figure out what the best practices are for running privileged commands with Ansible, and I ran across this upvoted StackExchange answer

The relevant part:

[OP's suggestion] Create a unprivileged user dedicated for Ansible with sudo access / let the Ansible user to run every commands through sudo without specifying any password

[Reply] Exactly. An unprivileged user that you can audit back to ansible, with sudo roles. Ideally, create a standard user dedicated to server-to-server/ansible communications with sudo access (without password).

From what I understand, this is saying it's best to create a user that can run commands with sudo without using a password. However, how is this any different than logging in as the root user?

Posted Jan 25, 2019, 12:42 AM UTC

68

Logging.

If you log in as root to do the work, all you see in the logs is that someone logged in as root. This may have been you doing ansible work, or it may have been a malicious actor with a compromised key or password. So having a separate account helps with after-the-fact auditing.

Disabling root login over SSH completely also helps a little with security and a little more with compliance.

There are also better ways to secure non-root accounts with other security features (limits, SELinux, permissions, etc.) that don't necessarily apply to the root account.

And if you get really paranoid, it's possible to selectively grant the ansible user sudo privileges to install software and do other things, but prevent them access to user management or interactive root shells. This would allow you to mostly manage your systems with ansible but somewhat limit the possibility that the account could be used maliciously.

10
domsch1988•

Yes, I'd go as far as to say disable password login over ssh completely. Use keys. And sudo can give you really fine control over which commands can be run by a user as root. So limit that to what your plays need.

1
aoeudhtns•

Any advice on selective permissions? I work in a highly regulated industry and password-less sudo is a compliance failure, as is ALL permission in sudoers. Unfortunately, ansible re-wraps everything through sh and temporary script files, so specifically allowing, for example, apt-get install ... won't work unless we ignore the apt module and go straight to the command module - which makes Ansible, in those circumstances, mostly an annoying script wrapper. And it makes idempotency a challenge as well.

1

Wait, you're technically not allowed to use sudo at all?!

Edit : never mind, I get it now, you can select but not grant blanket privileges. I'd have to think about that for a while. I suspect you'd need to create an enormous command alias that contains "approved" binaries from /sbin

2
aoeudhtns•

That's what we do. We have a huge sudoers file and we have to use the command module so that Ansible will pass directly to the underlying system, but then if we need idempotency we have to engineer it ourselves with checks/registers.

1
xiongchiamiov•

To add, you don't need to do passwordless sudo - you can use --ask-become-pass when running your playbooks to provide the password.

1
MikeSeth•

A better way is to have ansible authenticate with a proxied SSH key. I have a hardware token for the SSH key and the GPG agent also acts as SSH agent. I log in to the ansible box (at which point the agent asks me for the smartcard PIN code; without it the token will not accept any interaction with the key), and the SSH key authentication is forwarded to that user, and when I run ansible, it also authenticates against the target hosts without ansible ever having direct access to the key. If I disconnect from the ansible machine or pul out the hardware token, ansible will no longer be able to connect to the targets.

2
xiongchiamiov•

We were talking about password for sudo, not for ssh authentication, but you remind me that you can use ssh agent to authenticate to sudo as well and that is another good way to do it.

2