๐จ Build Inspiration & Tutorials¶
Ready to build? Here are 8 interactive tutorials to get you started with ANSAI.
๐ NEW: Interactive, executable tutorials now available โ
Infrastructure Automation¶
Auto-scale based on error rates
Goal: Automatically scale up resources when error rates spike, then scale down when stable.
๐ Try Interactive Tutorial โ
Blocks Used: - ๐ Log Analysis: Monitor Nginx/Apache logs for 500 errors. - ๐ค Orchestration: Trigger scaling action based on threshold. - ๐ฌ Notification: Alert team of scaling event.
How to Build:
1. Create Log Monitor: Use ansai-log-analyzer to tail logs and count 5xx errors/minute.
2. Set Threshold: If errors > 50/min, trigger scale_up.yml playbook.
3. Scale Up: Playbook spins up new container/instance or increases resource limits.
4. Scale Down: If errors < 10/min for 15 mins, trigger scale_down.yml.
Code Snippet:
ChatOps for infrastructure management
Goal: Control your infrastructure directly from Slack/Discord.
Blocks Used: - ๐ฌ Natural Language: Interpret "Restart web server" commands. - ๐ค Orchestration: Execute Ansible playbooks securely. - ๐ก๏ธ Security: Validate user permissions.
How to Build:
1. Setup Webhook: Create a simple Flask/FastAPI endpoint to receive Slack slash commands.
2. Parse Intent: Use LiteLLM to parse "Fix the database" into specific intent db_restart.
3. Execute: Trigger ansible-playbook playbooks/db-restart.yml.
4. Reply: Send "Database restarting..." back to Slack.
Code Snippet:
Multi-cloud orchestration with fallback
Goal: Deploy to AWS; if it fails, automatically deploy to DigitalOcean.
Blocks Used: - ๐ฏ Orchestration: Workflow logic with error handling. - โ๏ธ Cloud Modules: AWS and DigitalOcean Ansible collections. - ๐ง Notification: Report fallback status.
How to Build:
1. Try Primary: Run deploy-aws.yml inside a block.
2. Catch Failure: Use rescue to catch deployment errors.
3. Execute Fallback: Run deploy-do.yml in the rescue block.
4. Verify: Check health of fallback endpoint.
Code Snippet:
Cost optimization with intelligent scheduling
Goal: Shutdown dev environments at 7 PM and start them at 8 AM to save 50% costs.
Blocks Used: - โฐ Scheduling: Cron or Systemd timers. - โ๏ธ Cloud Modules: AWS/GCP instance state management. - ๐ค AI Prediction: (Optional) Don't shutdown if devs are still active.
How to Build:
1. Define Schedule: Create variables for start_time and stop_time.
2. Check Activity: (Optional) Check SSH logs for active sessions.
3. Stop Instances: Run playbook to stop tagged env:dev instances.
4. Start Instances: Run playbook to start them in the morning.
Code Snippet:
Security and Compliance¶
Compliance-as-code with auto-remediation
Goal: Automatically fix security misconfigurations (e.g., open SSH ports, root login).
Blocks Used: - ๐ Audit: Scan config files for violations. - ๐ค Orchestration: Apply hardening roles. - ๐ Reporting: Generate compliance report.
How to Build:
1. Scan: Check /etc/ssh/sshd_config for PermitRootLogin yes.
2. Remediate: Use lineinfile to set it to no.
3. Reload: Restart SSH service.
4. Report: Log the remediation action.
Code Snippet:
Automated disaster recovery testing
Goal: Randomly kill a service in staging to test if self-healing works (Chaos Engineering).
Blocks Used: - ๐ฒ Chaos: Randomly select and stop services. - ๐ก๏ธ Self-Healing: Verify the system recovers. - ๐ Reporting: Measure Time To Recovery (TTR).
How to Build:
1. Select Target: Randomly pick web, db, or cache.
2. Disrupt: systemctl stop <target>.
3. Wait: Wait 2 minutes.
4. Verify: Check if service is back up (self-healing should have kicked in).
5. Alert: If not back up, fail the test.
Code Snippet:
Advanced AI Ops¶
Self-optimizing database tuning
Goal: Adjust Postgres/MySQL config based on actual load patterns.
Blocks Used:
- ๐ Metrics: Prometheus/CloudWatch data.
- ๐ค AI Analysis: Analyze performance vs config.
- โ๏ธ Config Mgmt: Update postgresql.conf.
How to Build:
1. Gather Data: Collect cache hit ratio, active connections, memory usage.
2. AI Analyze: Ask LLM "Given these metrics and 32GB RAM, suggest work_mem setting."
3. Tune: Apply new config if different from current.
4. Restart: Gracefully reload DB.
Code Snippet:
Predictive maintenance with ML
Goal: Detect disk filling up trends and expand storage BEFORE it hits 100%.
Blocks Used: - ๐ Trend Analysis: Linear regression on disk usage. - โ๏ธ Cloud Modules: Expand EBS volume. - ๐ Filesystem: Resize file system online.
How to Build:
1. Analyze: Check disk usage over last 7 days.
2. Predict: "Will usage hit 95% in next 24 hours?"
3. Expand: If yes, modify AWS volume size +20%.
4. Resize: Run resize2fs.
Code Snippet: