All exams← All Red Hat Certified Engineer (RHCE) exam (EX294) notes

Managing Secrets with Ansible Vault: Red Hat Certified Engineer (RHCE) exam (EX294) revision notes

Revision notes

## 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.

Key facts

Flashcards

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
Practice questions

Managing Secrets with Ansible Vault: 5 practice questions

Q1. Which command is used to create a new, empty, encrypted file with Ansible Vault?
  • A.  ansible-vault init secrets.yml
  • B.  ansible-vault new secrets.yml
  • C.  ansible-vault create secrets.yml
  • D.  ansible-vault make secrets.yml
Show answer & explanation
✓ Answer: C. ansible-vault create secrets.yml
The `ansible-vault create` command is the correct and standard way to generate a new, encrypted file and open it in an editor.
Q2. You need to run a playbook that uses a vaulted file. How do you instruct Ansible to use a password stored in the file `/home/user/.vault_pass.txt`?
  • A.  ansible-playbook myplaybook.yml --vault-password-file /home/user/.vault_pass.txt
  • B.  ansible-playbook myplaybook.yml --vault-file /home/user/.vault_pass.txt
  • C.  ansible-playbook myplaybook.yml --ask-vault-pass /home/user/.vault_pass.txt
  • D.  ansible-playbook myplaybook.yml --secret-file /home/user/.vault_pass.txt
Show answer & explanation
✓ Answer: A. ansible-playbook myplaybook.yml --vault-password-file /home/user/.vault_pass.txt
The `--vault-password-file` argument tells `ansible-playbook` to read the vault password from the specified file instead of prompting interactively.
Q3. You have an existing plaintext file `sensitive_vars.yml` and want to encrypt it in-place. What is the correct command?
  • A.  ansible-vault create sensitive_vars.yml
  • B.  ansible-vault rekey sensitive_vars.yml
  • C.  ansible-vault edit sensitive_vars.yml
  • D.  ansible-vault encrypt sensitive_vars.yml
Show answer & explanation
✓ Answer: D. ansible-vault encrypt sensitive_vars.yml
The `ansible-vault encrypt` command is specifically designed to take an existing unencrypted file and encrypt it, prompting for a new password.
Q4. Which command allows you to encrypt a single string value for direct inclusion in a standard, unencrypted YAML file?
  • A.  ansible-vault hide 'mysecret'
  • B.  ansible-vault encrypt_string 'mysecret'
  • C.  ansible-vault string --encrypt 'mysecret'
  • D.  ansible-vault inline_encrypt 'mysecret'
Show answer & explanation
✓ Answer: B. ansible-vault encrypt_string 'mysecret'
`ansible-vault encrypt_string` is used to create an encrypted `!vault` block for a specific value, which can then be pasted into a regular YAML file.
Q5. What is the correct `ansible-vault` subcommand to change the password of an existing encrypted file named `credentials.yml`?
  • A.  ansible-vault rekey credentials.yml
  • B.  ansible-vault new-key credentials.yml
  • C.  ansible-vault passwd credentials.yml
  • D.  ansible-vault update-password credentials.yml
Show answer & explanation
✓ Answer: A. ansible-vault rekey credentials.yml
The `rekey` subcommand is used to change the password of a vault-encrypted file. It will prompt for the old password and then the new password.
Test yourself
See the Red Hat Certified Engineer (RHCE) exam (EX294) page

More Red Hat Certified Engineer (RHCE) exam (EX294) topics