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.
More on Write-an-Ansible-playbook tasks
Write a playbook that installs and starts Nginx on Ubuntu web servers.BeginnerWrite an inventory with groups, a group of groups, and per-group variables.BeginnerWrite a playbook that deploys a config file from a Jinja2 template and restarts the service only if it changed.BeginnerWrite a role structure for a Node.js app and show what goes in each folder.IntermediateWrite a task that creates multiple users with SSH keys from a list.BeginnerWrite a rolling deploy playbook: 2 servers at a time, removed from the load balancer during the update.Advanced