- 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!