## Managing Secrets with Ansible Vault
Ansible Vault is a feature that allows you to encrypt sensitive data such as passwords, API keys, and other confidential information within your Ansible projects. This ensures that sensitive data is not stored in plain text, making your automation more secure, especially when storing playbooks in version control systems.
Core Vault Commands
- `ansible-vault create <filename>`: Use this command to create a new, empty file that will be immediately encrypted. You'll be prompted to set a new vault password. The file will open in your default editor for content entry.
- `ansible-vault edit <filename>`: This command decrypts an existing vault-encrypted file, opens it in your editor, and re-encrypts it upon saving and exiting. This is the primary way to modify encrypted data.
- `ansible-vault view <filename>`: To inspect the contents of an encrypted file without editing it, use `view`. It decrypts the file and displays its content to standard output, then exits without saving.
- `ansible-vault encrypt <filename>`: Encrypts an *existing* unencrypted file in place. The original file is replaced by its encrypted version.
- `ansible-vault decrypt <filename>`: Decrypts an *existing* encrypted file in place, reverting it to plain text. Use with caution.
- `ansible-vault rekey <filename>`: This command allows you to change the password of an already encrypted vault file. You'll be prompted for the old password and then the new one.
- `ansible-vault encrypt_string`: This is useful for encrypting individual strings directly from the command line, which can then be pasted into `vars` files. For example: `ansible-vault encrypt_string --name 'db_password' 'mysecretpassword'`
Managing Vault Passwords
To interact with encrypted files, Ansible needs the vault password. You can provide it in several ways:
- `--ask-vault-pass`: Prompts you for the password interactively during execution.
- `--vault-password-file /path/to/vault_pass.txt`: Specifies a file containing the vault password. The file should contain only the password and have strict permissions (e.g., `chmod 0600`).
- `ansible.cfg`: You can configure `vault_password_file = /path/to/vault_pass.txt` in your `ansible.cfg` to automatically use a password file.
Using Vault in Playbooks
Encrypted files can be included in playbooks just like regular variable files. For example, if you have `group_vars/all/secrets.yml` encrypted with vault, Ansible will automatically prompt for the password (or use a configured password file) when it needs to access variables from that file. Variables defined within these encrypted files can then be used in your playbooks and templates.
Best Practices
- Never commit vault password files to version control systems (like Git). Use `.gitignore` to exclude them.
- Ensure vault password files have strict file permissions (e.g., `0600`) to prevent unauthorized access.
- Use different vault passwords for different environments (e.g., dev, staging, production) or sensitive projects if feasible.
What is the primary purpose of Ansible Vault?
To encrypt sensitive data (passwords, API keys, etc.) within Ansible projects to prevent plain-text storage.
tap to reveal
Which command creates a *new* encrypted file?
`ansible-vault create <filename>`
tap to reveal
How do you modify an *existing* encrypted file?
`ansible-vault edit <filename>`
tap to reveal
What command allows you to encrypt an *already existing* unencrypted file?
`ansible-vault encrypt <filename>`
tap to reveal
How can you provide the vault password to Ansible without typing it every time?
Using `--vault-password-file /path/to/vault_pass.txt` or configuring `vault_password_file` in `ansible.cfg`.
tap to reveal
What command is used to encrypt a single string for use as a variable in a playbook?
`ansible-vault encrypt_string --name 'my_var_name' 'my_secret_value'`
tap to reveal
What security best practice should be followed for vault password files?
They should have strict file permissions (e.g., `0600`) and never be committed to version control systems.
tap to reveal
What is the purpose of `ansible-vault rekey`?
To change the password of an existing encrypted vault file.
tap to reveal