Chapter 08 · Ansible Vault
Ansible Vault
Introducing Ansible Vault
Ansible Vault encrypts sensitive data (variables and files), preventing passwords such as “admin123” from being stored in plain text in your script. Vault encrypts and decrypts data as required, at execution time.
Managing secrets
- Encrypt variable files (e.g.
group_vars/prod/vault.yml) - Encrypt arbitrary files (certificates, sensitive configuration files)
- Use a
vault_password_file(with appropriate precautions) or an interactive password prompt
Rule: Vault protects secrets at rest. It does not replace a dedicated secrets management solution (HSM, HashiCorp Vault, KMS), but it is an excellent starting point.
Encryption and decryption
# Create an encrypted file
ansible-vault create vault.yml
# Encrypt an existing file
ansible-vault encrypt vars.yml
# View the decrypted contents
ansible-vault view vault.yml
# Edit
ansible-vault edit vault.yml
# Decrypt (warning: restores plaintext)
ansible-vault decrypt vault.yml
Using Vault in a playbook
Example (variables):
- name: Deploy using secrets
hosts: web
become: true
vars_files:
- vault.yml
tasks:
- name: Display values (not advisable in production)
ansible.builtin.debug:
msg: "User={{ db_user }} Password={{ db_pass }}"
Run:
ansible-playbook -i inventory.ini site.yml --ask-vault-pass
PLEASE NOTE:
Because AES-256 is a symmetric encryption algorithm, you can share an encrypted file over a network. Using the original password, you can decrypt the file on any other machine.
Practical exercise: “handling secrets properly”
- Create
vault.ymlwithdb_useranddb_passusingansible-vault create. - Reference it using
vars_filesin your playbook. - Deploy a configuration file using a template (e.g.
templates/monapp.conf.j2) that uses these variables. - Check on node1 and node2 that /opt/monapp.conf contains the values in plaintext.
Minimal example
initial setup:
ansible-vault create vault.yml
You will be prompted for a password, then an editor (venerable vi) will open. For example, enter:
db_user: "appuser"
db_pass: "S3cret_p4ssw0rd"
mkdir -p templates
touch templates/monapp.conf.j2
# File generated by Ansible
DB_USER={{ db_user }}
DB_PASS={{ db_pass }}
HOST={{ inventory_hostname }}
site-vault.yml
- name: Deployment using Vault secrets
hosts: web
become: true
vars_files:
- vault.yml
tasks:
- name: Deploy an application with secrets
ansible.builtin.template:
src: templates/monapp.conf.j2
dest: /opt/monapp.conf
owner: root
group: root
mode: "0600"
To run:
ansible-playbook -i inventory.ini site-vault.yml --ask-vault-pass
or
and to avoid clear password:
nano ~/.ansible_vault_pass (write the password in this file)
chmod 600 ~/.ansible_vault_pass
ansible-playbook -i inventory.ini site-vault.yml --vault-password-file ~/.ansible_vault_pass
Bonus:
ansible-vault encrypt secrets.yml
ansible-vault view secrets.yml
Multiple-choice quiz
1) Ansible Vault is used to:
- A. Encrypt secrets and sensitive files
- B. Manage VirtualBox snapshots
- C. Replace SSH
2) Which command edits a Vault file?
- A. ansible-vault edit
- B. ansible-vault ping
- C. ansible-vault run
3) To run a playbook using Vault with an interactive password prompt:
- A. --ask-vault-pass
- B. --ask-ssh-pass
- C. --ask-root-pass
Answers
1) A · 2) A · 3) A
End of this section. You can now automate.