هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
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_prodStep by step
- Two files per group — the encrypted secret values, and a plain file that points to them (so
grepstill shows where a variable comes from):
vault_db_password: "<long random password>"
vault_api_token: "<token>"db_password: "{{ vault_db_password }}"
api_token: "{{ vault_api_token }}"- 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.)
- The vault password — never in Git. A file with
600rights 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_prodDifferent 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- Keep secrets out of logs —
no_log: trueon tasks that use them:
- name: Create the app database user
community.postgresql.postgresql_user:
name: shop
password: "{{ db_password }}"
no_log: true- 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 -3rekey = new vault password (after someone leaves).
- 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.
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
Attempting to decrypt but no vault secrets found | No 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 output | Task without no_log, or debug of the variable. | no_log: true; never debug secrets. |
| Hard to find where a variable is set | Everything inside the encrypted file. | The vars.yml → vault_* pattern. |
| Merge conflicts in vault files | Encrypted text changes completely on every edit. | Few people edit; one change per PR; or split into more files. |
| Vault password in the repo by accident | Convenience. | Rotate: rekey, change the real secrets too. |
Go further
- Command pages: ansible, vault.
- Next how-tos: Write an Ansible role, Keep secrets out of Terraform, Generate a strong password or secret.
- Full guide: Ansible Vault. Secret stores: Vault, SOPS.
- Practise: Lab: automate a web server with Ansible (with vault).
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.