Ansible-vault?!
Always wondered how to use ansible with sensitive information like secrets,files,variables or configurations? Thats where ansible-vault comes to play, this is a way to encrypt the data with a password.
There are two ways to use ansible-vault, with cli parameters or use a password file. We will explorer both options below.
How to use the CLI parameters
The CLI parameters variant asks for a password that you want to use for en/decrypt or view the encrypted content. I have created a file named in /Users/sebas/Desktop/ansible/supersecret.yml with the following content in it:
Content for the supersecret.yml file
|
|
CLI ansible-vault options
There are plenty of options in the ansible-vault CLI tool but we focus on the main options like encryptions, viewing decrypting.
Encrypting the file
Choose a password to use to encrypt the file. WARNING! DO NOT FORGET IT! otherwise the file cant be decrypted.
|
|
Viewing the encrypted file
The ouput is transformed into a non readable format for us but it is for ansible-vault.
|
|
Viewing the file
Viewing is a options to view the encrypted secret without decrypting the file itself. This uses the password we set earlier when encrypting the file.
|
|
Decrypting the file
Decrypting is making the file readable way for us and exposable.
|
|
Playbook with cli parameter ask password
To test this, we are creating a new playbook with vi /Users/sebas/Desktop/ansible/my_first_vault_playbook.yml and add the content below in it.
Encrypt the supersecret.yml file with a selfchosen password through the way showed above.
|
|
To run the playbook we use the following command ansible-playbook my_first_vault_playbook --ask-vault-password
This will ask for the password we used to encrypt the supersecret.yml file.
|
|
How to use ansible-vault with a password file?
To use a ansible-vault with a password file we need two essential files:
- ansible configuration file: /Users/sebas/Desktop/ansible/ansible.cfg
- password file: /Users/sebas/Desktop/ansible/.vault_pass
Ansible configuration and a password file
We can modify ansible to use certain parameters for the configuration, this will be done through ansible.cfg file.
We create this file the ansible folder with touch /Users/sebas/Desktop/ansible/ansible.cfg and touch /Users/sebas/Desktop/ansible/.vault_pass
Open the ansible.cfg file and place the following code in it:
|
|
Open the .vault_pass file and add something like this (preferable something strong password):
|
|
The ansible.cfg file works for the specific folder, this means we can add this file to more roles and specify for each its own .vault_pass password.
Using Encrypt, Decrypt again
Because the ansible.cfg, password, supersecret.yml files are in the same folder we can do the same commands again without getting the password prompt.
|
|
Password file for playbooks or roles?
We are going to reuse our role we’ve created in part 4. Copy the .vault_pass file into the vars folder and copy the ansible.cfg file into to main role folder.
There is no solution to use the vault straight into the role or playbook with a password file. The only solution is to use a group/vars or host vars folders outside the roles, this is a wider ansible configuration setup
Add the information below to our main.yml file in the vars folder.
This lets us use the information from our vault as a variable inside the role itself.
|
|
to use our password from our file we need to surround it with "{{ name from vault info }}"
Ansible knows this syntax and can be used for other vars aswell.
Add following code blocks somewhere in the file 01_hostname.yml.
|
|
Lets run our role playbook ansible-playbook ../role_book.yml
|
|
Conclusion
We all use different kind of secrets if its variables, files or other configurations. Using an encryption method like ansible-vault helps automation alot. It is not without faults but is better then exposing our secrets to the world.
Stay tuned for the Part 6: Tesing our Ansible code!