Ansible Tutorial · Configuration Management · Lesson 15 of 20
Ansible Vault
Encrypt secrets so they can be stored safely in version control.
Why Vault?
Playbooks often need passwords, API keys and certificates. Storing them in plain text in Git is a security risk. Ansible Vault encrypts files or individual variables with AES-256, so secrets can live in the repository safely.
Encrypt a file
ansible-vault create secrets.yml # create and encrypt a new file
ansible-vault encrypt existing.yml # encrypt an existing file
ansible-vault view secrets.yml # view without decrypting to disk
ansible-vault edit secrets.yml # decrypt in $EDITOR, re-encrypt on save
ansible-vault decrypt secrets.yml # permanently decrypt
You will be prompted for a vault password.
Encrypted file contents
A vault-encrypted file looks like this:
$ANSIBLE_VAULT;1.1;AES256
31363236396138353136346235323136623735626134363565373664613137623937
...
Using encrypted files in a playbook
# playbook.yml
---
- name: Deploy with secrets
hosts: webservers
become: true
vars_files:
- vars/common.yml
- vars/secrets.yml # encrypted
tasks:
- name: Set database password
ansible.builtin.template:
src: db.conf.j2
dest: /etc/app/db.conf
Run with the vault password:
ansible-playbook site.yml --ask-vault-pass
# or
ansible-playbook site.yml --vault-password-file ~/.vault_pass
Encrypting individual variables
Instead of encrypting the whole file, encrypt just the secret value:
ansible-vault encrypt_string 'my_secret_password' --name 'db_password'
Output:
db_password: !vault |
$ANSIBLE_VAULT;1.1;AES256
61306233393838...
Paste this into your variables file. The rest of the file stays readable.
Vault IDs — multiple passwords
For different environments you may want different vault passwords:
ansible-vault encrypt --vault-id prod@prompt secrets_prod.yml
ansible-vault encrypt --vault-id staging@~/.staging_pass secrets_staging.yml
Run with multiple vault IDs:
ansible-playbook site.yml --vault-id prod@prompt --vault-id staging@~/.staging_pass
Best practices
- Never commit unencrypted secrets to Git.
- Use
--vault-password-filepointing to a file excluded from Git (.gitignore). - In CI, store the vault password in a CI secret and write it to a temp file.
- Prefer encrypting individual variables over entire files — it keeps your YAML readable.
- Rotate vault passwords periodically with
ansible-vault rekey.
echo "vault_pass_123" > /tmp/.vault_pass
ansible-vault encrypt_string 'SuperSecret42' --name 'db_password' --vault-password-file /tmp/.vault_pass 2>/dev/null
Output
db_password: !vault |
$ANSIBLE_VAULT;1.1;AES256
33363264656537313039643432623665643036643833323534383466393466363061
3831356338633630316264323362326233393336386532650a353264323562653234
36643133666265313064616633303133653165383839346462613131663538613762
6462363339303261350a393465343438653161393237633864316631383535363163
3237
Encryption successfulRun this on your computer — see “Get Started” for setup.
ansible-vault --help 2>&1 | head -15
Output
usage: ansible-vault [-h] [--version] [-v]
{create,decrypt,edit,view,encrypt,encrypt_string,rekey}
...
encryption/decryption utility for Ansible data files
positional arguments:
{create,decrypt,edit,view,encrypt,encrypt_string,rekey}
create Create new vault encrypted file
decrypt Decrypt vault encrypted file
edit Edit vault encrypted file
view View vault encrypted file
encrypt Encrypt YAML file
encrypt_string Encrypt a string
rekey Re-key a vault encrypted fileRun this on your computer — see “Get Started” for setup.
Test yourself
How do you run a playbook that uses vault-encrypted files?
ansible-playbook site.yml --decryptansible-playbook site.yml --ask-vault-passansible-playbook site.yml --vaultansible-playbook site.yml --secret
Show answer
Answer: ansible-playbook site.yml --ask-vault-pass
Use --ask-vault-pass to be prompted for the password, or --vault-password-file to read it from a file.