Ansible Vault Tutorial: Protect Secrets in Automation Projects
Ansible Vault encrypts variables and files used by automation projects. This Ansible Vault tutorial shows how to create an encrypted variables file, load it in a local practice playbook and plan password handling and rotation. Encryption protects stored content; decrypted values still need protection in tasks, logs and destination files.
Ansible Vault encrypts variables and files used by automation projects. This Ansible Vault tutorial shows how to create an encrypted variables file, load it in a local practice playbook and plan password handling and rotation. Encryption protects stored content; decrypted values still need protection in tasks, logs and destination files.
The examples assume an existing Ansible installation on a Linux control machine, a normal user account and a new private practice directory. Commands were checked against official documentation on 17 November 2026 and have not been executed. Use dummy values and a separate practice password; no production inventory or credentials are needed.
Keep these boundaries visible
- Protect the vault password separately from encrypted project files.
- Review where decrypted values can appear.
- Use different passwords where access must differ.
- Plan both file rekeying and application credential rotation.
What Ansible Vault protects
A project can need a database password or API token without exposing its value in a readable variables file. Vault supplies encryption for that stored content and integrates with Ansible execution when the corresponding password is available. [1]
Trace the complete path before adding a secret: where it is stored, who can retrieve the vault password, which task consumes the value and where the result is written. If a template writes a readable credential on a managed machine, the destination still needs appropriate access controls.
For the exercise below, the only value is a dummy token. The play validates that input without contacting a remote service or writing a service configuration. This keeps the tutorial focused on loading encrypted data.
Prepare a separate practice project
Check which Ansible installation and configuration your shell will use. Work as a normal user, with an editor you trust and a new directory you own. Review any inherited project configuration or plugins before executing a playbook.
ansible-vault --version
ansible-playbook --version
mkdir ansible-vault-practice
cd ansible-vault-practice
The directory commands create and enter a local practice folder. If that name already exists, choose a new empty directory. No administrative privileges are required for this file-based exercise. The version output is intended to identify your environment; no example output is presented as observed here.
Choose a unique practice vault password and retain it in your approved password store. Keep it separate from the project files. The password used to encrypt content is required when that content is consumed. [5]
For background on the execution environment, see the WebAsha Ansible Navigator guide. Installation requirements should be checked separately for your platform.
Create an encrypted variables file
From the new practice directory, use the command below. It prompts for the practice password and opens your configured editor. Creating the file writes encrypted content after the editor session closes. [2]
ansible-vault create --vault-id dev@prompt vault.yml
Inside the editor, enter this YAML using the dummy value shown. Save and close the editor normally.
---
vault_demo_token: "practice-value-only"
The expected result is a Vault-encrypted file rather than readable YAML on disk. That is intended behaviour, not a test result from this article. Confirm the file was saved before continuing. Never use a real token while learning editor or command behaviour.
The dev label records which password source the project intends to use. It is visible metadata, so keep sensitive information out of labels and filenames. Review editor backup, swap and temporary-file settings before editing real secrets.
Load the encrypted file in a local playbook
Save the following as vault-demo.yml beside vault.yml. Its explicit localhost target and local connection avoid a remote-host dependency. Fact gathering is disabled because the exercise only needs the supplied variable.
---
- name: Validate an encrypted dummy variable locally
hosts: localhost
connection: local
gather_facts: false
vars_files:
- vault.yml
tasks:
- name: Check the practice token without displaying it
ansible.builtin.assert:
that:
- vault_demo_token is string
- vault_demo_token | length > 0
no_log: true
- name: Report the non-secret completion message
ansible.builtin.debug:
msg: "Practice variable validation completed."
Review both files, then use the commands below in that directory. The trailing comma in the inventory argument supplies a host list containing localhost. The first command checks syntax; the second executes the practice tasks on the control machine.
ansible-playbook -i localhost, vault-demo.yml --vault-id dev@prompt --syntax-check
ansible-playbook -i localhost, vault-demo.yml --vault-id dev@prompt
Vault passwords can be supplied interactively, through a password file or through a password client script. This tutorial uses the interactive vault-ID prompt so the example needs no stored password file. [4]
When the correct password and valid YAML are supplied, the intended outcome is that validation passes and the literal completion message appears. No token should be intentionally printed. Syntax checking alone does not establish that a later task will complete or that a real application credential works.
Choose view, edit or decrypt deliberately
For the encrypted dummy file, these commands display its contents or open them for editing. Viewing reveals plaintext on your screen; use a private terminal session. Editing saves the file encrypted again when the editor closes normally. [2]
ansible-vault view --vault-id dev@prompt vault.yml
ansible-vault edit --vault-id dev@prompt vault.yml
| Operation | Use | What to review |
|---|---|---|
| create | Make a new encrypted file | Editor settings and password retention. |
| encrypt | Encrypt an existing plaintext file | Original copies, backups and repository history. |
| view | Read decrypted content through a pager | Screen exposure and terminal capture. |
| edit | Change content and save it encrypted again | Temporary editor files and saved content. |
| decrypt | Remove file encryption | Plaintext output and any later commit. |
| rekey | Re-encrypt a vaulted file with a new secret | New password access and old copies. |
Decrypting a file is an intentional change to its stored protection. Use view or edit for the ordinary review steps in this tutorial. Encrypting a file later does not remove a plaintext copy that was already committed or shared.
Whole-file encryption or an encrypted variable?
Whole-file encryption hides the file’s contents, including variable names. An inline encrypted variable leaves the surrounding YAML readable. The choice affects review and maintenance: file-level content can be rekeyed, while an inline encrypted variable must be recreated for password rotation. [3]
To explore inline encryption separately, run this command with a dummy value at its prompt. It produces an encrypted YAML variable snippet; it does not add that snippet to the earlier playbook automatically.
ansible-vault encrypt_string --vault-id dev@prompt --name vault_demo_token --prompt
The prompt option requests the value interactively. Do not place a real secret directly in a shell command: command history can retain it. Copy the complete generated snippet, including its YAML tag and indentation, into a separate variables file if you want to extend the exercise. [3]
Do not combine competing definitions while testing the two approaches. Keep the original whole-file exercise intact and make the inline variant a separate practice case, with a clearly identified variables source.
Vault IDs and password sources
A vault ID uses a label and password source, such as dev@prompt. Multiple password sources can be supplied when a playbook needs differently encrypted content. By default, labels are hints: Ansible tries a matching label first and may try other supplied passwords afterward. [4]
Use different passwords where different people or jobs must have different decryption access. Changing a label while reusing the same password does not establish a new access boundary. Keep a private record of which project content depends on which password.
For unattended use, the password source can be a protected file or an approved client script that retrieves it from a secret store. Anyone who can read that file or retrieve that value can use it to decrypt the corresponding content. [5]
Repository access and decryption access are separate decisions. Review both when someone joins or leaves the project. The WebAsha automation workflow guide gives broader context for coordinating tools and environments.
Protect task output and pipeline handling
The practice assertion uses no_log: true because its inputs include the token. Ansible’s logging guidance warns that no_log does not cover debugging output. Avoid separate debug tasks that print secret variables, and review callbacks and saved output. [6]
For a real pipeline, first identify which job is permitted to obtain the password and why it needs it. Keep the source outside version control. Restrict runner access, avoid shell tracing around secrets and review logs and artifacts before retaining them.
If the pipeline uses a temporary password file, arrange cleanup on success and failure using the CI system’s supported lifecycle controls. Check the runner’s retention behaviour as well. A final cleanup command that runs only after a successful playbook is incomplete.
Use these questions to review your CI implementation. Test its secret-injection and cleanup mechanism in your organisation’s environment. Runtime permissions, destination-file controls and backup handling remain relevant after Vault decryption.
Rekey the file and rotate credentials separately
For the whole-file dummy example, the following command uses the original practice password and prompts for a new password under a new label. It rewrites the encrypted file, so retain an appropriate recovery copy and confirm password access before using rekey on important content.
ansible-vault rekey --vault-id dev@prompt --new-vault-id dev-v2@prompt vault.yml
The rekey action needs the previous secret and re-encrypts a vaulted file with a new secret. [2] After a successful rekey, update the exercise invocation to use dev-v2@prompt and repeat the local validation with the new password.
Rekeying changes file encryption. It does not change the token or password accepted by an application. If a real credential was disclosed, follow that service’s rotation or revocation process and update the stored value through the approved change procedure.
Old repository revisions and backups may still contain content encrypted with the old password. Include those copies and former password holders in the access review. Record verification without including the credential itself.
Troubleshoot the stage that failed
| Symptom | Check | Avoid |
|---|---|---|
| No usable vault secret | Password source, prompt and selected identity | Putting the password in a command or error report. |
| Decryption failed | Matching password and intact encrypted content | Editing encrypted payload text by hand. |
| YAML or variable error | Original YAML and vars_files path | Assuming successful decryption validates the data. |
| Secret appeared in output | Debug tasks, callbacks, logs and artifacts | Treating the encrypted file as runtime log protection. |
| Inline rotation confusion | Which value needs recreation | Applying file rekey assumptions to inline values. |
| Old access still works | Old copies and reused passwords | Assuming a renamed label revokes access. |
Use the dummy exercise to isolate a problem. Check the password source first, then whether the encrypted file is intact, then whether the decrypted variables match what the task expects. Share a redacted error and a non-secret reproduction when requesting help.
Before moving to a real project, document ownership, password recovery, output handling and the credential’s actual use. For more diagnostic practice, see the WebAsha Ansible interview question guide.
Discuss Ansible and DevOps training with WebAsha Technologies. Contact WebAsha with your starting skills and the automation practices you want to develop.
Official references
Frequently Asked Questions
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0