Ansible interview question · Most asked
How do you manage secrets with Ansible Vault?
How you'd do it Asked in DevOps Engineer interviews
Summary
Ansible Vault encrypts files or single variables with AES-256 so secrets can live in the repository encrypted.
Keep secrets in dedicated vault files (group_vars/prod/vault.yml), reference them from plain variables, and
supply the vault password at run time from a password file, a script or the CI system's secret store, never
from the repository.
Longer Answer
Common practice:
- Split plain and secret variables:
group_vars/prod/vars.ymlholdsdb_password: "{{ vault_db_password }}"andvault.yml(encrypted) holdsvault_db_password. You can still grep for where a secret is used. - One vault ID per environment (
--vault-id dev@prompt --vault-id prod@/run/secrets/prod-vault), so developers can decrypt dev but not prod. no_log: trueon tasks that handle secrets, so values never reach logs or CI output.- Rotate:
ansible-vault rekeychanges the vault password; rotating the secret itself means editing the value and re-running. - For larger setups, read secrets at run time from HashiCorp Vault, AWS Secrets Manager or Azure Key Vault with a lookup plugin instead of storing them in the repo at all.
Example
ansible-vault create inventories/prod/group_vars/all/vault.yml
ansible-vault encrypt_string 's3cr3t' --name 'vault_api_key'
ansible-playbook site.yml -i inventories/prod --vault-id prod@/run/secrets/prod-vault-pass
Key Points
- Encrypt files or single values; keep them in vault-only files.
- Vault password comes from outside the repo.
- Separate vault IDs per environment.
- Use
no_logon secret-handling tasks. - Consider an external secret manager with lookups.
Explanation
What interviewers look for
Layout (vault files referenced by plain variables), how the password is supplied in CI, no_log, and awareness of external secret managers.
More Ansible questions
- What is idempotency in Ansible, and how do you keep your own tasks idempotent?
- Write an Ansible playbook that installs Nginx, deploys its configuration, enables the service and restarts it only when the configuration changes.
- Explain Ansible's architecture: control node, inventory, modules, playbooks, roles and handlers.
- How would you automate TLS certificate installation and renewal with Ansible?
- How would you configure 100 Linux servers consistently with Ansible?
- How do Ansible and Terraform work together in one delivery pipeline?
- A playbook run fails halfway through 50 hosts. What do you do?
- How do you test Ansible roles before they reach production?