使用说明
# mcp_ansible
Ansible为MCP Tool Server(HTTP) streamable-http)提供的项目。
1.一目了然
| 项目 | 内容 |
|---|---|
| 目的 | AI playbook_id/inventory_id 仅基于安全运行Ansible |
执行方式查询类型为 run_playbook_apply,变更为 run_playbook_check 审阅后 run_playbook_apply |
|
| 策略文件 | policy/registry_playbooks.yaml, policy/registry_inventories.yaml |
| 入口点 | python -m mcp_ansible.main |
2.核心原则
- 禁止直接输入任意文件路径
- allow-list(ID)运行
- 记录审核日志(JSON line)
- 自动清理runs artifact(基于存档数量)
3.项目结构
mcp_ansible/
├─ main.py
├─ api/
│ ├─ mcp_router.py
│ └─ tools/
│ ├─ register.py
│ ├─ catalog_tools.py
│ ├─ run_tools.py
│ └─ context.py
├─ application/
│ ├─ env_loader.py
│ ├─ logging_config.py
│ ├─ registry_loader.py
│ └─ runner_wrapper.py
├─ domain/
│ └─ schemas.py
└─ policy/
├─ registry_playbooks.yaml
├─ registry_inventories.yaml
└─ Playbook/
4.安装
pip install -e .
或
uv sync
5.部署封闭网络服务器(建议的过程)
5.1准备条件
- 封闭网络服务器Linux环境(例如Rocky9)
- 可以使用Python 3.11
- 准备Ansible执行环境
- 方法A:在服务器上
ansible-core已安装 - 方法B:脱机安装时
requirements-offline-with-ansible214.txt使用
- 方法A:在服务器上
5.2在可上网的环境中准备软件包
Linux/macOS:
cd mcp_ansible
bash ./scripts/build_wheelhouse.sh
ansible-core包括:
cd mcp_ansible
REQUIREMENTS=requirements-offline-with-ansible214.txt bash ./scripts/build_wheelhouse.sh
Windows PowerShell:
cd mcp_ansible
powershell -ExecutionPolicy Bypass -File .\scripts\build_wheelhouse.ps1
ansible-core包括:
cd mcp_ansible
powershell -ExecutionPolicy Bypass -File .\scripts\build_wheelhouse.ps1 -Requirements requirements-offline-with-ansible214.txt
5.3复制到封闭网络服务器
复制以下内容。
mcp_ansible/wheelhouse/
5.4在封闭网络服务器上脱机安装
Linux:
cd /engn/mcp/mcp_ansible
bash ./scripts/install_offline.sh
ansible-core 嵌入式安装:
cd /engn/mcp/mcp_ansible
REQUIREMENTS=requirements-offline-with-ansible214.txt bash ./scripts/install_offline.sh
Windows PowerShell:
cd D:\MCPTools\mcp_ansible
powershell -ExecutionPolicy Bypass -File .\scripts\install_offline.ps1
ansible-core包括:
cd D:\MCPTools\mcp_ansible
powershell -ExecutionPolicy Bypass -File .\scripts\install_offline.ps1 -Requirements requirements-offline-with-ansible214.txt
5.5 .env 设置(重要)
policy/*.yaml的路径使用环境变量替换,因此将其设置为实际放置路径。
例子:
ANSIBLE_MCP_HOST=0.0.0.0
ANSIBLE_MCP_PORT=5000
ANSIBLE_MCP_HTTP_PATH=/mcp
ANSIBLE_MCP_PLAYBOOK_ROOT=/engn/ansible/playbook
ANSIBLE_MCP_INVENTORY_ROOT=/engn/ansible
ANSIBLE_CONFIG=/engn/ansible/ansible.cfg
ANSIBLE_MCP_RUNS_DIR=/var/lib/ansible-mcp/runs
ANSIBLE_MCP_STDOUT_MAX_LINES=300
ANSIBLE_MCP_LOG_LEVEL=INFO
ANSIBLE_MCP_LOG_DIR=/var/log/ansible-mcp
ANSIBLE_MCP_LOG_MAX_BYTES=10485760
ANSIBLE_MCP_LOG_BACKUP_COUNT=5
如果使用自定义env路径:
ANSIBLE_MCP_ENV_FILE=/etc/ansible-mcp/.env ./mcp_ansible/.venv/bin/python -m mcp_ansible.main
5.6服务器启动/停止/状态检查
机动:
cd /engn/mcp/mcp_ansible
bash ./scripts/start.sh
停止:
cd /engn/mcp/mcp_ansible
bash ./scripts/stop.sh
检查状态:
cd /engn/mcp/mcp_ansible
bash ./scripts/status.sh
5.7动作确认顺序
list_registered_playbooksget_playbook_schemalist_registered_inventorieslist_inventory_hosts(查看实际受控服务器列表)- 查询式诊断playbook
run_playbook_apply - 变更型playbook
run_playbook_check后必要时run_playbook_apply
6.MCP注册示例
[mcp_servers.ansible_http]
transport = "streamable_http"
url = "http://<ANSIBLE_SERVER_IP>:5000/mcp"
7.提供工具
| Tool | 说明 |
|---|---|
list_registered_playbooks playbook摘要列表(ID,description) |
|
get_playbook_schema |
特定playbook详细信息(path, inputs) |
list_registered_inventories |
inventory列表 |
list_inventory_hosts |
基于inventory文件的实际主机/组列表 |
run_playbook_check |
运行check模式(用于变更playbook预览) |
run_playbook_apply 运行apply模式(包括查询式playbook实际运行) |
8.运营者查询-Playbook映射
| 问题类型(示例) | 建议的Playbook ID | 运行时核心输入示例 |
|---|---|---|
| 验证服务器连接/SSH连接 | ping_all |
limit: web01 |
检查服务器基本信息(CPU/Mem/FS/User/Group) gather_facts_only |
limit: all |
|
| 创建/删除帐户/更改Shell | user_account_manage |
user_name: opsuser, user_state: present |
| 添加/删除/查询sudo权限 | sudoers_manage |
sudo_action: audit 或 sudo_action: present, sudo_user: opsuser |
| 完整更新软件包/安装/删除特定软件包 | package_manage |
package_action: install, package_names: ["nginx"] |
| 服务启动/停止/重新启动/激活 | systemd_service_manage |
service_name: nginx, service_state: restarted, service_enabled: true |
| 检查指定目录文件列表 | file_list_files |
target_dir: /var/log |
| 检查指定文件的最后N行 | file_tail_file |
target_file: /var/log/messages, tail_lines: 100 |
| 在指定文件中搜索ERROR/关键字 | file_grep_pattern |
target_file: /var/log/messages, search_pattern: ERROR |
| 在systemd日志中检查服务错误 | journal_service_errors |
service_name: nginx, journal_lines: 100 |
| 检查CPU使用情况 | process_top_cpu |
top_n: 20 |
| 检查内存消耗量大的进程 | process_top_memory |
top_n: 20 |
| 检查服务详细状态和最新日志 | service_status_detail |
service_name: nginx, journal_lines: 50 |
| 检查最近的系统错误日志 | journal_recent_errors |
since: -30m, journal_lines: 100 |
| 查看最近的内核消息 | kernel_recent_messages |
message_lines: 100 |
| 验证目录使用情况父项 | directory_usage_snapshot |
target_dir: /var, max_depth: 2, top_n: 20 |
| 检查特定PID的thread特定CPU | process_threads_cpu |
pid: 1234, top_n: 20 |
| 查看特定PID的运行详细信息 | process_runtime_detail |
pid: 1234 |
| 检查当前TCP/UDP连接状态 | socket_connection_snapshot |
top_n: 100 |
| 检查磁盘I/O状态 | disk_io_snapshot |
sample_interval: 1, sample_count: 3 |
| 查看最近更改的文件列表 | file_recent_changes |
target_dir: /var/log, top_n: 20 |
| 检查特定端口侦听器 | port_listener_detail |
target_port: 8080 |
| 检查每个远程IP的父连接数 | connection_top_remote_ips |
top_n: 20 |
| 检查网络接口状态 | network_interface_detail |
interface_name: eth0 |
8.1推荐运行模式
- 查询型playbook
command/shell有很多基础任务,可以在Ansible check模式下跳过。 - 因此,下面的playbook 只读但是,为了实际查询
run_playbook_apply建议使用。ping_all,gather_facts_onlyfile_list_files,file_tail_file,file_grep_pattern,file_recent_changesprocess_top_cpu,process_top_memory,process_threads_cpu,process_runtime_detailservice_status_detail,journal_service_errors,journal_recent_errors,kernel_recent_messagesdirectory_usage_snapshot,disk_io_snapshotsocket_connection_snapshot,port_listener_detail,connection_top_remote_ips,network_interface_detail
- 更改帐户/包/服务状态的变更playbook
run_playbook_check先用检查run_playbook_apply使用。
9.输入/输出规则
输入
- 执行
playbook_id,inventory_id标准 extra_vars是JSON objectextra_vars是playbookinputs基于模式的运行时验证(必需值/类型/enum)- 策略文件的
path支持替换环境变量
输出
run_idstatusrchost_summaryfailuresartifact_dirstdout(默认:tail N行)stdout_line_countstdout_truncated
10.主要环境变量
| 变量 | 说明 |
|---|---|
ANSIBLE_MCP_HOST, ANSIBLE_MCP_PORT, ANSIBLE_MCP_HTTP_PATH |
绑定服务器 |
ANSIBLE_MCP_ENV_FILE |
定制 .env 路径 |
ANSIBLE_MCP_PLAYBOOK_ROOT |
playbook根路径 |
ANSIBLE_MCP_INVENTORY_ROOT |
inventory根路径 |
ANSIBLE_CONFIG |
ansible.cfg绝对路径(建议固定) |
ANSIBLE_MCP_RUNS_DIR |
保存runs artifact的路径 |
ANSIBLE_MCP_STDOUT_MAX_LINES |
Tool响应中要包含的stdout最大行数(0以下:完整) |
ANSIBLE_MCP_RUNS_BACKUP_COUNT |
未设置runs电子仓库计数 LOG_BACKUP_COUNT 使用) |
ANSIBLE_MCP_LOG_LEVEL |
日志级别 |
ANSIBLE_MCP_LOG_DIR |
默认日志目录(app.log, audit.log) |
ANSIBLE_MCP_LOG_FILE |
应用程序日志文件路径 |
ANSIBLE_MCP_AUDIT_LOG_FILE |
审计日志文件路径 |
ANSIBLE_MCP_LOG_MAX_BYTES |
日志轮换大小 |
ANSIBLE_MCP_LOG_BACKUP_COUNT |
日志备份计数 |
11.运营建议
policy/*.yaml只修改管理员- apply前检查结果
- 最小化log/runs目录权限
- 使用Vault/外部Secret Manager获取机密信息
12.后台运行(Linux)
nohup 提供基于后台执行脚本。
bash ./scripts/start.sh
主要环境变量:
APP_DIR(默认:基于脚本的项目根)PYTHON_BIN(默认:$APP_DIR/.venv/bin/python)LOG_DIR(默认:/var/log/ansible-mcp)PID_FILE(默认:$APP_DIR/ansible-mcp.pid)
停止:
bash ./scripts/stop.sh
检查状态:
bash ./scripts/status.sh
13.解决部署问题
12.1阻止执行PowerShell脚本
症状:
running scripts is disabled on this system
解决方案:
powershell -ExecutionPolicy Bypass -File .\scripts\build_wheelhouse.ps1
12.2在Windows上强制下载Linux wheel pywin32 错误
症状:
No matching distribution found for pywin32>=310; sys_platform == "win32"
原因:
- 同时评估Windows条件依赖性并与Linux目标wheel下载冲突
解决方案:
- 在WSL/Linux上创建wheelhouse而不是直接创建Windows
12.3在WSL python3.11: command not found
症状:
./scripts/build_wheelhouse.sh: line 15: python3.11: command not found
解决方案:
PYTHON_EXE=python bash ./scripts/build_wheelhouse.sh
包括Mansible-core:
PYTHON_EXE=python REQUIREMENTS=requirements-offline-with-ansible214.txt bash ./scripts/build_wheelhouse.sh
12.4脱机安装时 PyYAML>=6.0.2 未探测到
症状:
No matching distribution found for PyYAML>=6.0.2
原因:
- wheelhouse ABI不匹配(例如:
cp312在Python3.11服务器上安装wheel)
确认:
ls wheelhouse | grep -i pyyaml
- 在文件名中
cp311需要包括
解决方案:
cp311根据wheelhouse再生后重新复制
12.5服务器Python多版本问题
示例:
python3 --version->3.9.xpython --version->3.11.x
解决方案:
PYTHON_EXE=python REQUIREMENTS=requirements-offline-with-ansible214.txt bash ./scripts/install_offline.sh
14.最近执行情况摘要
- 将工具模块重构为
catalog_tools和run_tools为了可维护性。 - 采用两步目录流程:摘要(
list_registered_playbooks)然后是模式详细信息(get_playbook_schema). - 添加了库存主机发现工具:
list_inventory_hosts(inventory_id). - 添加运行时
extra_vars验证(必需/类型/枚举/未知键)。 - 使用结构化审计事件(JSON行)改进日志记录。
- 扩展的
.env-基于路径/配置注入和优先级项目根.env加载。 - 在运行响应中包含stdout:
stdout,stdout_line_count,stdout_truncated. - 添加
ANSIBLE_MCP_STDOUT_MAX_LINES以控制响应stdout音量。 - 添加了运行工件保留清理(基于计数)。
- 标准化服务脚本
start.sh,stop.sh,status.sh.
15.整理日志文件
日志文件路径创建主体内容
| --- | --- | --- |
| ${ANSIBLE_MCP_LOG_FILE} 或 ${ANSIBLE_MCP_LOG_DIR}/app.log | application/logging_config.py (mcp-ansible)|应用程序常规日志(错误/异常/生产日志)|
| ${ANSIBLE_MCP_AUDIT_LOG_FILE} 或 ${ANSIBLE_MCP_LOG_DIR}/audit.log | api/tools/context.py的 audit_log() | Tool请求/结果/错误审计事件(JSON line)|
| ${LOG_DIR}/server.out.log, ${LOG_DIR}/server.err.log (默认 /var/log/ansible-mcp) | scripts/start.sh (nohup 重定向)|流程标准输出/标准错误(Uvicorn动态消息,运行时stderr)|
执行交付项(artifact):
路径内容
| --- | --- |
| ${ANSIBLE_MCP_RUNS_DIR}/{run_id}/... (默认 /var/lib/ansible-mcp/runs) | stdout, job_events, command, status 等ansible-runner运行详细结果