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
- Reviewed23 August 2026
Written against the versions above. 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 |
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