Ansible Vault — Secrets Management
Ansible
Chapter 8 · Ansible Vault — Secrets Management
ansible1-7 gave every concern its own tidy, version-controlled home. But roles and playbooks are naturally full of exactly the values pipelines1-5 and crypto1-11 both already warned about — database passwords, API keys, private keys. This chapter is the mechanism that lets those values live in version control safely, following the rule those earlier chapters already established.
The Problem — Roles and Playbooks Are Full of Secrets
Database passwords, API keys, TLS private keys — all naturally want to live as ordinary Ansible variables, in exactly the same files ansible1-7 just organized. But pipelines1-5's own rule stands regardless of which tool is committing the file: a credential committed to git in plaintext is compromised forever, even if it's later removed, since git history retains it. Ansible Vault is the tool for encrypting exactly these values, so they can live safely in version control alongside everything else — encrypted, not absent.
Encrypting a Whole File
edit never writes the plaintext to disk at any point during the process — it decrypts into memory, opens your editor, and re-encrypts on save. An encrypted file on disk looks genuinely unreadable:
Safe to commit, safe to diff (though the diff itself is meaningless without the password), and structurally no different from any other file in the repository.
Encrypting a Single Variable — encrypt_string
This produces a small, self-contained encrypted block that can be pasted directly into an otherwise-plaintext vars file. Often preferable to encrypting the whole file: the rest of the file — variable names, structure, non-sensitive values — stays readable in a normal diff, with only the genuinely sensitive value itself hidden.
Providing the Vault Password to Run a Playbook
--ask-vault-pass prompts interactively; --vault-password-file points at a file (or an executable script that outputs the password) — the practical option for CI, where nothing can be typed interactively. Running a playbook that references encrypted variables without supplying the password fails cleanly, with a clear error — not a silent, wrong value quietly used instead.
Vault IDs — Multiple Passwords for Multiple Environments
A light touch, worth naming: vault IDs let genuinely different environments use genuinely different vault passwords. Someone who only has the dev password structurally cannot decrypt production secrets — a real, meaningful security boundary between environments, not just a naming convention.
Directly Extending pipelines1-5/crypto1-11's Own Rule
pipelines1-5 named "a committed credential is compromised forever" as the reason never to commit secrets in plaintext; crypto1-11 covered key management practice more broadly. Ansible Vault is the concrete mechanism that lets Ansible-managed secrets follow that exact rule while still living in version control the same way everything else in this course does — ansible1-7's own roles, and the same discipline tf1's own state-file material already applied to Terraform's own sensitive values.
| Diff readability | Granularity | |
|---|---|---|
| Encrypt whole file | Diff is opaque ciphertext, no visible structure | All-or-nothing — every value in the file is hidden |
| encrypt_string | Surrounding structure/keys stay readable in diffs | Per-value — only the sensitive value itself is hidden |
pipelines1-5 already covered. The vault password sitting in the same repository (or worse, the same commit history) as the encrypted data it protects defeats the entire point.
Hands-On Exercises
Write the command that would encrypt an existing plaintext file named db_vars.yml in place, and explain what the file looks like on disk immediately afterward.
📄 View solutionA vars file has ten ordinary configuration variables and one API key. Explain, using this chapter's own compare-table, why encrypt_string would likely be the better choice here over encrypting the whole file.
📄 View solutionA CI pipeline needs to run a playbook that references vault-encrypted variables, with no human available to type a password interactively. Explain which vault option is appropriate here, and where the password itself should actually be stored.
📄 View solutionChapter 8 Quick Reference
- ansible-vault create/encrypt/edit/view — encrypt/decrypt whole files; edit never writes plaintext to disk
- encrypt_string — encrypt one variable inline, keeping the rest of a file's diff readable
- --ask-vault-pass (interactive) vs. --vault-password-file (CI-friendly) — how a playbook gets the password to decrypt
- Vault IDs let different environments use genuinely different passwords — a real security boundary, not just a label
- Ansible Vault is the concrete mechanism letting secrets follow pipelines1-5/crypto1-11's own never-commit-plaintext rule while still living in version control
- Never store the vault password near the repo it protects — a password manager or a protected CI secret, never the same commit history
- Losing the vault password permanently loses the secrets — there is no recovery mechanism