Secure Secrets with Ansible Vault: Use Encrypted Strings
Secure secrets by encrypting strings using Ansible Vault.
Some variables do not contain sensitive information. Those variables could also be instrumental when troubleshooting. And in certain situations, it might not make sense to encrypt the entire file, making discovery more difficult.
Perhaps we only want to encrypt the sensitive variables and not the whole file. Ansible Vault can do that, too.
You will create an encrypted string for the ansible_password variable and decrypt the group variable files.
-
Generate an encrypted string variable for
ansible_password. When prompted, enter the vault password.-
Replace
<Password>with the ansible user password. -
Copy the
ansible_passwordencrypted string value to the clipboard.
-
- Open
linux.ymlandwindows.yml. Replace theansible_passwordvariable with the encrypted string.
- Verify that the variables are loading.
Decryption
When using encrypted strings, theansible-inventorydoes not decrypt the string.
- Use the debug module to output the decrypted variable. When prompted, enter the vault password.
Shell History
The method of using theechocommand to populate the encrypted string leaves the shell history password. Please do not use it outside of testing.
You can use the following command in its stead:
Caution
Do not press Enter after supplying the string to encrypt. That will add a newline to the encrypted value.
-
Review the
hostsfile and thehost_varsand ensure that the files’ names and the IP addresses in the files match using thecatcommand. -
Update the passwords in the
group_varsfiles. -
Review the
group_vars/windows_encrypted.ymlandgroup_vars/linux_encrypted.ymlfiles.
We have provided these demo encrypted files for your review. The
group_vars/{windows|linux}.ymlwill look something like thegroup_vars/{windows/linux}_encrypted.ymlfiles, respectively.
- Run the commands.
Click on the Run button, wait for the environment to set up, and execute the commands summarized below in the widget’s terminal.
/
- windows.yml
In this lesson, we introduced how you can encrypt only the secret variables instead of the entire file. We looked at the following option with the ansible-vault command:
encrypt_stringdebug
