Ansible Vault: Securing Your Automation Secrets
In the world of infrastructure automation, security remains a paramount concern. As organizations increasingly adopt infrastructure as code practices, sensitive information like API keys, passwords, and certificates must be handled carefully.
Ansible Vault provides a robust solution to this challenge, allowing you to encrypt sensitive data while keeping it seamlessly integrated with your automation workflows.
Understanding Ansible Vault
At its core, Ansible Vault is a feature built into Ansible that provides encryption and decryption services for sensitive data. It uses AES-256 encryption to protect files, ensuring that unauthorized users cannot access your secrets.
Ansible Vault works by encrypting files or individual variables with a password. During playbook execution, Ansible automatically decrypts this information when needed, making the encryption process transparent to your automation workflows.
What makes Ansible Vault particularly powerful is its deep integration with the broader Ansible ecosystem. You can use encrypted content anywhere you would use unencrypted content in your playbooks, roles, or variable files. This seamless integration ensures that adding security doesn't increase complexity or reduce functionality.
Key benefits of Ansible Vault include:
- Simple implementation: Requires minimal additional setup as it's built into Ansible.
- Flexible encryption: Can encrypt entire files or individual variables.
- Secure storage: Allows sensitive data to be safely committed to version control.
- Transparent usage: Encrypted data works seamlessly with existing Ansible functionality.
Getting started with Ansible Vault
Ansible Vault comes built into Ansible, so if you have Ansible installed, you already have access to Vault. To confirm your Ansible installation includes Vault, you can check your Ansible version:
Ansible Vault commands follow a consistent structure:
Common actions include:
create: Create a new encrypted file.encrypt: Encrypt an existing file.decrypt: Decrypt an encrypted file.edit: Edit an encrypted file.view: View the contents of an encrypted file.rekey: Change the encryption password.
Let's create a new encrypted file to store database credentials:
When you run this command, Ansible will prompt you to create and confirm a password. After entering a password, your default editor will open, allowing you to add content to the file.
After saving and closing the editor, your file will be encrypted automatically.
If you try to view the file using a regular text editor or command like cat,
you'll see only encrypted content:
This encrypted format ensures that your sensitive information remains protected while at rest. The encrypted file contains the AES256 cipher, a salt, and the actual encrypted data.
Core operations
Let's explore the essential operations you can perform with Ansible Vault. If
you already have configuration files with sensitive data, you can encrypt them
using the encrypt command:
This command will prompt you for a password and then encrypt the file in place, replacing the original unencrypted file with the encrypted version. For example, let's say you have an existing file with API credentials:
After running the encrypt command, the file will be transformed into an encrypted version that's safe to store in version control.
You can encrypt multiple files simultaneously by providing multiple file paths:
This is particularly useful when setting up a new project with multiple configuration files containing sensitive information.
To convert an encrypted file back to plaintext, use the decrypt command:
You'll be prompted for the vault password, and upon successful authentication, the file will be decrypted in place. Be cautious with this command, as it removes the encryption protection from your sensitive data.
Decryption is typically used when you need to make significant changes to a file or when you're transitioning to a different security approach.
Editing encrypted files
Instead of decrypting a file to edit it and then re-encrypting it afterward (which creates a window of vulnerability), Ansible Vault provides a direct editing capability:
This command will:
- Prompt you for the vault password.
- Decrypt the file in memory (not on disk).
- Open the decrypted content in your default editor.
- Re-encrypt the file automatically when you save and close the editor.
This approach ensures your sensitive data is never written to disk in an unencrypted form during the editing process.
Viewing encrypted content
When you need to check the contents of an encrypted file without editing it, the
view command is ideal:
Similar to the edit command, this will decrypt the file in memory and display its contents without writing the decrypted version to disk.
Changing the encryption password
Security best practices often recommend periodic password rotation. To change the password used to encrypt a vault file:
You'll be prompted to enter the current password and then to create and confirm a new password. The file will be re-encrypted with the new password while the content remains unchanged.
Working with vault passwords
Managing vault passwords effectively is critical to maintaining both security and operational efficiency.
Single password management
The simplest approach is to use a single password for all your vault-encrypted files. While this is convenient, it provides limited security isolation. If you use this approach, you can avoid repeatedly typing the password by storing it in a file:
Ensure that you set restrictive permissions on this file to prevent unauthorized access. Now, you can reference this password file in your commands:
You can also configure Ansible to use this password file automatically by adding
the following to your ansible.cfg:
With this configuration, you won't need to specify the password file in each command.
Password files vs. password scripts
Instead of storing the vault password in a plaintext file (which has obvious security implications), you can use a script that retrieves the password dynamically. For example, you might create a script that fetches the password from a secure password manager:
Make the script executable:
Then use it with vault commands:
This approach adds an extra layer of security by avoiding storage of the vault password in plaintext.
Multiple vault password strategies
As your infrastructure grows, you might need different levels of security for different environments or types of secrets. Ansible Vault supports multiple password strategies through Vault IDs.
First, create separate password files for each environment:
Now you can create or encrypt files with specific Vault IDs:
The dev@ and prod@ prefixes are Vault IDs that identify which password
should be used for which file.
Vault ID implementation
You can configure multiple Vault IDs in your ansible.cfg file:
With this configuration, Ansible will try each password in order when decrypting files. During encryption, you'll need to specify which ID to use:
This approach allows for more sophisticated secret management strategies while maintaining the simplicity of the Ansible Vault interface.
Here's how to use multiple vault passwords in a playbook:
When running this playbook, you would specify which environment to use:
This command tells Ansible to use the dev_secrets.yml file and decrypt it
using the password stored in ~/.vault_pass.dev.
Using encrypted content in playbooks
Now that we understand how to manage encrypted files, let's explore how to use them effectively in Ansible playbooks.
Including encrypted files in playbooks
The simplest way to use encrypted content is to include encrypted files in your
playbooks using vars_files:
When executing this playbook with the correct vault password provided, Ansible
will automatically decrypt the secret_vars.yml file and make its variables
available to the playbook.
Running playbooks with vault-encrypted files
When running a playbook that uses encrypted files, you need to provide the vault password:
This command will prompt you for the vault password before executing the playbook. Alternatively, you can use a password file:
If you've configured a vault_password_file in your ansible.cfg, you don't
need to specify it in the command.
Handling encrypted files in roles
When organizing your Ansible code into roles, you might need to include encrypted files within role directories. Here's a typical structure:
You can reference encrypted files in your tasks:
And include encrypted variables:
Variable-level encryption
Instead of encrypting entire files, Ansible Vault allows you to encrypt individual variables within a file. This approach is useful when only a few variables in a file contain sensitive information.
To encrypt a single variable, use the ansible-vault encrypt_string command:
You can then include this encrypted variable in a YAML file:
This file can be committed to version control with only the password encrypted, making it easier to track changes to non-sensitive variables.
Using encrypted strings in inventory
You can also use encrypted strings in your inventory files to protect sensitive host-specific variables:
This approach is particularly useful for environment-specific credentials that need to be associated with specific inventory groups.
Using Ansible Vault in practical scenarios
Let's explore some practical scenarios where Ansible Vault proves invaluable.
Securing database credentials
One of the most common use cases for Ansible Vault is protecting database credentials used in application deployments:
You can use these variables in a database setup playbook:
Managing SSL certificates
Another common scenario is managing SSL certificates and private keys:
These can be used in a web server configuration playbook:
Securing API keys
API keys and tokens used for external service integration are perfect candidates for vault encryption:
You can use these in a deployment playbook:
Final thoughts
Ansible Vault provides a powerful yet straightforward approach to securing sensitive data within your automation workflows.
By encrypting either entire files or individual variables, you can protect your secrets while still benefiting from the full capabilities of Ansible.
Thanks for reading!