Ansible Vault for Secrets
Vault encrypts files with AES256 so that variables containing passwords can live in the same repository as everything else. It is one command to encrypt, one flag to use, and the encrypted file behaves like any other vars file. This covers the whole lifecycle including the two things people miss: no_log, and what happens when you forget the password.
Templates, Files and Vault Guide 21 of 45 Intermediate
- OSUbuntu 26.04 LTS
- ansible-core2.20.1
- Python3.14.4
- TimeAbout 16 min
An encrypted file starts with $ANSIBLE_VAULT;1.1;AES256, which makes it recognisable to file and to git. There is no password recovery - the key is the password, and losing it means the contents are gone. Vault protects against a repository being read; it does nothing about a running process, since Ansible must decrypt to use the value.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| ANS-CTL01 | 192.168.0.36 | Ubuntu 26.04 LTS | Ansible Control Node | 2 Core | 3 GB | 50 GB |
| ANS-A01 | 192.168.0.37 | Ubuntu 26.04 LTS | Managed Node (group: web) | 2 Core | 3 GB | 50 GB |
This guide includes
Use this when a variable file holds a password. This matters because Vault encrypts with AES256 so the file can live in Git - and encrypting a single string instead keeps the rest of the file readable in review.
- starting from a secret sitting in plain text
- encrypting the file, then viewing it without decrypting it on disk
- using an encrypted file in a play exactly like any other
- encrypting a single string instead, so the file stays reviewable
- changing the password with
rekey
Before you start
- A control node configured as shown in the first step.
- The session writes a password to
~/.vault_passfor automation. In production that file belongs outside the repository, in a secret manager, or is typed with--ask-vault-pass.
-
The configuration this guide assumes
-
A secret in plain text
-
Encrypt it
-
Reading it without decrypting the file
-
Using it in a play
-
Encrypting a single string
-
Changing the password