Ansible Vault.
Playbooks belong in git β passwords don't. Ansible Vault encrypts secrets with AES256 so the ciphertext is safe to commit, then decrypts them in memory the moment a play runs. Watch a secrets file flip between plaintext and an encrypted blob, and learn to run with the vault password.
Ansible Vault animated tutorial. Encrypting and editing secrets files with ansible-vault create, encrypt, edit, view, decrypt, and rekey, running playbooks with the vault password via --ask-vault-pass, --vault-password-file, and --vault-id, encrypt_string for a single inline secret, the $ANSIBLE_VAULT;1.1;AES256 header, group_vars/all/vault.yml convention, and password file hygiene with chmod 600. RHCE EX294 ready.Passwords and tokens can't live in plaintext YAML if your playbooks are in git. Watch a secrets file flip between readable plaintext and an AES256 vault blob, then get decrypted in memory the moment a play runs.
- Β·Comfortable writing and running Ansible playbooks
- Β·Know what a vars file / group_vars looks like
- Β·Basic git + file permissions (chmod 600)
Encrypt and edit vault files, run playbooks that supply the vault password, encrypt a single value with encrypt_string, and lay out vaults with vault-id and good password hygiene.
pace: 8 minutes
π secrets.ymlβ οΈ plaintextWhy Vault β secrets don't belong in plaintext
Playbooks live in git, but passwords, tokens, and keys must not sit there in the clear. Ansible Vault encrypts files (or single values) with a password, using AES256, so the ciphertext is safe to commit. The file stays valid YAML β only the readable secret is replaced by an opaque vault blob. Encrypt a brand-new file with `create`, or wrap an existing one with `encrypt`.
$ansible-vault create secrets.yml$ansible-vault encrypt existing.yml$head -1 secrets.ymlVault lets you commit secrets to git safely β the file stays valid YAML, but the sensitive value is an AES256 blob until a password unlocks it at runtime. Encrypt a whole file with `create`/`encrypt`, or just one value with `encrypt_string`; either way Ansible decrypts transparently when you supply the password.
$ansible-vault create FILE$ansible-vault encrypt FILE$ansible-vault decrypt FILE$ansible-vault view FILE$ansible-vault edit FILE$ansible-vault rekey FILE$ansible-vault encrypt_string 'val' --name 'k'$ansible-playbook --ask-vault-pass$--vault-password-file FILE$--vault-id label@source$vars_files: [secrets.yml]$group_vars/all/vault.yml$$ANSIBLE_VAULT;1.1;AES256$chmod 600 ~/.vault_passWhat does Ansible Vault protect, and how?
You need to run a playbook whose vars_files includes a vaulted file. What must you provide?
You want ONE encrypted value inside an otherwise-readable vars file. Which command?
What's the difference between `ansible-vault view` and `ansible-vault decrypt`?
These aren't graded β they're just for active recall, which is what actually makes the lesson stick.
Ansible error handling
Plays don't always go to plan. Learn block / rescue / always and failed_when to catch failures, run cleanup, and decide for yourself what "failed" even means.
Read this lesson as text5 chapters Β· quiz answers
01Why Vault β secrets don't belong in plaintext
Playbooks live in git, but passwords, tokens, and keys must not sit there in the clear. Ansible Vault encrypts files (or single values) with a password, using AES256, so the ciphertext is safe to commit. The file stays valid YAML β only the readable secret is replaced by an opaque vault blob. Encrypt a brand-new file with
create, or wrap an existing one withencrypt.$ ansible-vault create secrets.ymlβ new encrypted file in $EDITOR$ ansible-vault encrypt existing.ymlβ encrypt a file you already have$ head -1 secrets.ymlβ file stays valid YAML; value is the blob
02Create, edit, view, decrypt
createmakes a new encrypted file;editopens it in $EDITOR decrypted, then re-encrypts on save.viewprints the plaintext without editing, anddecrypt/encryptconvert the file permanently.rekeychanges the vault password without exposing the contents. The whole lifecycle never leaves a plaintext copy on disk unless you ask for one.$ ansible-vault edit secrets.ymlβ decrypt β edit β re-encrypt on save$ ansible-vault view secrets.ymlβ print plaintext, leave file encrypted$ ansible-vault rekey secrets.ymlβ change the vault password
03Run a playbook that uses vaulted vars
Ansible needs the password at runtime. Supply it with
--ask-vault-pass(interactive prompt),--vault-password-file FILE(a file or executable script that prints the password), or a labelled--vault-id. Reference the encrypted file the same way as any other vars βvars_files:or group_vars. Ansible decrypts it in memory as the play runs and the secret resolves to its plaintext value.$ ansible-playbook site.yml --ask-vault-passβ prompt for the password$ ansible-playbook site.yml --vault-password-file ~/.vault_passβ read it from a file$ vars_files:\n - secrets.ymlβ reference the vaulted file
04encrypt_string β one secret inline
You don't have to encrypt a whole file β
ansible-vault encrypt_stringproduces an encrypted scalar you paste into an otherwise-plaintext vars file. That's handy for mixing one secret into readable vars without hiding the rest. The output is a!vaulttagged block under your key name. Ansible decrypts it transparently at run, exactly like a fully encrypted file.$ ansible-vault encrypt_string 's3cret' --name 'db_password'β make an encrypted scalar$ db_password: !vault |\n $ANSIBLE_VAULT;1.1;AES256β paste under vars:$ ansible-playbook site.yml --ask-vault-passβ decrypts automatically at run
05Best practice β vault-id & password hygiene
Use
--vault-id label@sourceto manage multiple vaults β e.g. dev vs prod β each with its own password. Keep the password file mode 600 and OUT of git so the ciphertext is the only thing committed. The conventional layout pairs a vaulted file beside a readable one: group_vars/all/vault.yml holds the secrets, group_vars/all/vars.yml holds everything else.$ ansible-playbook site.yml --vault-id prod@promptβ labelled vault for prod$ chmod 600 ~/.vault_passβ lock down the password file$ group_vars/all/vault.ymlβ convention: vaulted vars beside vars.yml
Quiz β answers
- What does Ansible Vault protect, and how?
- βIt encrypts sensitive data at rest with AES256 so files can be committed safely; it's decrypted only at runtime when you supply the password.Vault encrypts the data at rest (AES256). The ciphertext is safe to commit; Ansible only decrypts it in memory at runtime when the correct password is provided. Base64 is encoding, not encryption β Vault is real cryptography.
- You need to run a playbook whose vars_files includes a vaulted file. What must you provide?
- βThe vault password β via --ask-vault-pass, --vault-password-file, or --vault-id.Ansible can't decrypt the file without the vault password. Supply it interactively (--ask-vault-pass), from a file or script (--vault-password-file), or with a labelled --vault-id. Without it the play fails to decrypt.
- You want ONE encrypted value inside an otherwise-readable vars file. Which command?
- βansible-vault encrypt_string
encrypt_stringproduces a single encrypted scalar (a !vault block) you paste under one key, leaving the rest of the vars file readable.encrypt/createwould hide the whole file. - What's the difference between
ansible-vault viewandansible-vault decrypt? - β
viewprints the plaintext but leaves the file encrypted on disk;decryptpermanently rewrites the file back to plaintext.viewis read-only: it shows the plaintext but the file on disk stays encrypted.decryptis destructive in the sense that it writes the file back as plaintext β you'd then need toencryptit again before committing.
keep going β these pair well with what you just learned.
Ansible control node & inventory
Ansible is agentless β one control node pushes work to a fleet over SSH. Install ansible-core, write an inventory with groups, ping the fleet, and fire ad-hoc commands. RHCE-ready.
Ansible playbooks & YAML
Plays, tasks, and idempotency. Watch a playbook run task-by-task β ok, changed, skipped β then run it again and watch everything go green with 0 changed. RHCE-ready.
Ansible core modules
file, user, copy, dnf, service β the modules that do the real work. Declare the desired state and let Ansible converge to it, idempotently. RHCE-ready.