Automation¶
Automate infrastructure tasks with Merlya's scripting and scheduling capabilities.
Non-Interactive Mode¶
Run Merlya commands without interactive prompts:
# Single command
merlya run "Check disk space on all web servers"
# From file
merlya run --file tasks.txt
# With confirmation disabled (use with caution)
merlya run --yes "Restart nginx on web-01"
# Choose model role: brain (complex reasoning) or fast (quick tasks)
merlya run --model fast "List all hosts"
merlya run -m brain "Analyze this incident and suggest remediation"
Task Files¶
Create reusable task files:
# tasks/daily-checks.yml
name: Daily Infrastructure Checks
model: fast # Default model for all tasks (optional)
tasks:
- description: Check disk space
prompt: Check disk usage on all servers, warn if above 80%
- description: Check memory
prompt: Check memory usage on all servers
- description: Analyze anomalies
prompt: Analyze any anomalies found and suggest fixes
model: brain # Override for complex analysis
Run the task file:
# Use models defined in file
merlya run --file tasks/daily-checks.yml
# Override all tasks to use fast model
merlya run --file tasks/daily-checks.yml --model fast
Model Selection Priority¶
- CLI
--modelargument (highest priority) - Task-level
modelfield in YAML - File-level
modelfield in YAML - Default configured model (lowest priority)
Cron Integration¶
Schedule Merlya tasks with cron:
# Edit crontab
crontab -e
# Daily health check at 6 AM
0 6 * * * /usr/local/bin/merlya run "Run daily health checks" >> /var/log/merlya-daily.log 2>&1
# Hourly disk check
0 * * * * /usr/local/bin/merlya run "Check disk space, alert if above 90%" >> /var/log/merlya-disk.log 2>&1
Webhooks¶
Trigger Merlya from webhooks:
#!/bin/bash
# webhook-handler.sh
PAYLOAD=$(cat)
EVENT=$(echo $PAYLOAD | jq -r '.event')
case $EVENT in
"deploy")
merlya run "Deploy latest version to staging servers"
;;
"rollback")
merlya run "Rollback to previous version on staging"
;;
"scale-up")
merlya run "Start 2 additional web servers"
;;
esac
CI/CD Integration¶
GitHub Actions¶
name: Deploy with Merlya
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Merlya
run: pip install merlya
- name: Configure Merlya
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
merlya config set model.provider openai
merlya config set model.model gpt-4o-mini
- name: Deploy
run: |
merlya run --yes "Deploy the latest code to production servers"
GitLab CI¶
deploy:
stage: deploy
script:
- pip install merlya
- export OPENAI_API_KEY="$OPENAI_API_KEY"
- merlya config set model.provider openai
- merlya config set model.model gpt-4o-mini
- merlya run --yes "Deploy version $CI_COMMIT_TAG to production"
only:
- tags
Output Formats¶
Control output format for automation:
# JSON output (for parsing)
merlya run --format json "List all servers with high CPU"
# Quiet mode (only results)
merlya run --quiet "Check nginx status"
# Verbose mode (full logging)
merlya --verbose run "Deploy to production"
JSON Output Example¶
{
"success": true,
"total": 1,
"passed": 1,
"failed": 0,
"tasks": [
{
"task": "Check disk space on web-01",
"success": true,
"message": "…",
"actions": ["ssh_execute"],
"data": null,
"task_type": "agent"
}
]
}
Error Handling¶
Handle errors in scripts:
#!/bin/bash
set -e
# Run with error handling
if merlya run --quiet "Check if all services are healthy"; then
echo "All services healthy"
else
echo "Service check failed!"
merlya run "Send alert to ops team about service failures"
exit 1
fi
Logging¶
Configure logging for automation:
# Set console log level (alias: logging.level)
merlya config set logging.console_level debug
# Set file log level
merlya config set logging.file_level debug
# Run with specific log file
MERLYA_LOG_FILE=/tmp/task.log merlya run "Check servers"
Best Practices¶
- Use
--yescarefully - Only for well-tested tasks - Log everything - Keep records of automated actions
- Set timeouts - Prevent hanging tasks
- Use JSON output - For reliable parsing
- Test in staging - Before automating production tasks
- Monitor failures - Set up alerts for failed tasks
Examples¶
Daily Backup Check¶
#!/bin/bash
# check-backups.sh
LOG_FILE="/var/log/merlya/backup-check.log"
echo "$(date): Starting backup check" >> $LOG_FILE
RESULT=$(merlya run --format json "Verify all database backups from last 24h exist and are valid")
if echo $RESULT | jq -e '.success' > /dev/null; then
echo "$(date): Backup check passed" >> $LOG_FILE
else
echo "$(date): Backup check FAILED" >> $LOG_FILE
merlya run "Send urgent alert: backup verification failed"
fi
Auto-Scaling¶
#!/bin/bash
# auto-scale.sh
CPU=$(merlya run --format json "Get average CPU across web tier" | jq '.cpu_percent')
if (( $(echo "$CPU > 80" | bc -l) )); then
merlya run --yes "Scale up web tier by 2 instances"
elif (( $(echo "$CPU < 20" | bc -l) )); then
merlya run --yes "Scale down web tier by 1 instance (keep minimum 2)"
fi