Ansible Vault is a feature that allows you to keep sensitive data such as passwords or keys in encrypted files, rather than as plaintext in playbooks or roles. This is crucial for maintaining security and confidentiality in your automation scripts.
Key Concepts
- Encryption and Decryption: Ansible Vault can encrypt and decrypt files using a password or a key file.
- Vault IDs: Allows you to manage multiple vault passwords.
- Vault Files: Files that are encrypted using Ansible Vault.
Why Use Ansible Vault?
- Security: Protect sensitive information from being exposed.
- Compliance: Meet security compliance requirements by encrypting sensitive data.
- Ease of Use: Integrates seamlessly with Ansible playbooks and roles.
Basic Commands
Encrypting a File
To encrypt a file using Ansible Vault, use the following command:
Example:
Decrypting a File
To decrypt a file, use:
Example:
Editing an Encrypted File
To edit an encrypted file, use:
Example:
Viewing an Encrypted File
To view the contents of an encrypted file without editing, use:
Example:
Re-keying an Encrypted File
To change the password of an encrypted file, use:
Example:
Using Vault in Playbooks
You can use encrypted files in your playbooks by specifying the vault password file or prompting for the password.
Example Playbook
--- - name: Example Playbook with Vault hosts: all vars_files: - secrets.yml tasks: - name: Print secret message debug: msg: "{{ secret_message }}"
Running the Playbook
To run the playbook with a vault password file:
To run the playbook and be prompted for the vault password:
Practical Exercise
Exercise: Encrypting and Using a Secret File
-
Create a file named
secrets.yml
with the following content:secret_message: "This is a secret message"
-
Encrypt the file using Ansible Vault:
ansible-vault encrypt secrets.yml
-
Create a playbook named
vault_playbook.yml
that uses the encrypted file:--- - name: Playbook using Vault hosts: localhost vars_files: - secrets.yml tasks: - name: Print the secret message debug: msg: "{{ secret_message }}"
-
Run the playbook and provide the vault password when prompted:
ansible-playbook vault_playbook.yml --ask-vault-pass
Solution
-
Encrypting the file:
ansible-vault encrypt secrets.yml
-
Playbook content (
vault_playbook.yml
):--- - name: Playbook using Vault hosts: localhost vars_files: - secrets.yml tasks: - name: Print the secret message debug: msg: "{{ secret_message }}"
-
Running the playbook:
ansible-playbook vault_playbook.yml --ask-vault-pass
Common Mistakes and Tips
- Forgetting to encrypt files: Always ensure sensitive files are encrypted before committing them to version control.
- Misplacing the vault password: Store the vault password securely and avoid hardcoding it in scripts.
- Using multiple vault passwords: Use Vault IDs to manage multiple vault passwords effectively.
Conclusion
Ansible Vault is a powerful tool for securing sensitive data in your automation scripts. By understanding how to encrypt, decrypt, and use vault files in playbooks, you can enhance the security of your Ansible projects. Practice encrypting and using vault files to become proficient in managing sensitive information securely.
Ansible: From Beginner to Advanced
Module 1: Introduction to Ansible
Module 2: Ansible Basics
Module 3: Playbooks
- Introduction to Playbooks
- Writing Your First Playbook
- Playbook Structure
- Variables and Facts
- Conditionals and Loops
Module 4: Roles
Module 5: Advanced Playbook Techniques
Module 6: Ansible Galaxy
Module 7: Ansible Tower
- Introduction to Ansible Tower
- Installing Ansible Tower
- Using Ansible Tower
- Managing Projects and Inventories