How do you apply least privilege to `become`?
BeginnerProductionSecurity and Secrets
Answer
Enable become only on the tasks that need it, not at play level for everything. Fact gathering, API calls delegated to localhost and reads do not need root. Use become_user for service accounts rather than root where possible, such as running application deploy steps as app.
- hosts: web
tasks:
- ansible.builtin.git: { repo: "{{ repo }}", dest: /srv/app }
become: true
become_user: app
- ansible.builtin.systemd_service: { name: app, state: restarted }
become: true
More on Security and Secrets
How do you set up a secure automation user and SSH access?BeginnerHow do you give the automation user sudo with least privilege?IntermediateWhat 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?Beginner