I have been waiting for ansible 2.3 as it was going to introduce encrypt_string feature.
Unfortuately I'm not sure how can I read the encrypted string.
I did try decrypt_string, decrypt (the file), view (the file) and nothing works.
cat test.yml
---
test: !vault |
$ANSIBLE_VAULT;1.1;AES256
37366638363362303836383335623066343562666662386233306537333232396637346463376430
3664323265333036663736383837326263376637616466610a383430623562633235616531303861
66313432303063343230613665323930386138613334303839626131373033656463303736366166
6635346135636437360a313031376566303238303835353364313434363163343066363932346165
6136
The error I'm geeting is ERROR! input is not vault encrypted data for test.yml
How can I decrypt the string so I know what it's value without the need to run the play?
Although, there is no problems showing encrypted string values with ansible debug messages or using ansible cli, there is one more solution that may be convenient for automation needs. You can utilize python libs from ansible and use them in your code (basically, all this located in ansible.parsing.*)
1) Provide vault password and generate "vault" with secrets.
2) Load yaml file with AnsibleLoader:
3) If you need to encrypt a new string and update your dictionary:
4) Once complete processing, write back with AnsibleDumper:
For a file like test.yml:
the following crude implementation (recomended only for some quick manual action obviously):
should work, provided that you have the key that encrypted the data.
since whole vault files do not play well with git histories, using vault strings within the variable files is the way to go, it also makes grepping out variables by name much clearer.
Here is a simple worked example:
I want to put fredsSecretString: value into vars.yml , (its value is fastfredfedfourfrankfurters but hush, don't let people know !!)
To decrypt the value feed the encrypted string back into ansible-vault as follows:
Here is what works for me, similar to what Scudelletti does but passing in the vault pass i.e.
The output will be on its own line for convenience, thanks to the trailing
&& echo
. The permission of my vault pass is 644 if you run into any permission errors.Hope it helps!
Did you try setting the encrypted string as a variable and then using
-debug
to get its decrypted output?i.e.
Define your encrypted string as a variable
test
in your playbook and then do:in your playbook and then run the playbook:
This one command extracts out just the encrypted data and passes it to decrypt. I like it a bit better, as you don't need to manually extract the data.