Ansible Series

🔒 The Top 1% Hands-On for Never Leaving a Password in Plaintext in Git With Ansible Vault

Introduction

This article is part of the Top 1% Series Complete Article Guide, the 4th article in the Ansible Series.

Prerequisites

Why This Problem Happens in the First Place

Ansible’s variable files (group_vars and host_vars) are usually version-controlled in the same Git repository as the rest of the Playbook’s code. Write a secret like a database connection password into one of these in plaintext, and anyone with read access to that Git repository can read that secret. What makes this even trickier: once it’s committed to Git’s history, deleting the offending line later doesn’t help — anyone can still dig through the commit history and read that secret.

The Big Picture

This hands-on consists of four steps.

graph LR
    Step1["Step1<br/>Encrypt a variable file<br/>containing a secret"]
    Step2["Step2<br/>Inspect the encrypted<br/>file's contents"]
    Step3["Step3<br/>Run a Playbook using<br/>the vault password"]
    Step4["Step4<br/>Edit the encrypted<br/>file's contents"]
    Step1 --> Step2 --> Step3 --> Step4

Hands-On Steps

Step 1: Encrypt a variable file containing a secret

Create a variable file containing a database password. This hands-on doesn’t actually build a database server. The database password here is purely illustrative — a stand-in for “a typical secret a real-world Playbook has to handle.” What matters here isn’t the value’s actual content — it’s the sequence of steps you’re about to go through: “encrypt a variable file containing a secret.”

cat > db_secrets.yml << 'EOF'
db_password: "SuperSecretP@ssw0rd"
EOF

Encrypt this file with Ansible Vault.

ansible-vault encrypt db_secrets.yml

Running this prompts you for a password to use for encryption. Remember this password — you’ll need it every time you decrypt or edit this file going forward.

Step 2: Inspect the encrypted file’s contents

Run cat db_secrets.yml and check the file’s contents. You’ll find they’re now unreadable ciphertext starting with $ANSIBLE_VAULT. In this state, you can safely commit this file to a Git repository as-is, without leaking the secret.

Step 3: Run a Playbook using the vault password

To run a Playbook that reads this encrypted variable file, you need to additionally specify the vault password.

ansible-playbook -i inventory.ini site.yml --ask-vault-pass

Specifying --ask-vault-pass prompts you for the vault password at runtime, temporarily decrypts the content with it, and runs the Playbook. The decrypted content is never left on disk in plaintext. If typing this in manually every time is too tedious, you can instead save the vault password to a file and specify it with the --vault-password-file option (just don’t forget to add that file itself to .gitignore, so it never ends up in the Git repository).

Step 4: Edit the encrypted file’s contents

If you want to change the encrypted file’s contents later, you don’t need the tedious process of manually decrypting it and re-encrypting it afterward.

ansible-vault edit db_secrets.yml

This command bundles the entire flow — prompting for the vault password, opening the temporarily decrypted content in an editor, and automatically re-encrypting it once you save — into a single command.

What a Pro Sees Here (Top 1% Understanding)

A design that lets “encrypted” and “unencrypted” files safely coexist in a Git repository

In practice, rather than encrypting an entire Playbook, the common design is to carve out just the variables containing secrets into a dedicated encrypted file (often named something like vault.yml), while everything else — the regular Playbooks and task definitions — stays version-controlled unencrypted, as-is. This keeps the vast majority of the code able to go through normal Git diff review, while safely protecting only the secrets.

A common layout for this is group_vars/all/vault.yml. group_vars, covered in depth in The Top 1% Hands-On for Safely Running dev/staging/prod From One Ansible Playbook, is the mechanism for managing variables per inventory group, and group_vars/all/ is a special location within it for “variables common to every host.” A common convention is placing both an encrypted vault.yml (the secrets) and an unencrypted vars.yml (everything else) inside this same group_vars/all/, with vars.yml referencing the variables defined in vault.yml.

Using vault-id to manage multiple encryption passwords

As a more advanced technique, ansible-vault has a mechanism called vault-id. This lets you, for example, manage a “development environment encryption password” and a “production environment encryption password” separately, using files encrypted with different passwords side by side within the same repository.

ansible-vault encrypt --vault-id prod@prompt prod_secrets.yml
ansible-playbook site.yml --vault-id prod@prompt

This mechanism prevents the accident of someone who knows the development environment’s vault password accidentally being able to decrypt production secrets too. As a team grows and you want to restrict who can access which environment, this vault-id-based separation becomes practically important.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. ansible-playbook doesn’t prompt for the vault password at all: Check whether that file is actually encrypted, by confirming with cat that it starts with $ANSIBLE_VAULT.
  2. You entered the vault password, but decryption fails: If you’re managing multiple passwords with vault-id, check whether the vault-id you specified matches the password that file was actually encrypted with.
  3. The file specified with --vault-password-file accidentally got committed to Git: Immediately change that file’s contents (rotate the vault password itself), and consider also removing it from Git’s history.

Summary

Takeaways to Apply Today

  1. Whenever you need to put a secret into a Playbook, consider encrypting it with Ansible Vault before ever writing it in plaintext.
  2. If you use --vault-password-file, don’t forget to add that file itself to .gitignore.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).