Interview Prep/Ansible/Security and Secrets

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
All Ansible questions Roadmap: Ansible

More on Security and Secrets