Ansible vault example

2018-02-21 - Getting started with ansible vault
Tag: ansible

Editing a protected file

Here is how to edit a vault protected file :

ansible-vault edit hostvars/blah.yml

Using a vault entry in a task or a jinja template

It is as simple as using any variable :

- copy:
    path: /etc/ssl/private.key
    mode: 0400
    content: '{{ ssl_key }}'

How to specify multiple lines entries

This is actually a yaml question, not a vault one but since I ask myself this frequently in this context here is how to put a multiple lines entry like a private key in vault (for a simple value, just don’t use a |):

ssl_key : |
  ----- BEGIN PRIVATE KEY -----
  blahblahblah
  blahblahblah
  ----- END PRIVATE KEY -----  

How to run playbooks when vault values are needed

To run a playbook, you will need to pass the --ask-vault argument or to export a ANSIBLE_VAULT_PASSWORD_FILE=/home/julien/.vault_pass.txt variable (the file needs to contain a single line with your vault password here).

Ressources