Ansible
STDIO高级Ansible MCP服务器自动化
高级Ansible MCP服务器自动化
MCP Ansible Server
Advanced Ansible Model Context Protocol (MCP) server in Python exposing Ansible utilities for inventories, playbooks, roles, and project workflows.
Quick start
git clone https://github.com/bsahane/mcp-ansible.git cd mcp-ansible # Create and activate Python virtual environment python3 -m venv .venv source .venv/bin/activate # Install dependencies via requirements.txt python -m pip install -U pip pip install -r requirements.txt # (Optional) install the project package locally pip install -e . # Run the MCP server python src/ansible_mcp/server.py
Requirements
Setup
cd /Users/bsahane/Developer/cursor/mcp-ansible python3 -m venv .venv source .venv/bin/activate python -m pip install -U pip pip install "mcp[cli]>=1.2.0" "PyYAML>=6.0.1" "ansible-core>=2.16.0" pip install -e .
Run the server
python src/ansible_mcp/server.py
Cursor config (/Users/bsahane/.cursor/mcp.json)
{ "mcpServers": { "ansible-mcp": { "command": "python", "args": [ "/Users/bsahane/Developer/cursor/mcp-ansible/src/ansible_mcp/server.py" ], "env": { "MCP_ANSIBLE_PROJECT_ROOT": "/Users/bsahane/GitLab/projectAIOPS/mcp-ansible-server", "MCP_ANSIBLE_INVENTORY": "/Users/bsahane/GitLab/projectAIOPS/mcp-ansible-server/inventory/hosts.ini", "MCP_ANSIBLE_PROJECT_NAME": "projectAIOPS" } } } }
Claude for Desktop config
Add to ~/Library/Application Support/Claude/claude_desktop_config.json:
{ "mcpServers": { "ansible-mcp": { "command": "python", "args": [ "/Users/bsahane/Developer/cursor/mcp-ansible/src/ansible_mcp/server.py" ], "env": { "MCP_ANSIBLE_PROJECT_ROOT": "/Users/bsahane/GitLab/projectAIOPS/mcp-ansible-server", "MCP_ANSIBLE_INVENTORY": "/Users/bsahane/GitLab/projectAIOPS/mcp-ansible-server/inventory/hosts.ini", "MCP_ANSIBLE_PROJECT_NAME": "projectAIOPS" } } } }
Tools (names)
Core Ansible Tools:
Local inventory suite (no AAP/AWX):
Advanced Troubleshooting Suite:
Foundation Tools:
Intelligent Diagnostics:
Automation & Self-Healing:
Network & Security:
Performance & Monitoring:
Environment variables (optional)
Examples (Claude Tools)
List hosts from inventory:
Run a simple playbook:
Ad-hoc ping localhost:
Scaffold a role:
Register a project and run a project playbook:
Examples for local inventory suite
Parse via ansible.cfg with multiple inventories (merges group_vars/host_vars):
Parse a specific extensionless inventory file:
Ping a group from inventory:
Notes
Reference
Below are all tools with short descriptions, minimal args, an example question you can ask in the MCP UI, and a sample answer.
create-playbook: Create an Ansible playbook file from YAML string or object
{ "playbook": [{"hosts":"all","tasks":[{"debug":{"msg":"hi"}}]}] }
{ "path": "/tmp/playbook_x.yml", "bytes_written": 123, "preview": "- hosts: all..." }validate-playbook: Syntax check a playbook
{ "playbook_path": "/abs/playbook.yml" }
{ "ok": true, "rc": 0 }ansible-playbook: Run a playbook
{ "playbook_path": "/abs/playbook.yml", "inventory": "localhost," }
{ "ok": true, "rc": 0, "stdout": "PLAY [all]..." }ansible-task: Run an ad‑hoc module
{ "host_pattern": "localhost", "module": "ping", "inventory": "localhost," }
{ "ok": true, "stdout": "pong" }ansible-role: Execute a role via a temporary playbook
{ "role_name": "myrole", "hosts": "localhost", "inventory": "localhost," }
{ "ok": true, "rc": 0 }create-role-structure: Scaffold a role directory tree
{ "base_path": "/tmp", "role_name": "demo" }
{ "created": [".../tasks/main.yml", ...], "role_path": "/tmp/demo" }ansible-inventory: List hosts and groups from an inventory
{ "inventory": "/abs/inventory" }
{ "hosts": ["host01"], "groups": {"web": ["host01"]} }register-project: Register an Ansible project for reuse
{ "name": "proj", "root": "/abs/project", "make_default": true }
{ "path": "~/.config/mcp-ansible/config.json", "projects": ["proj"] }list-projects: Show registered projects
{}{ "default": "proj", "projects": {"proj": {"root": "/abs"}} }project-playbooks: Discover playbooks under a project root
{ "project": "proj" }
{ "ok": true, "playbooks": ["/abs/x.yml", "/abs/y.yml"] }project-run-playbook: Run a playbook using project inventory/env
{ "playbook_path": "/abs/x.yml", "project": "proj" }
{ "ok": true, "rc": 0 }inventory-parse: Parse inventories (ansible.cfg aware, merges group_vars/host_vars)
{ "project_root": "/abs/project", "include_hostvars": true }
{ "hosts": ["h1"], "groups": {"web":["h1"]}, "hostvars": {"h1": {...}} }inventory-graph: Show inventory graph
{ "project_root": "/abs/project" }
inventory-find-host: Show a host’s groups and merged vars
{ "project_root": "/abs/project", "host": "h1" }
{ "groups": ["web"], "hostvars": {"ansible_user":"root"} }ansible-ping: Ping hosts via ad-hoc
{ "project_root": "/abs/project", "host_pattern": "localhost" }
{ "ok": true, "rc": 0 }ansible-gather-facts: Run setup and return facts
{ "project_root": "/abs/project", "host_pattern": "localhost" }
{ "facts": {"localhost": {"ansible_hostname":"node"}} }validate-yaml: Validate YAML files
{ "paths": ["/abs/file.yml"] }
{ "ok": true, "results": [{"path":"/abs/file.yml","ok":true}] }galaxy-install: Install roles/collections from requirements
{ "project_root": "/abs/project" }
{ "ok": true, "executed": [{"kind":"collection","rc":0}] }project-bootstrap: Bootstrap project (env info + galaxy install)
{ "project_root": "/abs/project" }
{ "ok": true, "details": {"ansible_version":"..."} }inventory-diff: Diff two inventories
{ "left_project_root": "/abs/project", "right_project_root": "/abs/project" }
{ "added_hosts": [], "removed_hosts": [], "group_membership_changes": {} }ansible-test-idempotence: Run playbook twice and assert no changes second run
{ "playbook_path": "/abs/playbook.yml", "project_root": "/abs/project" }
{ "ok": true, "changed_total_second": 0 }galaxy-lock: Generate a lock file of installed roles/collections
{ "project_root": "/abs/project" }
{ "ok": true, "path": "/abs/requirements.lock.yml" }vault-encrypt / vault-decrypt / vault-view / vault-rekey: Vault operations
{ "file_paths": ["/abs/group_vars/all/vault.yml"], "project_root": "/abs/project" }
{ "ok": true, "rc": 0 }ansible-remote-command: Execute shell commands with enhanced parsing
{ "host_pattern": "webserver", "command": "ps aux | grep nginx" }
{ "ok": true, "stdout": "Process list...", "parsed_output": {...} }ansible-fetch-logs: Fetch and analyze log files
{ "host_pattern": "app*", "log_paths": ["/var/log/nginx/error.log"], "analyze": true }
{ "ok": true, "logs": {...}, "summary": {"total_logs": 1, "successful": 1} }ansible-service-manager: Service management with logs
{ "host_pattern": "web", "service_name": "nginx", "action": "restart", "check_logs": true }
{ "ok": true, "action_result": {...}, "status": {...}, "logs": {...} }ansible-diagnose-host: Comprehensive health assessment
{ "host_pattern": "production", "checks": ["system", "network", "security"], "include_recommendations": true }
{ "ok": true, "diagnosis": {...}, "health_score": {"score": 85, "level": "good"} }ansible-capture-baseline: Capture system state baseline
{ "host_pattern": "web*", "snapshot_name": "pre-deployment", "include": ["configs", "processes"] }
{ "ok": true, "snapshot_id": "snapshot_20250101_120000_abc123", "categories_captured": [...] }ansible-compare-states: Compare against baseline
{ "host_pattern": "web*", "baseline_snapshot_id": "snapshot_20250101_120000_abc123" }
{ "ok": true, "comparison": {"differences": {...}, "summary": {...}} }{ "host_pattern": "database", "symptoms": ["high_memory", "disk_full"], "max_impact": "medium", "dry_run": true }
{ "ok": true, "proposed_actions": [...], "summary": {"actionable_symptoms": 2} }ansible-network-matrix: Network connectivity testing
{ "host_patterns": ["web*", "db*"], "check_ports": [22, 3306, 443] }
{ "ok": true, "network_matrix": {...}, "summary": {"source_patterns": 2, "ports_tested": [22, 3306, 443]} }ansible-security-audit: Security vulnerability assessment
{ "host_pattern": "all", "audit_categories": ["packages", "permissions", "network"], "generate_report": true }
{ "ok": true, "audit": {...}, "security_assessment": {"score": 75, "level": "warning"} }ansible-health-monitor: Continuous monitoring with trends
{ "host_pattern": "production", "monitoring_duration": 300, "metrics_interval": 30 }
{ "ok": true, "monitoring": {"trend_analysis": {...}, "anomalies": []} }ansible-performance-baseline: Performance benchmarking
{ "host_pattern": "web*", "benchmark_duration": 60, "store_baseline": true }
{ "ok": true, "baseline": {"benchmarks": {...}, "performance_assessment": {"score": 90}} }ansible-log-hunter: Advanced log correlation
{ "host_pattern": "app*", "search_patterns": ["ERROR", "CRITICAL"], "time_range": "1h", "correlation_window": 300 }
{ "ok": true, "hunt_results": {...}, "correlation": {"correlated_events": 3} }All troubleshooting tools maintain the same safety, auditability, and Ansible-native approach while providing enterprise-grade automation capabilities.