Does anyone have an example of decrypting and uploading a file using ansible-vault.
I am thinking about keeping my ssl certificates encrypted in source control.
It seems something like the following should work.
---
- name: upload ssl crt
copy: src=../../vault/encrypted.crt dest=/usr/local/etc/ssl/domain.crt
Until the 'copy' module has been extended to automatically decrypt vault files, here's a simple workaround:
When stdout is not a tty,
ansible-vault view <file>
prints cleartext to stdout without invoking a pager.In combination with a 'pipe' lookup, this behavior can be used with a vault password file to feed into the copy module's 'content' option:
There is a feature request to support this natively in the copy module. But until that is implemented, here is the workaround (similar to @dave1010's answer, but repeating common parts for completeness):
Create a
secrets.yml
file encrypted with ansible vault which contains your secrets, for example:In your playbook, include it:
Then you can use the variables in tasks:
However, this doesn't work if the file that you are trying to copy is a binary file. In that case, you need to first encode the content with base64:
Then put the base64 encoded value in your
secrets.yml
file, e.g.:Then you can create the remote file in two steps:
Note that you could delete the temporary
cert.b64
file on the remote host. But then re-running the playbook will re-create it instead of skipping this task. So, I prefer to leave it there.UPDATE: This feature has been implemented in Ansible 2.1.
That's not going to work. What you will get is your
encrypted.crt
(with Ansible Vault) uploaded literally asdomain.crt
What you need to do is make your playbook part of a "Vault" and add a variable that contains your certificate content. Something like this:
You can choose to put your
mycert
variable in a separate variable file using Ansible Vault too.I think, you have a simpler way to do this.
If you use certificate+key in one file in some format (like pkcs12 or just concatenated), you can use generic
openssl
(orgpg
, or something else) encryption. It will look like this:After that you can just copy encrypted.aes to remote host and decrypt it in-place:
If you have separate key file in pem or der format, you can use
You can also use local_action to temporairly decrypt your file as part of the playbook:
Update: As of April 2016 my Github PR has been merged and is available in Ansible 2.1 and later. The below was an interim solution until the PR was merged.
Wanting to do the same thing I created an action plugin to implement the feature. This is available via github. The plugin is exactly the copy action plugin as shipped with ansible, but with support for vault decryption.
You can use it like this:
if secret.txt is encrypted (and the vault password is supplied) then it will be decrypted and copied.