Ansible Vault
Playbooks need secrets: database passwords, API tokens, TLS keys. They must live next to the code (the play needs them) but must never be readable in Git. Ansible Vault encrypts files or single values with AES-256, using a password you keep elsewhere, and decrypts them in memory during a run.
Encrypting a variables file
printf 'correct horse battery staple\n' > .vault_pass && chmod 600 .vault_pass
mkdir -p group_vars/prod
cat > group_vars/prod/vault.yml <<'YML'
vault_db_password: s3cr3t-pg
vault_api_token: tok_live_8f2a
YML
ansible-vault encrypt group_vars/prod/vault.yml --vault-password-file .vault_pass
echo "=== on disk (first line and line count)"
head -1 group_vars/prod/vault.yml; wc -l < group_vars/prod/vault.yml
echo "=== view"
ansible-vault view group_vars/prod/vault.yml --vault-password-file .vault_pass=== on disk (first line and line count)
$ANSIBLE_VAULT;1.1;AES256
8
=== view
vault_db_password: s3cr3t-pg
vault_api_token: tok_live_8f2a
The file is now an $ANSIBLE_VAULT;1.1;AES256 header followed by hex-encoded ciphertext (with a random salt, so it is different every time you encrypt). Other commands: ansible-vault edit (decrypts into your $EDITOR and re-encrypts on save), create, decrypt, rekey (change the password).
The vault_ indirection pattern
Put the secrets in vault.yml with a vault_ prefix, and reference them from a plain vars.yml next to it:
# group_vars/prod/vars.yml (plain text, reviewable)
db_password: "{{ vault_db_password }}"
api_token: "{{ vault_api_token }}"grep -r db_password then still finds where a variable is defined, and reviewers see which secrets exist without decrypting anything.
Using it in a play
printf '[defaults]\ninterpreter_python = auto_silent\n' > ansible.cfg
printf 'correct horse battery staple\n' > .vault_pass
mkdir -p group_vars/all
printf 'vault_db_password: s3cr3t-pg\n' > group_vars/all/vault.yml
ansible-vault encrypt group_vars/all/vault.yml --vault-password-file .vault_pass >/dev/null 2>&1
printf 'db_password: "{{ vault_db_password }}"\n' > group_vars/all/vars.yml
cat > app.yml <<'YML'
- name: Use a vaulted secret
hosts: localhost
gather_facts: false
tasks:
- name: Write the DB credentials file
ansible.builtin.copy:
dest: db.env
content: "PGPASSWORD={{ db_password }}\n"
mode: "0600"
no_log: true
YML
echo "=== without the password"
ansible-playbook -i localhost, -c local app.yml 2>&1 | grep -E 'ERROR|fatal' | cut -c1-90
echo "=== with it"
ansible-playbook -i localhost, -c local app.yml --vault-password-file .vault_pass | grep -E '^(ok|changed)'
echo "=== with -v"
rm db.env
ansible-playbook -i localhost, -c local app.yml --vault-password-file .vault_pass -v | grep -E '^(ok|changed)'
cat db.env=== without the password
[ERROR]: Attempting to decrypt but no vault secrets found.
=== with it
changed: [localhost]
=== with -v
changed: [localhost] => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": true}
PGPASSWORD=s3cr3t-pg
Two details:
- Without the password, the run fails as soon as the encrypted value is needed.
- With
-v, Ansible prints each task's result;no_log: truereplaced it with a "censored" message, so the secret can't leak into logs, even with-vvv. Use it on every task that handles secrets (anddiff: false, as in Check Mode & Diff Mode).
-i localhost, (with the comma) is an inline inventory: a comma-separated host list instead of a file.
Encrypting single values
encrypt_string encrypts one value, to paste into an otherwise plain YAML file:
printf 'correct horse battery staple\n' > .vault_pass
ansible-vault encrypt_string --vault-password-file .vault_pass 'tok_live_8f2a' --name api_token 2>/dev/null | head -2api_token: !vault |
$ANSIBLE_VAULT;1.1;AES256
Whole-file encryption is easier to rotate and review; inline values are handy when a file has one secret among many plain settings.
Where the vault password lives
| Option | Use |
|---|---|
--ask-vault-pass |
type it; fine for a person at a terminal |
--vault-password-file .vault_pass (or vault_password_file in ansible.cfg, ANSIBLE_VAULT_PASSWORD_FILE) |
a file outside Git (.gitignore it) |
| an executable password file | a script that prints the password, fetched from a password manager, AWS Secrets Manager or HashiCorp Vault |
| CI secret | the pipeline writes it to a temporary file from a masked CI variable |
--vault-id prod@prompt, --vault-id dev@.dev_pass |
several passwords, one per environment, labelled |
Vault's limits
Ansible Vault is simple and good enough for many teams, but: every secret is shared by everyone who has the one password, there is no audit trail of who read what, and rotating means re-encrypting files and redeploying. For larger setups, keep the secrets in a secrets manager and read them at run time with a lookup (community.hashi_vault.hashi_vault, amazon.aws.aws_secret). The Secrets & Policy as Code course goes deeper.
Test yourself
What does ansible-vault encrypt group_vars/prod/vault.yml do?
- Uploads it to HashiCorp Vault
- Encrypts the file in place with AES-256
- Deletes it
- Hashes the values
Why reference vault_db_password from a plain db_password variable?
- Required by Ansible
- Variables stay greppable and reviewable without decrypting
- It makes it faster
- To use two passwords
What does no_log: true do?
- Encrypts the task
- Hides the task's arguments and results from output and logs
- Disables logging for the whole play
- Skips the task