Ansible MCP Server

Ansible MCP servers enable AI models to interact with Ansible, providing capabilities for infrastructure automation, configuration management, and application deployment.

GitHub stars

Overview

The MCP Ansible Server is a Python-based server that allows AI models to interact with Ansible's powerful automation capabilities. It exposes Ansible utilities for managing inventories, executing playbooks, and running roles, providing a safe and structured way for AI to automate infrastructure and deployments.

Created by:

Developed by bsahane

Key Features

📚

Playbook Execution

Execute Ansible playbooks to automate tasks and deployments

📦

Inventory Management

Manage and query Ansible inventories for host and group information

⚙️

Role Execution

Run Ansible roles for modular and reusable automation

Syntax Validation

Validate Ansible playbook syntax before execution

Available Tools

Quick Reference

ToolPurposeCategory
create-playbookCreate playbooks from YAML strings or dictsPlaybook
validate-playbookValidate playbook syntaxPlaybook
ansible-playbookExecute playbooksPlaybook
ansible-taskRun ad-hoc tasksAd-hoc
ansible-roleExecute rolesRole
ansible-inventoryList inventory hosts and groupsInventory

Detailed Usage

ansible-playbook

Execute Ansible playbooks.

use_mcp_tool({
  server_name: "ansible",
  tool_name: "ansible-playbook",
  arguments: {
    playbook_path: "/path/to/your/playbook.yml",
    inventory: "/path/to/your/inventory/hosts.ini"
  }
});
ansible-task

Run ad-hoc Ansible tasks.

use_mcp_tool({
  server_name: "ansible",
  tool_name: "ansible-task",
  arguments: {
    host_pattern: "localhost",
    module: "ping"
  }
});
ansible-inventory

List inventory hosts and groups.

use_mcp_tool({
  server_name: "ansible",
  tool_name: "ansible-inventory",
  arguments: {
    inventory: "/path/to/your/inventory/hosts.ini"
  }
});

Installation

{
  "mcpServers": {
    "ansible": {
      "command": "python",
      "args": [
        "/path/to/mcp-ansible/src/ansible_mcp/server.py"
      ],
      "env": {
        "MCP_ANSIBLE_PROJECT_ROOT": "/path/to/your/ansible/project",
        "MCP_ANSIBLE_INVENTORY": "/path/to/your/ansible/inventory/hosts.ini"
      }
    }
  }
}

Setup:

Clone the mcp-ansible repository and install dependencies:

git clone https://github.com/bsahane/mcp-ansible.git
cd mcp-ansible
python3 -m venv .venv
source .venv/bin/activate
pip install -U pip
pip install "mcp[cli]>=1.2.0" "PyYAML>=6.0.1" "ansible-core>=2.16.0"
pip install -e .

Then, update the command and env paths in the mcpServers configuration above to match your setup.

Common Use Cases

1. Configuration Management

Automate system configuration and package management:

use_mcp_tool({
  server_name: "ansible",
  tool_name: "ansible-playbook",
  arguments: {
    playbook_path: "/path/to/playbooks/configure_webserver.yml",
    inventory: "/path/to/inventory/production.ini"
  }
});

2. Infrastructure Automation

Provision and manage cloud resources:

use_mcp_tool({
  server_name: "ansible",
  tool_name: "ansible-playbook",
  arguments: {
    playbook_path: "/path/to/playbooks/provision_vms.yml",
    inventory: "/path/to/inventory/cloud.ini",
    extra_vars: {
      cloud_provider: "aws",
      region: "us-east-1"
    }
  }
});

3. Application Deployment

Deploy applications with rolling updates:

use_mcp_tool({
  server_name: "ansible",
  tool_name: "ansible-playbook",
  arguments: {
    playbook_path: "/path/to/playbooks/deploy_app.yml",
    inventory: "/path/to/inventory/staging.ini",
    tags: "deploy,restart_service"
  }
});