Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 164IaC · Ansible

How to encrypt secrets with Ansible Vault

Your playbooks need database passwords and API keys, and the playbooks live in Git. Ansible Vault encrypts files (or single values) with AES-256, so the repo can hold them safely and Ansible decrypts them at run time. This how-to creates an encrypted vars file, uses a clear naming pattern, runs playbooks with the vault password from a file or CI secret, and keeps secrets out of the output.

  • 5 Lessen
  • 6 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

164.1

The quick answer

ansible-vault create group_vars/prod/vault.yml
ansible-vault edit group_vars/prod/vault.yml
ansible-playbook -i inventory.ini site.yml --ask-vault-pass
ansible-playbook -i inventory.ini site.yml --vault-password-file ~/.vault_pass_prod

164.2

Step by step

  1. Two files per group — the encrypted secret values, and a plain file that points to them (so grep still shows where a variable comes from):
group_vars/prod/vault.yml (encrypted)
vault_db_password: "<long random password>"
vault_api_token: "<token>"
group_vars/prod/vars.yml (plain)
db_password: "{{ vault_db_password }}"
api_token: "{{ vault_api_token }}"
  1. Encrypt a single value inside a normal file:
ansible-vault encrypt_string --name 'smtp_password' '<the password>'

(Typing the value on the command line puts it in your shell history — prefer --stdin-name smtp_password and paste it.)

  1. The vault password — never in Git. A file with 600 rights for people, or a CI secret written to a temp file:
chmod 600 ~/.vault_pass_prod
ansible-playbook -i inventory.ini site.yml --vault-password-file ~/.vault_pass_prod

Different passwords per environment with vault IDs:

ansible-vault create --vault-id prod@~/.vault_pass_prod group_vars/prod/vault.yml
ansible-playbook site.yml --vault-id dev@~/.vault_pass_dev --vault-id prod@~/.vault_pass_prod
  1. Keep secrets out of logs — no_log: true on tasks that use them:
tasks/main.yml (part)
- name: Create the app database user
  community.postgresql.postgresql_user:
    name: shop
    password: "{{ db_password }}"
  no_log: true
  1. Daily commands:
ansible-vault view group_vars/prod/vault.yml
ansible-vault rekey group_vars/prod/vault.yml
ansible-vault decrypt --output - group_vars/prod/vault.yml | head -3

rekey = new vault password (after someone leaves).

  1. Bigger setups: read secrets at run time from HashiCorp Vault, AWS Secrets Manager or Azure Key Vault with lookup plugins — one source of truth, no copies in the repo.

164.3

Check that it worked

grep -c '^.ANSIBLE_VAULT;1.1;AES256' group_vars/prod/vault.yml
1
git grep -n 'vault_db_password'

The file in Git starts with the $ANSIBLE_VAULT;1.1;AES256 header (it is encrypted), the playbook runs with the vault password, and the output shows censored instead of secret values.

164.4

Common problems

ProblemCauseFix
Attempting to decrypt but no vault secrets foundNo vault password given.--ask-vault-pass or --vault-password-file.
Decryption failed (no vault secrets were found that could decrypt)Wrong password or vault ID.Use the right file/ID for that environment.
A secret printed in the outputTask without no_log, or debug of the variable.no_log: true; never debug secrets.
Hard to find where a variable is setEverything inside the encrypted file.The vars.yml → vault_* pattern.
Merge conflicts in vault filesEncrypted text changes completely on every edit.Few people edit; one change per PR; or split into more files.
Vault password in the repo by accidentConvenience.Rotate: rekey, change the real secrets too.

164.5

Go further

1Why keep a plain vars.yml that points to vault_* variables?

You can still find which variables exist and where they come from, without decrypting the vault file.

2How do you stop a task from printing a secret?

Add no_log: true to the task.

Alle commando's in dit hoofdstuk