How do you give the automation user sudo with least privilege?
IntermediateProductionSecurity and Secrets
Answer
Passwordless sudo for everything (ansible ALL=(ALL) NOPASSWD: ALL) is the common default and it makes the account equivalent to root. If your compliance regime requires tighter control, restrict by command, but be realistic: Ansible runs Python modules as /bin/sh -c ... python3 ..., so a command allow-list is hard to maintain.
Practical controls in order of value: key only access from a known control node (via from= in authorized_keys or security groups), a sudoers drop-in validated by visudo, session logging with auditd, and short-lived credentials where you can get them.
- ansible.builtin.copy:
dest: /etc/sudoers.d/ansible
content: "ansible ALL=(ALL) NOPASSWD: ALL\n"
mode: "0440"
validate: /usr/sbin/visudo -cf %s
More on Security and Secrets
How do you set up a secure automation user and SSH access?BeginnerWhat are the alternatives to Ansible Vault for secrets, and when do you use them?IntermediateHow do you read AWS Secrets Manager and SSM parameters in a playbook?IntermediateHow do you integrate HashiCorp Vault with Ansible?AdvancedHow do you stop secrets from leaking into logs and output?BeginnerHow do you handle the vault password in CI/CD?Intermediate