- Posted on
- • Artificial Intelligence
Artificial Intelligence for DevOps Engineers
- Author
-
-
- User
- linuxbash
- Posts by this author
- Posts by this author
-
Artificial Intelligence for DevOps Engineers: Bash-First, Practical, and Private
You know the drill: a pipeline fails at 3:07 AM. The log is 20,000 lines long. The root cause is buried somewhere in a sea of retries and timeouts. What if you had a tireless teammate to summarize logs, propose fixes, draft runbooks, and even write clean commit messages—right from your terminal?
This article shows how to add AI into your day-to-day DevOps workflow using simple Bash wrappers and tools you already use. We’ll focus on pragmatic, low-friction integrations, with options for both local models (privacy-first) and cloud APIs.
Why this matters:
DevOps work is text-heavy: logs, diffs, YAML, alerts. LLMs excel at summarization, pattern extraction, and drafting.
You can keep humans in the loop. Let AI propose, you approve.
Local models are now good enough for many tasks—reducing data exposure and cost.
Below are 4 actionable, real-world workflows you can drop into your dotfiles or CI today.
Prerequisites and Installation
We’ll use:
curl, jq, git (plumbing)
Python + pip (optional, for API clients)
Podman (optional, containerized Ollama)
Ollama (local LLM) or OpenAI (cloud API)
Install base tools:
- Debian/Ubuntu (apt)
sudo apt-get update
sudo apt-get install -y curl git jq python3 python3-pip podman
- Fedora/RHEL (dnf)
sudo dnf install -y curl git jq python3 python3-pip podman
- openSUSE (zypper)
sudo zypper refresh
sudo zypper install -y curl git jq python3 python3-pip podman
Install a local LLM with Ollama (recommended if you want data privacy and offline use):
curl -fsSL https://ollama.com/install.sh | sh
ollama pull llama3
Alternatively, run Ollama in a container (using Podman):
podman run -d --name ollama -p 11434:11434 -v ollama:/root/.ollama docker.io/ollama/ollama
# Optional: pull model inside the container
podman exec -it ollama ollama pull llama3
If using the container, set:
export OLLAMA_HOST="http://localhost:11434"
Using OpenAI instead of a local model? Set your API key:
export OPENAI_API_KEY="sk-..."
Tip: You can use both. The helper function below will prefer Ollama if it’s installed and fall back to OpenAI if OPENAI_API_KEY is set.
A single Bash helper to talk to an LLM
Drop this in your ~/.bashrc or scripts repo. It reads from stdin or command arguments. If Ollama is installed, it uses it; otherwise, it uses OpenAI via curl.
ai() {
# Usage:
# echo "Summarize this text" | ai
# ai "Write a concise commit message for the following diff: ..."
local input
if [ -t 0 ]; then
input="$*"
else
input="$(cat)"
fi
if command -v ollama >/dev/null 2>&1; then
# Local model (privacy-friendly)
printf "%s" "$input" | ollama run "${LLM_MODEL:-llama3}"
elif [ -n "${OPENAI_API_KEY:-}" ]; then
# OpenAI fallback (requires jq)
jq -n \
--arg model "${OPENAI_MODEL:-gpt-4o-mini}" \
--arg content "$input" \
'{model:$model, messages:[{role:"user", content:$content}]}' \
| curl -sS https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer '"$OPENAI_API_KEY"'" \
-d @- \
| jq -r '.choices[0].message.content'
else
echo "No LLM configured (install ollama or set OPENAI_API_KEY)" >&2
return 1
fi
}
Optional tuning:
export LLM_MODEL="llama3" # For Ollama (e.g., llama3, qwen2, codellama)
export OPENAI_MODEL="gpt-4o-mini"
1) Generate meaningful commit messages and PR descriptions
Conventional commit messages speed up code review and changelog generation. Let an LLM craft a crisp message from your staged diff—then you approve.
Generate a commit message:
git add -A
git diff --staged | ai "Write a single-line conventional commit message (type: feat, fix, chore, docs, refactor, test) summarizing the following diff. Keep it under 72 characters. No trailing period."
Approve and commit:
msg="$(git diff --staged | ai "Write a single-line conventional commit message (type: feat, fix, chore, docs, refactor, test). Under 72 chars. No trailing period.")"
git commit -m "$msg"
Draft a PR description with risks and test instructions:
git diff main...HEAD | ai "Generate a PR description with: Summary, Motivation, Risks, Test Plan, Rollback Plan based on this diff."
Why it works:
It’s fast and consistent.
Humans still sign off—no auto-committing without review.
2) Auto-triage CI failures with a root-cause summary and likely fixes
Pipe the last part of a build log to the model and ask for a structured, actionable summary:
tail -n 400 build.log | ai "You are a CI failure triage assistant. Identify the root cause and list 3 likely fixes with specific commands or file edits. Use concise bullets. If flaky, say how to quarantine."
Add a quick wrapper script:
#!/usr/bin/env bash
set -euo pipefail
LOGFILE="${1:-build.log}"
tail -n 800 "$LOGFILE" \
| ai "Summarize the failure (root cause, affected component, minimal repro). Then propose 3 concrete fixes with commands. If infra-related (network, permission, quota), highlight that."
Real-world payoff:
Faster time-to-first-fix.
Great for onboarding: new team members get actionable context immediately.
3) Draft incident runbooks directly from live system context
When incidents happen, first responders need quick, consistent guidance. Use AI to turn raw system context into a runbook draft that your SRE lead can refine.
Collect context and draft:
#!/usr/bin/env bash
set -euo pipefail
collect() {
echo "== Host =="
uname -a
echo
echo "== Uptime and Load =="
uptime
echo
echo "== Disk =="
df -h
echo
echo "== Memory =="
free -m
echo
echo "== Services (failed) =="
systemctl --failed || true
echo
echo "== Containers (podman) =="
podman ps --format 'table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}' || true
echo
echo "== Recent journal (errors) =="
journalctl -p 3 -n 100 --no-pager || true
}
collect | ai "You are a senior SRE. Produce a short incident runbook in Markdown for this host context. Include: Symptoms, Impact, Probable Root Causes, Verification Commands, Remediation Steps, and a Rollback/Recovery section. Keep it focused and copy-paste friendly."
Benefits:
Standardizes response quality during high stress.
Builds a knowledge base as you iterate on the AI draft.
4) Natural-language to Bash—with a safe, human-in-the-loop runner
Let AI propose a command but never run it automatically. You approve explicitly after reading the explanation.
suggest() {
# Example: suggest "Find top 10 largest files under /var/log excluding gz"
local prompt="$*"
local plan
plan="$(ai "Turn this request into a safe Bash one-liner with explanation. Use standard tools (find, du, awk, grep). Return two sections:
Explanation:
Command:
Request: $prompt")"
echo "$plan"
echo
read -r -p "Run the Command above? [y/N] " ans
if [[ "$ans" =~ ^[Yy]$ ]]; then
# Extract the command line between 'Command:' and end
cmd="$(printf "%s\n" "$plan" | awk '/^Command:/{flag=1;next}/^$/{flag=0}flag')"
echo "+ $cmd"
eval "$cmd"
else
echo "Aborted."
fi
}
Why it’s safe:
Human approval is required.
The model explains intent, making risky actions obvious.
You can sandbox (e.g., run in a container or on a test host).
Security, Privacy, and Reliability Considerations
Prefer local inference for sensitive data. Ollama keeps data on your machine.
If using cloud APIs, avoid sending secrets/logs with credentials. Redact with grep/sed first.
Keep humans in the loop for any action that changes infrastructure (deployments, deletions).
Log AI outputs used in incidents/changes for auditability.
Validate generated config with existing tooling (linters, unit tests, canaries).
Troubleshooting
Model quality: If outputs are weak, try a code-oriented model locally (e.g., CodeLlama) or switch OPENAI_MODEL to a stronger tier.
Long inputs: Trim irrelevant log noise (e.g., timestamps, ANSI codes) before sending.
Networking: If Ollama runs in a container, ensure OLLAMA_HOST is set and reachable.
JSON escaping: The ai function uses jq to safely build requests.
Conclusion and Next Steps
Start small: pick one painful, text-heavy task (commit messages or CI triage) and wire the ai function into your dotfiles. Keep privacy top-of-mind: default to a local model, and only use cloud APIs when appropriate. Build from there—codify what works into your team runbooks and CI jobs.
Call to action:
Install the prerequisites using your package manager.
Add the ai helper function to your shell startup file.
Pilot one workflow this week (commit messages or CI triage).
Gather feedback, iterate prompts, and standardize successful patterns across your team.
Your terminal just got a new teammate. Use it wisely—and sleep better on incident nights.