Red Hat Certified Engineer RHCE · Free Practice Question Hard
Question 3
You are tasked with managing sensitive data, such as database passwords, in an Ansible playbook. How would you use Ansible Vault to encrypt these sensitive variables and ensure they are protected in the playbook?
-
A
Encrypting Sensitive Variables with Ansible Vault
To protect sensitive data:- ansible-vault encrypt secrets.yml
In your playbook:
- - hosts: all
- vars_files:
- - secrets.yml
- tasks:
- - name: Use encrypted database password
- ansible.builtin.debug:
- msg: "Database password is {{ db_password }}"
During playbook execution:
- ansible-playbook playbook.yml --ask-vault-pass
This ensures sensitive data is encrypted and decrypted as needed.
-
B
This is a performance based question and not multiple choice. Therefore the answer is in #1. If you require additional explanation, please ask your question in the Q&A section.
Reveal correct answer
Correct answer: A
A.
In this scenario, Ansible Vault is used to encrypt sensitive variables, such as database passwords, within a playbook to ensure that they are protected. First, the sensitive data is encrypted using ansible-vault encrypt secrets.yml. In the playbook, the encrypted file is referenced as vars_files to access the sensitive data. The variable (e.g., db_password) is used in the playbook as if it were a regular variable, but it will be decrypted during execution using the appropriate vault password. When running the playbook, the --ask-vault-pass option is used to prompt for the vault password, ensuring that the sensitive data is decrypted when needed and kept secure throughout the playbook's execution.
Line-by-Line Explanation
ansible-vault encrypt secrets.ymlThis command is used to encrypt the
secrets.ymlfile, which contains sensitive data (like passwords). After encryption, the file will no longer be readable without the appropriate password.
In the playbook:
This section shows the Ansible playbook content.
- hosts: allThis line specifies that the tasks will be executed on all hosts listed in the Ansible inventory.
vars_files:This line references the encrypted
secrets.ymlfile, which contains the sensitive variables to be used in the playbook.
- secrets.ymlThis is the file that contains the encrypted sensitive variables (such as passwords). It is used here to provide the necessary values for the playbook to execute securely.
tasks:This starts the list of tasks to be executed on the managed hosts.
- name: Use encrypted database passwordThis is a descriptive name for the task, indicating that it will use an encrypted database password.
ansible.builtin.debug:This module is used to print the value of the decrypted variable (the database password) for debugging purposes. It is helpful to confirm that the variable has been successfully decrypted.
msg: "Database password is {{ db_password }}"This line outputs the value of the
db_passwordvariable, which is decrypted during playbook execution.
During playbook execution:
This section shows the command used to execute the playbook.
ansible-playbook playbook.yml --ask-vault-pass
This command is used to run the playbook. The
--ask-vault-passoption prompts for the vault password during execution, which is necessary to decrypt the sensitive data insecrets.yml.
This approach ensures that sensitive data, like passwords, is securely stored in an encrypted file (using Ansible Vault) and is only decrypted when the playbook is executed. The playbook is configured to automatically decrypt this data and use it in the tasks, but it requires the vault password to do so. This method maintains the security of sensitive information while automating the deployment and management tasks.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
