Write a role structure for a Node.js app and show what goes in each folder.
IntermediateHands-onWrite-an-Ansible-playbook tasks
Answer
roles/nodeapp/
defaults/main.yml # lowest-priority vars users override: app_port: 3000
vars/main.yml # internal constants, high priority
tasks/main.yml # the steps
handlers/main.yml # restart nodeapp
templates/app.service.j2
files/ # static files to copy
meta/main.yml # dependencies, platforms
# site.yml
- hosts: app
become: true
roles:
- role: nodeapp
vars:
app_port: 8080
Scaffold with ansible-galaxy role init roles/nodeapp.
Key trick → Put tunables in defaults/ (easy to override) and not vars/ (hard to override). That one distinction comes up in almost every Ansible interview.
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 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.AdvancedEncrypt a database password with Ansible Vault and use it in a playbook.Beginner