Ansible Vault
Last updated: July 1, 2025
My Journey with Sensitive Data in Automation
Early in my automation journey, I made a critical mistake that still makes me cringe. I was working on automating server deployments and database configurations, and I had hardcoded passwords, API keys, and SSH private keys directly into my playbooks. Everything worked perfectly in development, and I was proud of my automation scripts.
Then came the moment of sharing. When I pushed my "brilliant" automation code to our Git repository, a colleague immediately pulled me aside. "You just committed database passwords and API keys to version control," he said quietly. My heart sank as I realized the security implications of what I'd done.
That incident taught me a valuable lesson about the importance of securing sensitive data in automation. It was also when I discovered Ansible Vault β a built-in encryption system that allows you to protect passwords, keys, and other sensitive information while still maintaining the power and flexibility of Ansible automation.
In this blog post, I'll share what I've learned about using Ansible Vault effectively across Linux and Windows environments. Whether you're managing database credentials, API tokens, or SSL certificates, mastering Ansible Vault is essential for secure automation.
Understanding Ansible Vault
Ansible Vault is a feature that allows you to encrypt sensitive data at rest. Instead of storing passwords and keys in plain text, Vault uses AES256 encryption to protect your sensitive information. The encrypted files can be safely stored in version control systems and shared with team members.
Key features of Ansible Vault:
File-level encryption - Encrypt entire files containing sensitive data
Variable-level encryption - Encrypt individual variables within files
Multiple password support - Use different passwords for different environments
Seamless integration - Works transparently with playbooks and roles
AES256 encryption - Industry-standard encryption for data security
Basic Vault Operations
Creating Encrypted Files
The simplest way to start with Ansible Vault is to create encrypted files from scratch:
This command opens your default editor with an empty file. When you save and exit, the file is automatically encrypted.
Encrypting Existing Files
You can also encrypt existing files:
Let's look at a practical example. Suppose you have a file with database credentials:
database_passwords.yml (before encryption)
After running ansible-vault encrypt database_passwords.yml, the file becomes:
Viewing Encrypted Content
To view the content of encrypted files without decrypting them permanently:
Decrypting Files
To permanently decrypt files (use with caution):
Linux Automation Examples
Example 1: Linux User Management with Encrypted Passwords
Let's create a comprehensive example for managing Linux users with encrypted password data:
user_secrets.yml (encrypted vault file)
linux_users.yml (main playbook)
Example 2: SSL Certificate Management
ssl_secrets.yml (encrypted vault file)
ssl_setup.yml
Windows Automation Examples
Example 3: Windows Service Account Management
windows_secrets.yml (encrypted vault file)
windows_services.yml
Example 4: Windows Registry and Certificate Management
windows_security_secrets.yml (encrypted vault file)
windows_security.yml
Sequence Diagram: Ansible Vault Workflow
Here's a sequence diagram illustrating how Ansible Vault processes encrypted content during playbook execution:
This diagram shows the critical security aspects:
Passwords are only in memory during execution
Encrypted files remain encrypted on disk
Data transmission uses encrypted channels
Memory is cleared after execution
Managing Vault Passwords
Single Password Approach
For simple environments, you can use a single vault password:
Multiple Vault IDs
For more complex environments, use vault IDs to manage different passwords:
Password Scripts
You can use scripts to retrieve passwords from external systems:
vault_password_script.py
Usage:
Variable-Level Encryption
Instead of encrypting entire files, you can encrypt individual variables:
This produces output like:
You can then use this directly in your playbooks:
Advanced Vault Techniques
Vault in Roles
You can use vault-encrypted files within roles:
roles/database/vars/vault.yml (encrypted)
roles/database/tasks/main.yml
Rekey Operations
Change vault passwords when needed:
Vault and CI/CD Integration
For automated deployments, integrate vault passwords securely:
GitLab CI Example (.gitlab-ci.yml)
Configuration File Integration
Set default vault behavior in ansible.cfg:
Best Practices and Security Considerations
Security Best Practices
Use Strong Passwords: Vault passwords should be complex and unique
Rotate Passwords Regularly: Use the rekey feature to change passwords periodically
Separate Environments: Use different vault passwords for dev, staging, and production
Limit Access: Only grant vault password access to authorized personnel
Monitor Usage: Log and audit vault password usage
Development Workflow
Never commit vault passwords: Add password files to
.gitignoreUse vault IDs: Organize secrets by environment or purpose
Document requirements: Clearly indicate which vault passwords are needed
Test encryption: Verify that sensitive data is properly encrypted before committing
Common Pitfalls to Avoid
Don't use weak passwords: Avoid simple or predictable vault passwords
Don't share passwords insecurely: Use secure channels for password distribution
Don't decrypt permanently: Avoid using
ansible-vault decryptunless absolutely necessaryDon't log sensitive output: Use
no_log: truefor tasks handling sensitive data
Real-World Multi-Environment Example
Here's a comprehensive example showing how to manage multiple environments with different vault configurations:
Directory Structure
site.yml
Deployment Commands
Conclusion
Ansible Vault has become an indispensable part of my automation toolkit. What started as a lesson learned from accidentally committing sensitive data has evolved into a comprehensive security practice that protects credentials across all my automation projects.
The beauty of Ansible Vault lies in its simplicity and seamless integration. You don't need to fundamentally change how you write playbooks or structure your automation. Instead, you simply encrypt the sensitive parts and provide the appropriate passwords during execution.
Remember that security is not just about the tools you use, but also about the processes and practices you follow. Use strong passwords, rotate them regularly, separate environments appropriately, and always follow the principle of least privilege when sharing vault access.
As you implement Ansible Vault in your own projects, start small with a few encrypted variables and gradually expand to more complex multi-environment setups. The investment in learning these security practices will pay dividends in the long run, protecting both your infrastructure and your organization from security breaches.
Last updated