1. Set up decrypt_with.py
to <your-playbook-path>/filter_plugins/
(making the directory if it doesn't exist), and copy encrypt_file.py
to a new <your-playbook-path>/scripts/
directory. Make the latter executable with chmod a+x scripts/encrypt_file.py
.
2. Use http://docs.ansible.com/playbooks_vault.html to set up a playbook with some secret variables (I use vaulted_...
as a naming convention), including a key:
$ ansible-vault create staging.yml
---
- include: site.yml # Your main playbook
vars:
vaulted_key_password: "somereallylongexamplekeyhere"
3. Encrypt a file with scripts/encrypt_file.py
:
$ scripts/encrypt_file.py files/example/keys/secret_file.key
With password:
$ ls files/example/keys/secret_file*
files/example/keys/secret_file.key
files/example/keys/secret_file.key.enc
4. Then decrypt it on the fly in a playbook:
---
# ...
- name: "Some Files"
copy:
dest: "/tmp/keys/{{ item }}.key"
content: "{{ lookup('file', 'example/keys/' + item + '.key.enc') | decrypt_with(vaulted_key_password) }}"
with_items:
- "secret_file"
- "other_secret_file"
# ...
# Note the "copy:" hash, instead of the usual Ansible key=value pairs.
# See https://twitter.com/damncabbage/status/511701541808799744
A more fleshed-out write-up of the
copy:
hash comment above: ansible/ansible#9067