Interview Prep/Real-World Interview/Write-an-Ansible-playbook tasks

Encrypt a database password with Ansible Vault and use it in a playbook.

BeginnerHands-onWrite-an-Ansible-playbook tasks
Answer
ansible-vault encrypt_string 'S3cr3t!' --name 'db_password' >> group_vars/prod/vault.yml
ansible-playbook site.yml --vault-password-file ~/.vault_pass   # or --ask-vault-pass
- name: Configure app
  ansible.builtin.template:
    src: app.env.j2
    dest: /etc/app/app.env
    mode: "0600"
  no_log: true

Key trick → encrypt_string encrypts a single value, so the rest of the vars file stays reviewable in PRs. no_log: true keeps the secret out of task output.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-an-Ansible-playbook tasks