Encrypt secrets at rest using Ansible Vault so credentials can be version-controlled safely. Practice the full operator workflow: create, view, edit, use in a playbook run, and rotate vault passwords.
Your Ansible repo needs to store database credentials and API
tokens, but storing secrets in plaintext is not acceptable. You
will use ansible-vault to encrypt secrets at rest,
load them during a playbook run, and rotate the vault password.
Vault is the baseline for secrets-in-Git workflows. It reduces accidental leakage, supports reviews, and enables controlled rotation without rewriting your automation.
ansible-vault create.
vars_files in a playbook.--ask-vault-pass (interactive).
ansible-vault rekey.
cat is a red flag.
ansible-vault create secrets.yml
You will be prompted for a vault password and dropped into an editor. Save and exit to write the encrypted file to disk.
db_user: appuser
db_pass: "S3cure!ChangeMe"
db_host: 10.10.20.15
api_token: "REPLACE_ME"
head -n 5 secrets.yml
You should see the vault header and ciphertext. If you see plaintext, stop and fix your workflow before committing anything.
$ANSIBLE_VAULT;1.1;AES256
63343036626337373633623130316337306431393933623365323762313662613330656162666162
3437613934313637396537333663396161373535653733650a383132336231373431613765333834
64666130356161613161343364313834313565333235366535633333313433656365323131353631
ansible-vault edit secrets.yml
Vault decrypts in memory, opens your editor, and re-encrypts on save.
ansible-vault view secrets.yml
This prints plaintext. Treat it like you would treat
cat on a private key.
vars_files.
# vault-playbook.yml
- name: Test vault secrets
hosts: localhost
gather_facts: false
vars_files:
- secrets.yml
tasks:
- name: Show DB connection target
debug:
msg: "DB={{ db_user }}@{{ db_host }}"
The vault file is decrypted at run time when you provide the vault password.
ansible-playbook vault-playbook.yml --ask-vault-pass
Vault password:
PLAY [Test vault secrets] ******************************************************
TASK [Show DB connection target] ***********************************************
ok: [localhost] => {
"msg": "DB=appuser@10.10.20.15"
}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
ansible-vault rekey secrets.yml
Rekey changes the password used to decrypt this file. This is a normal control for rotation workflows.
You either provided the wrong vault password, or the file was encrypted with a different vault secret. Rekey requires the current password to rotate.
Treat it as a leak. Remove the secret from history and rotate credentials. Vault prevents this scenario, but it does not fix it after the fact.
Avoid printing secrets to stdout. Prefer using secrets in tasks without exposing
values, and avoid debug with sensitive variables.
rm -f secrets.yml vault-playbook.yml
ansible-vault create <file>
: Create a new encrypted file (prompts for password, opens editor).
ansible-vault edit <file>
: Edit an encrypted file (decrypts in memory, re-encrypts on save).
ansible-vault view <file>
: View decrypted content to stdout.
ansible-vault rekey <file>
: Rotate the vault password for a file.
ansible-playbook <playbook> --ask-vault-pass
: Prompt for vault password during a run.
vars_files:
: Load variables from external YAML files (including vault files).