Interview › Infrastructure as Code (Terraform, Ansible)
What is Ansible Vault, and how do you protect secrets with it?
Infrastructure as Code (Terraform, Ansible) · Intermediate level
Answer
Ansible Vault encrypts sensitive YAML values or files so secrets can be stored with playbooks without being readable in plaintext. I use vault IDs, separate secrets by environment, avoid printing secret values, and integrate decryption with CI/CD using controlled credentials.
Technical explanation
Vault can encrypt whole files or individual values.
Use no_log for tasks that might print decrypted values.
Rotate vault passwords or vault identities according to your secrets policy.
Prefer idempotent modules over shell so repeated runs are safe and change reporting is meaningful.
Separate reusable role logic from inventory-specific variables so the same automation works across environments.
Run lint, syntax checks, check mode where useful, and staged rollouts before production-wide changes.
Hands-on example
1. Model variables and facts for: What is Ansible Vault, and how do you protect secrets with it?
2. Create inventory variables:
group_vars/web.yml:
app_port: 8080
package_name_by_os:
RedHat: httpd
Debian: apache2
host_vars/web1.yml:
app_port: 9090
3. Use facts and variables in a task:
- name: Install OS-specific web package
ansible.builtin.package:
name: "{{ package_name_by_os[ansible_facts['os_family']] }}"
state: present
when: ansible_facts['os_family'] in package_name_by_os
4. Run ansible-playbook site.yml -e app_port=7070 in a lab to see extra vars override lower-precedence values.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Infrastructure as Code (Terraform, Ansible) interview questions
- What is Infrastructure as Code, and what problems does it solve over click-ops?
- What is the difference between declarative and imperative IaC, and where do Terraform and Ansible fall?
- What is the difference between configuration management and provisioning?
- What is Terraform, and what is the core plan/apply workflow?
- What does terraform init do?
- What is the Terraform state file, and why is it critical?
- Why should state be stored remotely, and what backend would you use on AWS?
- What is state locking, and why does it matter for teams?