• Back to the Cloud
  • Posts
  • πŸ” Ansible Vault β€” How to Secure Secrets in Your Playbooks

πŸ” Ansible Vault β€” How to Secure Secrets in Your Playbooks

Welcome back to β€œBack to the cloud” β€” your no-fluff guide to mastering DevOps, Cloud, and AI workflows, one skill at a time.

Last week we covered:
➑ Ansible Roles β€” Why and How to Use Them for Clean, Reusable Playbooks.
Missed it? Read it here.

🚨 Problem:

Hardcoding passwords in your playbooks is like taping your house key to the front door.

Plain text variables like this are risky:

vars:
  db_password: supersecret123

Anyone with access to your repo or logs can read it.

πŸ’‘ Solution: Ansible Vault

Ansible Vault allows you to encrypt:

  • Variables

  • Files

  • Entire playbooks

…so your secrets stay safe β€” even in version control.

πŸ’» How to Create an Encrypted File

1️⃣ Run this command:

ansible-vault create secrets.yml

2️⃣ Enter your secure variables:

db_password: supersecret123
api_key: ABCD-1234-EFGH

3️⃣ Save and exit.
This file is now encrypted!

πŸ”“ How to View or Edit Encrypted Files

  • View:

ansible-vault view secrets.yml
  • Edit:

ansible-vault edit secrets.yml
  • Re-key:

ansible-vault rekey secrets.yml

πŸš€ Using Vault Secrets in Playbooks

Reference the encrypted file like any normal vars file:

---
- name: Deploy App
  hosts: webservers
  vars_files:
    - secrets.yml
  tasks:
    - name: Print DB password
      debug:
        msg: "Password is {{ db_password }}"

Then run your playbook with:

ansible-playbook deploy.yml --ask-vault-pass

Or configure password-less vaults using environment variables or vault password files.

πŸ† Pro Tip:

Always encrypt sensitive data before committing to Git β€”
Vault makes this safe and easy.

🎯 When to Use Vault?

Scenario

Use Vault?

Passwords or API Keys

βœ… Yes

Infrastructure Config (no secrets)

❌ No

Cloud Credentials

βœ… Yes

Plaintext SSH Private Keys

βœ… Yes

πŸ”₯ Next Week:

β€œHow to Write Idempotent Ansible Tasks β€” Avoiding Common Mistakes.”
Learn how to write tasks that only change things when necessary.

πŸ’¬ Question for You:

What’s your biggest challenge in handling secrets securely?
Reply and let me know β€” I might include your question in an upcoming guide!