Lab 10: Ansible Capstone — Server Provisioning
Overview
Prerequisites
Step 1: Project Structure and Inventory Setup
docker run --rm ubuntu:22.04 bash -c "
apt-get update -qq 2>/dev/null && apt-get install -y -qq python3-pip python3 2>/dev/null
pip3 install ansible --quiet 2>/dev/null
mkdir -p /tmp/capstone/{roles,group_vars/all,host_vars,templates,tasks}
echo '=== Creating project structure ==='
find /tmp/capstone -type d | sort
cat > /tmp/capstone/inventory.ini << 'EOF'
[servers]
localhost ansible_connection=local
[servers:vars]
ansible_python_interpreter=/usr/bin/python3
EOF
cat > /tmp/capstone/ansible.cfg << 'EOF'
[defaults]
inventory = inventory.ini
host_key_checking = False
retry_files_enabled = False
stdout_callback = yaml
interpreter_python = auto_silent
gather_facts = smart
fact_caching = memory
roles_path = ./roles
[privilege_escalation]
become = False
become_method = sudo
EOF
cat > /tmp/capstone/group_vars/all/main.yml << 'EOF'
---
# Server identity
server_environment: production
server_timezone: UTC
# Users to create
server_users:
- name: deploy
comment: Deployment User
shell: /bin/bash
groups: [sudo]
ssh_key: ssh-rsa AAAA...examplekey deploy@ci
sudo_nopasswd: true
- name: appuser
comment: Application Service User
shell: /bin/bash
groups: []
ssh_key: ssh-rsa AAAA...appkey appuser@server
sudo_nopasswd: false
# Nginx configuration
nginx_port: 80
nginx_server_name: myapp.example.com
nginx_document_root: /var/www/html
nginx_worker_processes: auto
nginx_worker_connections: 1024
# Firewall rules
firewall_allowed_tcp_ports:
- 22
- 80
- 443
# Fail2ban configuration
fail2ban_maxretry: 5
fail2ban_bantime: 3600
fail2ban_findtime: 600
# Sysctl hardening
sysctl_settings:
net.ipv4.ip_forward: 0
net.ipv4.conf.all.accept_redirects: 0
net.ipv4.conf.all.send_redirects: 0
net.ipv4.conf.all.accept_source_route: 0
net.ipv4.tcp_syncookies: 1
net.ipv4.conf.all.log_martians: 1
kernel.randomize_va_space: 2
fs.suid_dumpable: 0
# Application service
app_service_name: mywebapp
app_service_user: appuser
app_service_exec: /usr/local/bin/mywebapp
app_service_port: 3000
# Log rotation
logrotate_app_logs:
- path: /var/log/myapp/*.log
rotate: 30
frequency: daily
compress: true
delaycompress: true
missingok: true
notifempty: true
EOF
echo '=== ansible.cfg ==='
cat /tmp/capstone/ansible.cfg
echo ''
echo '=== group_vars/all/main.yml ==='
cat /tmp/capstone/group_vars/all/main.yml
"Step 2: Users and SSH Keys
Step 3: Nginx Installation and Configuration
Step 4: Sysctl Hardening and Firewall Rules
Step 5: Fail2ban and Log Rotation
Step 6: Systemd Service Creation
Step 7: Validation Play
Step 8: Capstone — Complete Orchestrated Provisioning Playbook
Summary
Component
Playbook Pattern
Key Modules
Labs 06–10 Skill Matrix
Lab
Topic
Key Skill
Last updated
