How do you keep secrets out of Git?
BeginnerProductionSecurity and Secrets
Answer
- Encrypt anything sensitive with Vault before committing, and name files so it is obvious (
vault.yml). - A pre-commit hook that rejects unencrypted vault files (check the
$ANSIBLE_VAULT;header) plus a scanner such asgitleaksordetect-secretsin pre-commit and CI. .gitignorefor*.pem,.vault_pass,*.retry,collections/.- Branch protection so the CI scan cannot be skipped.
If a secret does get committed, treat it as compromised: rotate it first, then clean history. Rewriting history alone does not un-leak anything.
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