↗ Ansible Vault in Five Minutes

I use Ansible Vault to keep benZine API tokens in an encrypted YAML file alongside my playbooks. Git stores the encrypted file, and Ansible unlocks it when I run a playbook with the vault password.
Update an existing token
From the ansible catalog, run:
ansible-vault edit vault/benzine-api-tokens.yml
Enter the vault password if prompted. Ansible opens a temporary plaintext copy in your editor. Change the token, save, and quit. Ansible encrypts the updated file again before writing it back.
To choose the editor for this run:
EDITOR=nano ansible-vault edit vault/benzine-api-tokens.yml
Set it up for the first time
If the file doesn't exist yet, create it directly as an encrypted file:
mkdir -p vault
ansible-vault create vault/benzine-api-tokens.yml
The file holds ordinary YAML variables. This example uses a placeholder token and an illustrative variable name:
benzine_api_token: "<your-api-token>"
If you already have a plaintext YAML file at that path, encrypt it in place:
ansible-vault encrypt vault/benzine-api-tokens.yml
Load the tokens in a playbook
For a playbook at the repository root, add the vault file to the play's vars_files:
- hosts: benzine
vars_files:
- vault/benzine-api-tokens.yml
Use {{ benzine_api_token }} wherever the play needs the token. If the playbook is named deploy.yml, run:
ansible-playbook deploy.yml --ask-vault-pass
Two commands worth remembering
| Command | What it does |
|---|---|
ansible-vault view vault/benzine-api-tokens.yml | Displays the decrypted YAML in your terminal. |
ansible-vault rekey vault/benzine-api-tokens.yml | Changes the password used to encrypt the vault file. |
Commit the encrypted YAML and keep the vault password outside the repository. Vault protects secrets at rest. Use no_log: true on tasks whose output could expose the token, and keep it out of debug output.
