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

  1. Encryption and Decryption: Ansible Vault can encrypt and decrypt files using a password or a key file.
  2. Vault IDs: Allows you to manage multiple vault passwords.
  3. 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:

ansible-vault encrypt <filename>

Example:

ansible-vault encrypt secrets.yml

Decrypting a File

To decrypt a file, use:

ansible-vault decrypt <filename>

Example:

ansible-vault decrypt secrets.yml

Editing an Encrypted File

To edit an encrypted file, use:

ansible-vault edit <filename>

Example:

ansible-vault edit secrets.yml

Viewing an Encrypted File

To view the contents of an encrypted file without editing, use:

ansible-vault view <filename>

Example:

ansible-vault view secrets.yml

Re-keying an Encrypted File

To change the password of an encrypted file, use:

ansible-vault rekey <filename>

Example:

ansible-vault rekey secrets.yml

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:

ansible-playbook playbook.yml --vault-password-file .vault_pass.txt

To run the playbook and be prompted for the vault password:

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

Practical Exercise

Exercise: Encrypting and Using a Secret File

  1. Create a file named secrets.yml with the following content:

    secret_message: "This is a secret message"
    
  2. Encrypt the file using Ansible Vault:

    ansible-vault encrypt secrets.yml
    
  3. 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 }}"
    
  4. Run the playbook and provide the vault password when prompted:

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

Solution

  1. Encrypting the file:

    ansible-vault encrypt secrets.yml
    
  2. 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 }}"
    
  3. 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.

© Copyright 2024. All rights reserved