online posts 16
ansible 101 for networking

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.

you edit, in the labinventory/group_vars/all/vault.ymlinventory/group_vars/fabric.ymlansible.cfg.gitignore

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
Vault password (lab only)netdoodles-vault, kept in .vault-pass
Secret to vaultvault_ansible_password: capeos-lab
Inline secretsnmp_community: nd-lab-ro-2026
1 explain

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:

  1. The secret, encrypted, in a vault file.
  2. 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.
  3. The key, in a file that’s never committed, and ansible.cfg pointing 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.

2 show

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.

3 follow along

The password, into a vault

  1. Write the key file, make it readable only by you, and check Git will never pick it up:

    run in the terminal
    echo netdoodles-vault > .vault-pass
    chmod 600 .vault-pass
    git check-ignore -v .vault-pass
    

    chmod 600 means only your user can read or write the file: it’s a key, so treat it like one. The last command prints the .gitignore rule that covers it. No output would mean it’s not ignored: stop and fix .gitignore before going on.

  2. Tell Ansible where the key is. Under [defaults]:

    editansible.cfgCtrl+P → ansible.cfg
    [defaults]
    vault_password_file = .vault-pass
    
  3. Create the encrypted file. ansible-vault create opens an editor:

    run in the terminal
    mkdir -p inventory/group_vars/all
    ansible-vault create inventory/group_vars/all/vault.yml
    

    Type 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.yml shows it decrypted.

  4. Replace the plain-text password with the reference:

    editinventory/group_vars/fabric.ymlCtrl+P → fabric.yml
    ansible_password: "{{ vault_ansible_password }}"   # from inventory/group_vars/all/vault.yml
    
  5. Find anything that still has the password: git grep capeos-lab. (The README, lab/ and lib/ will show up: they set the switches’ own login, and the checker allows them. Your inventory and playbooks shouldn’t.) Then make run T=7.1: the playbook runs, so Ansible decrypted the vault on its own.

4 challenge

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.1

when 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