RHCE 9.0 Practice Exam: Creating an Ansible Vault for Password Storage
π Introduction
In this RHCE 9.0 Ansible exam practice guide, we will securely store sensitive information, such as user passwords, using Ansible Vault. Ansible Vault allows us to encrypt and protect sensitive data, ensuring passwords are not exposed in plaintext within playbooks or configuration files.
This guide includes:
- Step-by-step instructions to create an encrypted vault
- How to automatically use a vault password file
- How to encrypt and decrypt password variables
- How to verify encryption and troubleshoot common issues
π‘ What You Will Learn
β
How to create an Ansible Vault for password storage
β
How to use ansible-vault create
to store sensitive information securely
β
How to configure ansible.cfg
to auto-load the vault password
β
How to verify encrypted files and decrypt them when needed
π 1. Task Requirements
π 1.1. Problem Statement
- Create an Ansible Vault named
/home/greg/ansible/locker.yml
. - Store two password variables inside the vault:
pw_developer: Imadev
pw_manager: Imamgr
- Use the vault password:
whenyouwishuponastar
- Store the vault password inside a file at
/home/greg/ansible/secret.txt
- Configure
ansible.cfg
to use the vault password file automatically. - Verify encryption and decryption using
ansible-vault
.
π 2. Writing the Ansible Vault Playbook
π 2.1. Configure Ansible to Use a Vault Password File
$ vim /home/greg/ansible/ansible.cfg
π Add the Following Content
[defaults]
vault_password_file = /home/greg/ansible/secret.txt
π Explanation:
vault_password_file = /home/greg/ansible/secret.txt
β This tells Ansible to automatically use the vault password stored insecret.txt
.
π 2.2. Store the Vault Password in a File
$ echo whenyouwishuponastar > /home/greg/ansible/secret.txt
$ chmod 600 /home/greg/ansible/secret.txt
π Explanation:
- The vault password is saved to
secret.txt
. chmod 600
restricts access to the file so only the owner can read and write.
π 2.3. Create an Encrypted Vault File
$ ansible-vault create /home/greg/ansible/locker.yml
π This command will prompt for a password (we use whenyouwishuponastar
).
π Once inside the editor, add the following content:
---
pw_developer: Imadev
pw_manager: Imamgr
π Explanation:
- Creates a new encrypted file (
locker.yml
). - Stores user passwords securely.
π 2.4. Verify That the File Is Encrypted
$ cat /home/greg/ansible/locker.yml
π Expected Output (Encrypted Content)
$ANSIBLE_VAULT;1.1;AES256
3863666662376132653636383666306664633665636166366465323533396130663431393932663
3038366162383733643633383935663431376163646639350a39343666356636663130306465393
6337303063633334336262313065363336363033646164626236323964333535346665353464313
...
β This confirms that the file is successfully encrypted.
π 3. Viewing and Editing the Encrypted Vault
π 3.1. View the Vault Contents
$ ansible-vault view /home/greg/ansible/locker.yml
π Expected Output
---
pw_developer: Imadev
pw_manager: Imamgr
β This confirms that the vault decryption is working.
π 3.2. Edit the Vault File
$ ansible-vault edit /home/greg/ansible/locker.yml
π This allows you to modify the contents of the encrypted vault.
π 3.3. Encrypt an Existing File
$ ansible-vault encrypt /home/greg/ansible/locker.yml
π This encrypts an already existing plaintext file.
π 3.4. Decrypt an Encrypted File
$ ansible-vault decrypt /home/greg/ansible/locker.yml
π This permanently removes encryption from the file.
π 4. Verifying Encryption in Playbooks
To ensure that Ansible can use encrypted variables within playbooks, follow these steps:
π 4.1. Create a Test Playbook
$ vim /home/greg/ansible/test_vault.yml
π Add the Following Content
---
- name: Test Ansible Vault Variables
hosts: localhost
tasks:
- name: Show Encrypted Passwords
debug:
msg:
- "Developer Password: {{ pw_developer }}"
- "Manager Password: {{ pw_manager }}"
π Explanation:
- This playbook retrieves passwords from the vault and displays them.
π 4.2. Run the Playbook
$ ansible-playbook /home/greg/ansible/test_vault.yml
π Expected Output
TASK [Show Encrypted Passwords] ********************************************
ok: [localhost] => {
"msg": [
"Developer Password: Imadev",
"Manager Password: Imamgr"
]
}
β This confirms that the encrypted variables are correctly retrieved and used.
π 5. Common Issues & Troubleshooting
π΄ Issue 1: ERROR! Decryption failed
β Solution:
- Ensure the correct vault password is used.
Manually specify the password file if needed:
$ ansible-vault view /home/greg/ansible/locker.yml --vault-password-file=/home/greg/ansible/secret.txt
Verify that the password in secret.txt
matches the vault password:
$ cat /home/greg/ansible/secret.txt
π΄ Issue 2: Ansible Playbook Cannot Access Encrypted Variables
β Solution:
Run the playbook with --ask-vault-pass
if needed:
$ ansible-playbook /home/greg/ansible/test_vault.yml --ask-vault-pass
Ensure ansible.cfg
includes the correct vault password file:
[defaults]
vault_password_file = /home/greg/ansible/secret.txt
π΄ Issue 3: File Not Encrypted
β Solution:
Check if the file is already encrypted:
$ cat /home/greg/ansible/locker.yml
If the file is in plaintext, encrypt it manually:
$ ansible-vault encrypt /home/greg/ansible/locker.yml
π 6. Summary
- Created a secure Ansible Vault file (
locker.yml
) to store passwords. - Configured
ansible.cfg
to automatically load the vault password. - Verified encryption and decryption using
ansible-vault view
. - Used the encrypted passwords in an Ansible playbook.
- Troubleshot common encryption and decryption errors.
π Congratulations! You have successfully implemented Ansible Vault to securely store passwords for RHCE 9.0! π
π’ If you found this guide helpful, share it with your RHCE 9.0 study group! π’
π₯ Good luck on your RHCE 9.0 exam! π₯