LFCE 151 ๐ง Ansible โ Vault for Secrets
Ansible Vault encrypts variables and files so that passwords, API keys, and private keys can live in version control without being visible as plaintext. It is not a secret manager and it is not a substitute for one. It is a file-level encryption tool that protects data at rest: the encrypted file is safe to commit, but once Ansible decrypts it to use the value, the plaintext exists in memory and in any output that the playbook produces. The encryption is AES-256, the file format is text-armored, and the password that unlocks it must be supplied at runtime .
This chapter covers the Vault command-line tool, the file and string encryption modes, the password sources that avoid hardcoding the secret, and the convention that keeps the encrypted secrets separate from the plaintext variables that reference them.
Key point: Ansible Vault protects data at rest. The encrypted file is safe to commit, but the decrypted value is the playbook’s responsibility. Use no_log: true on tasks that handle secrets, keep the vault password out of the repository, and separate the encrypted vault.yml from the plaintext vars.yml that references it.
Why Vault is a file-level tool
The at-rest problem. A playbook that contains a database password in plaintext cannot be committed to a repository without exposing the password to everyone who can read the repository. Vault encrypts the file, so the committed content is ciphertext. The repository can be public, and the secret remains protected as long as the password is not committed alongside it .
The in-use problem. Vault does not protect the secret after decryption. When the playbook runs, Ansible decrypts the file and the plaintext is available to the modules that need it. The no_log task keyword is the mechanism that prevents the decrypted value from appearing in the playbook output. The Vault documentation states this explicitly: “Encryption with Ansible Vault ONLY protects ‘data at rest’. Once the content is decrypted (‘data in use’), play and plugin authors are responsible for avoiding any secret disclosure” .
The password problem. The vault password is the key to the encrypted data. If the password is stored in the repository, the encryption is useless. The password must be supplied at runtime from a source that is not committed: a prompt, an environment variable, a password file outside the repository, or a script that retrieves it from a secret manager .
a. The ansible-vault commands
The ansible-vault command has six subcommands. Each one operates on a file or a string and requires the vault password.
# Create a new encrypted file (opens $EDITOR)
ansible-vault create group_vars/all/vault.yml
# Encrypt an existing plaintext file
ansible-vault encrypt secrets.yml
# View an encrypted file without editing
ansible-vault view group_vars/all/vault.yml
# Edit an encrypted file (decrypts to temp, re-encrypts on save)
ansible-vault edit group_vars/all/vault.yml
# Decrypt a file (removes encryption)
ansible-vault decrypt secrets.yml
# Change the password on one or more files
ansible-vault rekey group_vars/all/vault.yml
The create subcommand opens the editor defined by $EDITOR, which defaults to vim. The file is encrypted when the editor closes. The edit subcommand decrypts the file to a temporary location, opens the editor, and re-encrypts the file when the editor closes. The rekey subcommand accepts multiple files and changes the password on all of them .
The encrypt_string subcommand encrypts a single value and outputs a YAML block that can be pasted into a variables file. The --name flag sets the variable name, and the output is the !vault tagged block.
# Encrypt a single string
ansible-vault encrypt_string 'SuperSecret123!' --name 'db_password'
The output is a block that begins with db_password: !vault | followed by the encrypted payload. The block is pasted directly into a playbook or a variables file .
b. The password sources
The vault password can be supplied in four ways, and the choice determines how the password is protected.
Interactive prompt. The --ask-vault-pass flag prompts for the password when the command runs. It is the safest option for a developer’s local machine because the password is never written to disk. It is unsuitable for automation because it requires a human at the terminal .
Password file. The --vault-password-file flag points to a file containing the password. The file must be outside the repository, and its permissions must be restricted to the owner. The ansible.cfg file can set the vault_password_file option to avoid the flag on every command .
# ansible.cfg
[defaults]
vault_password_file = ~/.vault_pass
Environment variable. The ANSIBLE_VAULT_PASSWORD_FILE environment variable sets the default password file. It is the mechanism for a CI pipeline that writes the password to a temporary file and sets the variable before running the playbook .
Script. The password file can be an executable script. Ansible runs the script and uses its standard output as the password. This is the mechanism for retrieving the password from a secret manager or a keyring at runtime .
The --vault-id flag combines a label with a password source. It is the mechanism for using different passwords for different environments or teams. The label appears in the encrypted file’s header, and Ansible uses the label to select the correct password source when decrypting .
# Encrypt with a vault ID
ansible-vault encrypt --vault-id prod@~/.vault_pass_prod group_vars/prod/vault.yml
# Run with multiple vault IDs
ansible-playbook site.yml --vault-id dev@prompt --vault-id prod@~/.vault_pass_prod
c. The separation convention
The standard convention is to separate the encrypted secrets from the plaintext variables that reference them. The vault.yml file contains the encrypted values with a vault_ prefix. The vars.yml file contains the plaintext variables that reference the encrypted ones.
# group_vars/all/vault.yml (encrypted)
vault_db_password: "SuperSecret123!"
vault_api_key: "ak_live_abc123"
# group_vars/all/vars.yml (plaintext)
db_password: "{{ vault_db_password }}"
api_key: "{{ vault_api_key }}"
The playbook references db_password, not vault_db_password. The vars.yml file is readable and diffable, and the vault.yml file is encrypted. The separation makes it clear which variables are secrets and which are not, and it keeps the diff on the plaintext file meaningful .
The .gitignore must exclude the password file and any temporary decrypted files. The vault.yml file is committed because it is encrypted.
# .gitignore
*.vault_pass
.vault_pass*
vault_pass.txt
*.decrypted
*.plain
Complete Example Session
# ============================================
# PART 1: CREATE THE ENCRYPTED FILE
# ============================================
ansible-vault create group_vars/all/vault.yml
# The editor opens. Add:
# vault_db_password: "SuperSecret123!"
# vault_api_key: "ak_live_abc123"
# ============================================
# PART 2: VIEW THE ENCRYPTED FILE
# ============================================
ansible-vault view group_vars/all/vault.yml
# The decrypted content is displayed in a pager.
# ============================================
# PART 3: ENCRYPT AN EXISTING FILE
# ============================================
ansible-vault encrypt secrets.yml
# The file is encrypted in place.
# ============================================
# PART 4: ENCRYPT A SINGLE STRING
# ============================================
ansible-vault encrypt_string 'SuperSecret123!' --name 'db_password'
# Output:
# db_password: !vault |
# $ANSIBLE_VAULT;1.1;AES256
# ...
# ============================================
# PART 5: THE SEPARATION CONVENTION
# ============================================
# group_vars/all/vars.yml
db_password: "{{ vault_db_password }}"
api_key: "{{ vault_api_key }}"
# ============================================
# PART 6: THE PASSWORD FILE
# ============================================
echo 'YourVaultPassword' > ~/.vault_pass
chmod 600 ~/.vault_pass
# The file is outside the repository.
# ============================================
# PART 7: THE ANSIBLE.CFG
# ============================================
[defaults]
vault_password_file = ~/.vault_pass
# ============================================
# PART 8: THE PLAYBOOK RUN
# ============================================
ansible-playbook -i inventory site.yml
# The password is read from the configured file.
# ============================================
# PART 9: THE REKEY
# ============================================
ansible-vault rekey group_vars/all/vault.yml
# The old password is required, then the new one.
# ============================================
# PART 10: THE NO_LOG TASK
# ============================================
# In the playbook:
- name: Configure database
ansible.builtin.template:
src: db.conf.j2
dest: /etc/app/db.conf
no_log: true
# Prevents the decrypted values from appearing in the output.
The ten parts covered file creation, viewing, encryption, string encryption, the separation convention, the password file, the ansible.cfg, the playbook run, the rekey, and the no_log task.
Quick Reference
ansible-vault subcommands
| Subcommand | Purpose |
|---|---|
create | Create a new encrypted file |
encrypt | Encrypt an existing file |
decrypt | Remove encryption |
view | View decrypted content |
edit | Edit and re-encrypt |
rekey | Change the password |
encrypt_string | Encrypt a single value |
Password sources
| Method | Flag / setting |
|---|---|
| Interactive prompt | --ask-vault-pass |
| Password file | --vault-password-file |
| Config default | vault_password_file in ansible.cfg |
| Environment variable | ANSIBLE_VAULT_PASSWORD_FILE |
| Executable script | Password file with execute permission |
Vault ID
| Item | Detail |
|---|---|
| Flag | --vault-id label@source |
| Header | The label appears in the encrypted file |
| Multiple IDs | Pass multiple --vault-id flags |
The separation convention
| File | Content | Committed |
|---|---|---|
vault.yml | Encrypted secrets | Yes |
vars.yml | Plaintext references | Yes |
~/.vault_pass | Password | No (gitignored) |
Best Practices
โ Do This:
# Keep the password file outside the repository
chmod 600 ~/.vault_pass # โ
# Separate encrypted secrets from plaintext variables
db_password: "{{ vault_db_password }}" # โ
# Use no_log on tasks that handle secrets
no_log: true # โ
# Use --vault-id for multiple environments
ansible-vault encrypt --vault-id prod@~/.vault_pass_prod # โ
โ Don’t Do This:
# Commit the password file
git add .vault_pass # โ
# Put the plaintext secret in vars.yml
db_password: "SuperSecret123!" # โ
# Print the secret in a debug task
- debug: var=db_password # โ
# Type the secret at the command line
ansible-vault encrypt_string 'secret' --name 'x' # โ (shell history)
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Vault password not found | No --ask-vault-pass or password file | Set vault_password_file in ansible.cfg |
| Secret appears in output | No no_log: true | Add the keyword to the task |
| Password file committed | .gitignore missing | Add the pattern and rotate the password |
| Wrong vault ID | Multiple IDs without labels | Use --vault-id label@source |
| Encrypted file is not readable | Wrong password | Check the password source |
Real-World Examples
1. Create an encrypted file
ansible-vault create group_vars/all/vault.yml
2. Encrypt an existing file
ansible-vault encrypt secrets.yml
3. View an encrypted file
ansible-vault view group_vars/all/vault.yml
4. Edit an encrypted file
ansible-vault edit group_vars/all/vault.yml
5. Encrypt a string
ansible-vault encrypt_string 'secret' --name 'db_password'
6. Password file
echo 'pass' > ~/.vault_pass && chmod 600 ~/.vault_pass
7. Config default
vault_password_file = ~/.vault_pass
8. Vault ID
ansible-vault encrypt --vault-id dev@prompt file.yml
9. No log
no_log: true
10. The separation
db_password: "{{ vault_db_password }}"
Visual
The at-rest protection
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PLAINTEXT โ
โ db_password: "SuperSecret123!" โ
โ โ โ
โ โผ โ
โ ansible-vault encrypt โ
โ โ โ
โ โผ โ
โ CIPHERTEXT (safe to commit) โ
โ $ANSIBLE_VAULT;1.1;AES256 โ
โ ... โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The password flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ~/.vault_pass (not committed) โ
โ โ โ
โ โผ โ
โ ansible.cfg: vault_password_file = ~/.vault_pass โ
โ โ โ
โ โผ โ
โ ansible-playbook decrypts the vault.yml โ
โ โ โ
โ โผ โ
โ The decrypted value is available to the play โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The separation convention
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ group_vars/all/ โ
โ โโโ vault.yml (encrypted) โ
โ โ โโโ vault_db_password: "..." โ
โ โ โ
โ โโโ vars.yml (plaintext) โ
โ โโโ db_password: "{{ vault_db_password }}" โ
โ โ
โ The playbook references db_password. โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The no_log boundary
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Task without no_log: โ
โ The decrypted value appears in the output. โ
โ โ
โ Task with no_log: true: โ
โ The output is suppressed. โ
โ The value is still decrypted in memory. โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Tool | ansible-vault |
| Encryption | AES-256 |
| Protection scope | Data at rest only |
| File encryption | create, encrypt, decrypt, edit, view, rekey |
| String encryption | encrypt_string --name |
| Password source | Prompt, file, config, env var, script |
| Multiple passwords | --vault-id label@source |
| Secret separation | vault.yml (encrypted) + vars.yml (references) |
| Output suppression | no_log: true |
Key takeaways:
- Vault protects data at rest, not data in use. The encrypted file is safe to commit, but the decrypted value is the playbook’s responsibility. Use
no_log: trueon tasks that handle secrets to prevent the value from appearing in the output . - The password must not be committed. The vault password is the key to the encrypted data. Store it in a file outside the repository, an environment variable, or a script that retrieves it from a secret manager. Add the file to
.gitignore. - The
encrypt_stringsubcommand encrypts a single value. It outputs a!vaultblock that can be pasted into a variables file. The--nameflag sets the variable name . - The separation convention keeps the encrypted secrets apart from the plaintext variables. The
vault.ymlfile contains the encrypted values with avault_prefix. Thevars.ymlfile contains the plaintext variables that reference them. The playbook uses the plaintext names . - The
--vault-idflag supports multiple passwords. Each ID has a label and a source. The label appears in the encrypted file’s header, and Ansible uses it to select the correct password when decrypting . - The password source can be a script. An executable password file is run by Ansible, and its standard output is used as the password. This is the mechanism for retrieving the password from a keyring or a secret manager at runtime .
Remember: Ansible Vault is a file-level encryption tool, not a secret manager. It protects the encrypted file at rest. It does not protect the decrypted value after the playbook starts. Keep the password outside the repository, separate the encrypted secrets from the plaintext variables, and use no_log on the tasks that handle secrets. The encryption is AES-256, the format is text-armored, and the password is the only thing standing between the ciphertext and the secret. Treat it accordingly.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!