Ansible: Automate Everything from First Playbook to Production · Roles and Ansible Galaxy
Project: Deploy a Web Application with Roles
Combine everything: a base role, a webserver role and a deploy role that together install packages, configure Nginx from a template, pull application code and restart services — across staging and production.
What you'll build
A multi-role playbook that deploys a web application to any environment. The base role installs packages, the webserver role configures Nginx, and the deploy role pulls the application code and restarts services. Environment differences are controlled entirely by group_vars.
The idea
Real-world deploys use multiple roles composed in a site playbook:
project/
├── ansible.cfg
├── inventory/
│ ├── staging.ini
│ └── production.ini
├── group_vars/
│ ├── staging.yml
│ └── production.yml
├── roles/
│ ├── common/ # base packages, users, SSH
│ ├── webserver/ # nginx install, config template
│ └── deploy/ # pull code, install deps, restart
└── site.yml # the entry point
Each role is independent and testable. The site playbook composes them in order. Environment-specific values live in group_vars/ — the roles never contain when: env == 'production' conditionals.
The code
mkdir -p webapp/{inventory,group_vars,roles/common/tasks,roles/common/defaults}
mkdir -p webapp/roles/webserver/{tasks,templates,handlers,defaults}
mkdir -p webapp/roles/deploy/{tasks,defaults}
# --- Inventory ---
cat > webapp/inventory/staging.ini <<'EOF'
[webservers]
localhost ansible_connection=local
EOF
# --- Group vars ---
cat > webapp/group_vars/staging.yml <<'EOF'
env_name: staging
app_repo: "https://github.com/example/webapp.git"
app_branch: develop
app_root: /opt/webapp
app_port: 3000
nginx_listen_port: 8080
nginx_server_name: staging.example.com
nginx_worker_connections: 512
EOF
cat > webapp/group_vars/production.yml <<'EOF'
env_name: production
app_repo: "https://github.com/example/webapp.git"
app_branch: main
app_root: /opt/webapp
app_port: 3000
nginx_listen_port: 80
nginx_server_name: app.example.com
nginx_worker_connections: 4096
EOF
# --- Common role ---
cat > webapp/roles/common/defaults/main.yml <<'EOF'
common_packages:
- curl
- git
- unzip
common_deploy_user: deploy
EOF
cat > webapp/roles/common/tasks/main.yml <<'EOF'
- name: Install common packages
ansible.builtin.debug:
msg: "Installing: {{ common_packages | join(', ') }}"
- name: Ensure deploy user exists
ansible.builtin.debug:
msg: "User '{{ common_deploy_user }}' ready"
EOF
# --- Webserver role ---
cat > webapp/roles/webserver/defaults/main.yml <<'EOF'
nginx_listen_port: 80
nginx_server_name: _
nginx_worker_connections: 1024
EOF
cat > webapp/roles/webserver/templates/nginx-app.conf.j2 <<'TMPL'
# {{ env_name }} — managed by Ansible
upstream app {
server 127.0.0.1:{{ app_port }};
}
server {
listen {{ nginx_listen_port }};
server_name {{ nginx_server_name }};
location / {
proxy_pass http://app;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /health {
return 200 'ok';
}
}
TMPL
cat > webapp/roles/webserver/tasks/main.yml <<'EOF'
- name: Install nginx
ansible.builtin.debug:
msg: "Would install nginx via apt"
- name: Deploy nginx site config
ansible.builtin.template:
src: nginx-app.conf.j2
dest: /tmp/nginx-app-{{ env_name }}.conf
mode: "0644"
notify: reload nginx
- name: Show deployed config
ansible.builtin.command: cat /tmp/nginx-app-{{ env_name }}.conf
register: nginx_conf
changed_when: false
- name: Display config
ansible.builtin.debug:
msg: "{{ nginx_conf.stdout }}"
EOF
cat > webapp/roles/webserver/handlers/main.yml <<'EOF'
- name: reload nginx
ansible.builtin.debug:
msg: "Would run: systemctl reload nginx"
EOF
# --- Deploy role ---
cat > webapp/roles/deploy/defaults/main.yml <<'EOF'
app_root: /opt/webapp
app_branch: main
EOF
cat > webapp/roles/deploy/tasks/main.yml <<'EOF'
- name: Pull application code
ansible.builtin.debug:
msg: "Would git clone {{ app_repo }} --branch {{ app_branch }} to {{ app_root }}"
- name: Install application dependencies
ansible.builtin.debug:
msg: "Would run: cd {{ app_root }} && npm ci --production"
- name: Restart application
ansible.builtin.debug:
msg: "Would run: systemctl restart webapp"
- name: Wait for application health check
ansible.builtin.debug:
msg: "Would wait for http://127.0.0.1:{{ app_port }}/health to return 200"
- name: Deployment summary
ansible.builtin.debug:
msg: >-
Deployed {{ app_branch }} to {{ env_name }}:
app on :{{ app_port }}, nginx on :{{ nginx_listen_port }},
server_name={{ nginx_server_name }}
EOF
# --- Site playbook ---
cat > webapp/site.yml <<'EOF'
- name: Deploy web application
hosts: webservers
gather_facts: false
roles:
- common
- webserver
- deploy
EOF
cd webapp
ansible-playbook -i inventory/staging.ini site.yml 2>/dev/null
Output
Run this on your computer — see “Get Started” for setup.
The staging deploy used the develop branch, port 8080, and the staging server name. Switching to production is one flag change:
ansible-playbook -i inventory/production.ini site.yml
The same roles execute with production group_vars: the main branch, port 80, and the production server name. No code change, no conditionals in the roles.
Common mistakes
Putting environment logic inside roles — roles should be environment-agnostic. Use group_vars/ to supply different values per environment. The role just uses {{ variable }}.
Not testing roles in isolation — before composing roles in a site playbook, test each one independently with a minimal playbook. Use Molecule for automated role testing.
Running the full site playbook when you only changed the deploy role — use --tags or --start-at-task to skip unchanged roles during development. Add tags to roles in the site playbook:
roles:
- { role: common, tags: ['common'] }
- { role: webserver, tags: ['webserver'] }
- { role: deploy, tags: ['deploy'] }
Then: ansible-playbook site.yml --tags deploy
Key points
- Compose multiple roles in a site playbook: each role handles one concern.
- Environment differences live in
group_vars/, not in role logic. - Use tags to run individual roles during development:
--tags deploy. - The same playbook targets any environment by swapping the inventory file.
Practice questions
How do you run only the deploy role without re-running common and webserver?
Answer
Add tags to roles in the site playbook, then run with --tags deploy. This skips roles that don't match the tag.
Why should environment-specific values live in group_vars instead of inside roles?
Answer
Roles should be environment-agnostic and reusable. group_vars supply different values per environment, so the same role works in staging and production without conditionals.
What is the advantage of using multiple inventory files (staging.ini, production.ini) instead of one?
Answer
Separate inventories prevent accidentally running a production deploy against staging hosts. The operator explicitly chooses the environment with the -i flag.
Open in the app to mark lessons complete, take notes and track your progress.