task 7.1 · chapter 7 of 8: secrets · 25 min
Vault the password
objective
Get the switch password out of plain text: an encrypted vault file, a reference to it, a key that never reaches Git, and one value encrypted inline.
inventory/group_vars/all/vault.ymlinventory/group_vars/fabric.ymlansible.cfg.gitignoreopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Vault password (lab only) | netdoodles-vault, kept in .vault-pass |
|---|---|
| Secret to vault | vault_ansible_password: capeos-lab |
| Inline secret | snmp_community: nd-lab-ro-2026 |
What is Ansible Vault?
Right now the switch password sits in plain text in group_vars/fabric.yml. Commit that and it’s in Git’s
history forever, for everyone with access to the repo.
Ansible Vault encrypts values with a password (the vault password, or key). Ansible decrypts them in memory when a playbook runs; the files in Git stay encrypted. Three pieces:
- The secret, encrypted, in a vault file.
- A reference to it, in plain text, where the setting used to be. Anyone reading the repo can see that there’s a password and where it comes from, but not what it is.
- The key, in a file that’s never committed, and
ansible.cfgpointing at it.
There are two ways to encrypt: a whole file (ansible-vault create), or a single value inside a normal
file (ansible-vault encrypt_string). You’ll use both.
How does the vault fit together?
.vault-pass netdoodles-vault ← the key. git-ignored. Never committed.
ansible.cfg vault_password_file = .vault-pass ← where Ansible finds the key
inventory/group_vars/all/vault.yml $ANSIBLE_VAULT;1.1;AES256 ... ← the encrypted secret
(decrypted: vault_ansible_password: capeos-lab)
inventory/group_vars/fabric.yml ansible_password: "{{ vault_ansible_password }}" ← the reference
The naming convention, vault_ + the setting’s name, makes it obvious where a value comes from. The vault file is
under group_vars/all/, a folder rather than a file: every host gets the variables of every file in it.
The password, into a vault
Write the key file, make it readable only by you, and check Git will never pick it up:
run in the terminalecho netdoodles-vault > .vault-pass chmod 600 .vault-pass git check-ignore -v .vault-passchmod 600means only your user can read or write the file: it’s a key, so treat it like one. The last command prints the.gitignorerule that covers it. No output would mean it’s not ignored: stop and fix.gitignorebefore going on.Tell Ansible where the key is. Under
[defaults]:editansible.cfgCtrl+P → ansible.cfg[defaults] vault_password_file = .vault-passCreate the encrypted file.
ansible-vault createopens an editor:run in the terminalmkdir -p inventory/group_vars/all ansible-vault create inventory/group_vars/all/vault.ymlType one line,
vault_ansible_password: capeos-lab, then save and exit (in nano: Ctrl+O, Enter, Ctrl+X). Open the file in VS Code: it’s a block of numbers starting$ANSIBLE_VAULT.ansible-vault view inventory/group_vars/all/vault.ymlshows it decrypted.Replace the plain-text password with the reference:
editinventory/group_vars/fabric.ymlCtrl+P → fabric.ymlansible_password: "{{ vault_ansible_password }}" # from inventory/group_vars/all/vault.ymlFind anything that still has the password:
git grep capeos-lab. (The README,lab/andlib/will show up: they set the switches’ own login, and the checker allows them. Your inventory and playbooks shouldn’t.) Thenmake run T=7.1: the playbook runs, so Ansible decrypted the vault on its own.
One value, encrypted inline
The fabric needs an SNMP read-only community, nd-lab-ro-2026. Put it in group_vars/fabric.yml as
snmp_community, but this time encrypt just that value, inline, with ansible-vault encrypt_string, so
the rest of the file stays readable in a diff. Then prove Ansible can read it.
hint
ansible-vault encrypt_string 'the-secret' --name snmp_community prints a ready-to-paste YAML block starting
snmp_community: !vault |. Paste it into the file as it is.
hint
To read it back: ansible spine1 -m ansible.builtin.debug -a var=snmp_community.
show the solutioninventory/group_vars/fabric.yml
or pull it into your lab: make solve T=7.1, then git diff --staged shows what changed
ansible-vault encrypt_string 'nd-lab-ro-2026' --name snmp_community
# inventory/group_vars/fabric.yml (your ciphertext will be different)
snmp_community: !vault |
$ANSIBLE_VAULT;1.1;AES256
6639656461663766613234326663303264643966326165613765323362656533...
5 check: run it, then prove it
$ make run T=7.1
$ make check T=7.1when it's right, you'll see
╭─ task 7.1 · vault the password
│ ✓ the switch password isn't in any file you commit
│ ✓ inventory/group_vars/all/vault.yml is encrypted
│ ✓ inventory/group_vars/fabric.yml gets ansible_password from the vault
│ ✓ .vault-pass is git-ignored
│ ✓ the challenge: snmp_community is in group_vars/fabric.yml, encrypted inline (ansible-vault encrypt_string)
│ ✓ ...and Ansible decrypts it
│ ✓ playbooks/site.yml runs cleanly
╰─ 7/7 checks passed verified
background: part 7, The vault_ pattern