Lesson 5 of 9 · 26m
Ansible Vault Without Secret Sprawl
Use vault encryption, vaulted variable indirection, permissions, and rotation discipline without turning every variable file into an opaque blob.
Vault is easy to demonstrate and hard to operate. The demonstration encrypts a string. Operations asks harder questions: who can decrypt it, how is it reviewed in a pull request, how is it rotated, which environment does it belong to, and what happens when a CI job needs only one secret out of fifty? If you cannot answer those, vault is secrecy theater.
Encrypt values, not entire policy files
Whole-file encryption makes diffs useless. Reviewers cannot see which key changed, and teams begin blindly merging opaque blobs:
ansible-vault encrypt_string 'correct horse battery staple' --name 'db_admin_password' >> group_vars/production/vault.ymlPrefer a small vault file containing only secrets, while nonsensitive structure remains reviewable:
# group_vars/production/vault.yml
db_admin_password: !vault |
$ANSIBLE_VAULT;1.2;AES256;prod
3433...Indirection keeps vault names stable
Use a convention where plaintext variables reference vaulted variables:
# group_vars/production/main.yml
app_db_password: "{{ vault_app_db_password }}"
# group_vars/production/vault.yml
vault_app_db_password: !vault |
$ANSIBLE_VAULT;1.2;AES256;prod
6337...The vault prefix makes secret material obvious during review. Roles consume app_db_password and do not care how it was protected. Rotation changes one source of truth rather than hunting every task.
Vault IDs beat one universal password
Separate vault identities by environment or trust domain:
ansible-playbook site.yml --vault-id production@~/.vault/pass-production --vault-id staging@~/.vault/pass-stagingA single all-powerful vault password means every laptop that ever joined the project retains production decryption capability. Separate IDs make revocation meaningful. Store passwords in a real secret manager or OS keyring, not beside inventory in a dotfile committed by accident.
Know what vault does not protect
Vault encrypts data at rest in your repository. Once a playbook runs, values exist in memory, command lines, temporary files, logs, service configuration, and sometimes fact output. Reduce exposure:
- name: Write application secret configuration
ansible.builtin.template:
src: app.env.j2
dest: /etc/app/app.env
mode: "0600"
no_log: trueno_log hides task output, but it also hides debugging evidence. Use it narrowly. Better still, avoid putting secrets into registered output or command arguments in the first place.
Rotation is the real test
A secret you cannot rotate is not managed; it is trapped. The role should support overlapping credentials where the downstream system allows it, or at least a controlled maintenance path. Rotation documentation should name the exact inventory variable, the service restart behavior, and the verification check.
CI changes the threat model
CI systems need narrow vault access, short-lived credentials, protected branches, and masked logs. Do not decrypt production vault material merely to run lint or unit tests. Use dummy test values locally and reserve production decryption for controlled deployment jobs.
Field notes
- Vault is not access control after decryption; anyone with the password has every secret encrypted under it.
- Do not reuse secrets across staging and production to make tests easier.
ansible-vault rekeychanges encryption, not the secret value. Rotation means changing the credential in the downstream system.- Beware templates that render secrets into files with world-readable parent backups.
- If a secret must be typed by humans during a routine deployment, the workflow is fragile and will eventually be bypassed.
Was this lesson helpful?