Your Ansible repo contains a TLS private key, a database password, and three API tokens. You want them under version control next to the playbook that uses them, but obviously not in plain text. Ansible Vault is the built-in answer — symmetric AES-256-CTR encryption, per-file or per-variable, with the password supplied at runtime. This article covers the patterns that work in a multi-environment, multi-operator setting and the ones that get someone fired.
How to verify
A correctly-vaulted file is unreadable but parseable as a vault payload; the cleartext is decrypted only when Ansible runs.
head -1 inventory/production/group_vars/db.vault.yml
ansible-vault view --vault-password-file=.vault-pass inventory/production/group_vars/db.vault.yml
ansible-playbook -i inventory/production playbooks/site.yml --vault-password-file=.vault-pass --check
grep -rni 'password\|secret\|api_key' inventory/ roles/ playbooks/ \
| grep -v '!vault\|ANSIBLE_VAULT'
The first line of a vault file should be $ANSIBLE_VAULT;1.2;AES256;<label>. The grep at the end should return zero matches — any hit is a cleartext secret that escaped vaulting.
What’s happening
Ansible Vault encrypts files (whole-file mode) or values (inline mode) with AES-256-CTR. The encryption key is derived from a password using PBKDF2; the password is supplied at runtime via --vault-password-file, a password prompt, or a vault password script. Ansible 2.4+ supports vault-id — multiple labeled vault passwords in the same run, so production and staging can use different passwords without splitting the repo.
The mental model: vault is at-rest protection. The vault password is the actual secret, and it lives outside the repo — typically in ~/.vault-pass/<env> on operator workstations and as an environment variable or secret store reference in CI. If the vault password leaks, every secret encrypted under it is compromised.
The procedure
-
Choose a password storage path. A simple file in
~/.vault-pass/productionworks for solo and small-team setups; for larger ones, use a vault password script that calls a real secret store (1Password CLI, AWS Secrets Manager, HashiCorp Vault).mkdir -m 0700 ~/.vault-pass openssl rand -base64 48 > ~/.vault-pass/production chmod 0600 ~/.vault-pass/production -
Encrypt a file. Whole-file mode is right for
group_vars/<group>.vault.yml.ansible-vault encrypt \ --vault-id production@~/.vault-pass/production \ inventory/production/group_vars/db.vault.yml -
Encrypt a single value (inline). When most of a vars file is non-sensitive, encrypt only the sensitive value and keep the rest readable in diffs.
ansible-vault encrypt_string \ --vault-id production@~/.vault-pass/production \ 'super-secret-password' \ --name 'db_password'Paste the resulting
db_password: !vault |block into the vars file. -
Reference the vault-id everywhere.
ansible.cfglets you set a default so operators do not have to remember the flag.# ansible.cfg [defaults] vault_identity_list = production@~/.vault-pass/production, staging@~/.vault-pass/staging -
Wire CI with the password supplied as a secret. GitHub Actions or GitLab CI mounts the password as a file before invoking Ansible.
# .github/workflows/deploy.yml - name: Configure Ansible vault run: | mkdir -p ~/.vault-pass printf '%s' "${{ secrets.ANSIBLE_VAULT_PRODUCTION }}" > ~/.vault-pass/production chmod 0600 ~/.vault-pass/production - run: ansible-playbook -i inventory/production playbooks/site.yml --check --diff -
Rotate the vault password.
rekeyre-encrypts the file with a new password without changing the cleartext.ansible-vault rekey \ --vault-id production@~/.vault-pass/production \ --new-vault-id production@~/.vault-pass/production.new \ inventory/production/group_vars/db.vault.ymlAfter rekey, replace the old password file with the new one, distribute the new one to operators, and update CI secrets.
-
Edit vaulted files in place.
editdecrypts to a tempfile, opens$EDITOR, and re-encrypts on save — no plaintext lands on disk.ansible-vault edit \ --vault-id production@~/.vault-pass/production \ inventory/production/group_vars/db.vault.yml
Common pitfalls
- A repo with
.vault-passcommitted alongside the vault file means the encryption is decorative;.gitignorethe password file as the first commit. ansible-vault viewwrites the cleartext to the terminal — shell history can capture it. Useeditfor interactive work.- The vault password is not derived from the contents, so the encrypted ciphertext changes on every edit even if the plaintext did not — large vault files produce noisy diffs.
- Inline
!vault |values cannot be diffed meaningfully — for anything you expect to change often, encrypt the whole file instead. - A leaked vault password requires rotating every secret encrypted under it; vault is not a substitute for proper secret rotation in the source systems.
In the engagements we run, Ansible Vault is the floor — every secret in source is vaulted, the vault password sits in a real secret store (not in CI variables), and rotations are tracked. For higher-stakes secrets (DB superuser, TLS keys for the customer edge), we lean on HashiCorp Vault or the cloud provider’s secret manager and use Ansible to read them at run time. See /en/services/managed-operations/.