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_password
encrypted string value to the clipboard.
-
echo -n '<Password>' | ansible-vault encrypt_string --stdin-name 'ansible_password'
ansible_password: !vault |$ANSIBLE_VAULT;1.1;AES256363562306633656261653965636233343331643131623064303738623338386366383337353936326633626333363163623035343538363632373261636663610a663335633333346238333264383465336433343832663332353333663865393837376431363237366363336338346630373636663039623138323239636561340a34643962336462666230383336653233653361386431626664356465643032393764653437323534646336666235373162643731303862353564386534326234Encryption successful
- Open
linux.yml
andwindows.yml
. Replace theansible_password
variable with the encrypted string.
---ansible_user: ansibleansible_password: !vault |$ANSIBLE_VAULT;1.1;AES256363562306633656261653965636233343331643131623064303738623338386366383337353936326633626333363163623035343538363632373261636663610a663335633333346238333264383465336433343832663332353333663865393837376431363237366363336338346630373636663039623138323239636561340a34643962336462666230383336653233653361386431626664356465643032393764653437323534646336666235373162643731303862353564386534326234ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
- Verify that the variables are loading.
ansible-inventory -i hosts --list --ask-vault-pass
Decryption
When using encrypted strings, theansible-inventory
does not decrypt the string.
"hostvars": {"ec2-50-17-205-144.compute-1.amazonaws.com": {"ansible_connection": "winrm","ansible_password": {"__ansible_vault": "$ANSIBLE_VAULT;1.1;AES256\n3635623066336562616539656362333433316431316230643037386 2333838636638333735393632\n6633626333363163623035343538363632373261636663610a663335 633333346238333264383465\n336433343832663332353333663865393837376431363237366363336 33834663037363666303962\n3138323239636561340a34643962336462666230383336653233653361 3864316266643564656430\n32393764653437323534646336666235373162643731303862353564386 534326234"},"ansible_user": "ansible","ansible_winrm_server_cert_validation": "ignore"},
- Use the debug module to output the decrypted variable. When prompted, enter the vault password.
ansible all -i hosts -m debug -a "var=ansible_password" --ask-vault-pass
Shell History
The method of using theecho
command 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:
ansible-vault encrypt_string --stdin-name 'ansible_password'
Caution
Do not press Enter after supplying the string to encrypt. That will add a newline to the encrypted value.
-
Review the
hosts
file and thehost_vars
and ensure that the files’ names and the IP addresses in the files match using thecat
command. -
Update the passwords in the
group_vars
files. -
Review the
group_vars/windows_encrypted.yml
andgroup_vars/linux_encrypted.yml
files.
We have provided these demo encrypted files for your review. The
group_vars/{windows|linux}.yml
will look something like thegroup_vars/{windows/linux}_encrypted.yml
files, 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.
# Encrypt ansible passwordecho -n '<Password>' | ansible-vault encrypt_string --stdin-name 'ansible_password'# ORansible-vault encrypt_string --stdin-name 'ansible_password'## Replace the ansible password in linux.yml and windows.yml files.# Verify variablesansible-inventory -i hosts --list --ask-vault-pass# Decrypt ansible_passwordansible all -i hosts -m debug -a "var=ansible_password" --ask-vault-pass
[linux] vm-linuxweb001.eastus.cloudapp.azure.com ec2-54-88-16-230.compute-1.amazonaws.com [windows] vm-winweb001.eastus.cloudapp.azure.com ec2-54-173-157-198.compute-1.amazonaws.com
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_string
debug
Get hands-on with 1300+ tech skills courses.