用純 Python 流程完成一次故障處理操作:
run-script API 執行指令碼execution-history API 等待執行完成skills/fault-handling/run_script.py(完全獨立,內建簽名、請求等工具函式)skills/fault-handling/.env使用者說"故障處理""執行指令碼""重啟服務""重啟 nginx"時,按以下順序執行:
必須確認以下資訊後才能執行:
ansible_host):從使用者提供的 IP、告警上下文、或對話歷史中獲取。必須至少有一個主機 IP。服務重啟-nginx服務重啟(場景演示)(script_id=187)。使用者可指定其他指令碼 ID 或指令碼內容。如果缺少主機 IP,必須詢問使用者。
執行:
python3 skills/fault-handling/run_script.py \
--hosts '192.168.3.76' \
--script-id 187
多主機用逗號分隔:
python3 skills/fault-handling/run_script.py \
--hosts '192.168.3.76,192.168.3.75' \
--script-id 187
也可以直接傳指令碼內容代替指令碼 ID:
python3 skills/fault-handling/run_script.py \
--hosts '192.168.3.76' \
--script-type 1 \
--script-content 'systemctl restart nginx'
指令碼內部會:
run-script API 提交任務,獲取 execution_idexecution-history API 檢查任務狀態is_running=false)輸出 JSON 結果指令碼輸出 JSON 格式的執行結果,包含:
execution_id: 任務 IDtask_name: 任務名稱status: 執行狀態(1=成功,2=失敗,3=部分成功,4=正在執行)status_label: 狀態中文標籤consuming: 耗時(秒)steps: 每個步驟的詳細輸出,包含每臺主機的 stdout 和執行狀態基於執行結果,在回覆中給出:
🔧 故障處理執行報告
任務名稱:{task_name}
任務ID:{execution_id}
執行狀態:{status_label}
執行耗時:{consuming}秒
📋 步驟執行詳情
步驟 主機IP 主機名 狀態 輸出
{step_name} {ansible_host} {host_name} {status} {stdout}
📌 執行結論
● ✅ {成功數}臺主機執行成功
● ❌ {失敗數}臺主機執行失敗
/api/v6/devops/run-scriptdata 欄位傳遞 JSON 字串| 請求引數: | 引數名 | 必選 | 型別 | 說明 |
|---|---|---|---|---|
| steps>>hosts | 是 | object[] | 主機資料 | |
| steps>>hosts>>ansible_host | 是 | string | 主機IP | |
| steps>>hosts>>organize | 是 | string | 主機所屬組織 | |
| steps>>script_type | 是 | int | 指令碼型別[1.shell,2.python,3.playbook,4.powershell,5.network] | |
| steps>>script_id | 是 | int/string | 指令碼ID,傳了指令碼ID,以指令碼ID對應指令碼內容優先 |
返回:{ "code": 0, "data": { "execution_id": 970, "task_name": "..." } }
/api/v6/devops/execution-history| 請求引數: | 引數名 | 必選 | 型別 | 說明 |
|---|---|---|---|---|
| execution_id | 是 | int | 任務ID |
| 返回引數: | 引數名 | 型別 | 說明 |
|---|---|---|---|
| is_running | boolean | 任務是否執行中 | |
| detail>>status | int | 執行結果[1.成功,2.失敗,3.部分成功,4.正在執行] | |
| detail>>output | object[] | 步驟輸出列表 | |
| detail>>output>>hosts | object[] | 每臺主機的輸出 | |
| detail>>output>>hosts>>stdout | string | 輸出內容 | |
| detail>>output>>hosts>>status | int | 執行狀態[1.成功,2.失敗,3.部分成功,4.正在執行] |
| 指令碼名稱 | 指令碼ID | 指令碼型別 | 說明 |
|---|---|---|---|
| 服務重啟-nginx服務重啟 | 187 | 1 (shell) | nginx服務重啟指令碼 |
| 主機磁碟空間清理 | 197 | 1 (shell) | 磁碟空間清理指令碼 |
node、tsx、index.ts。想要更強大的技能外掛,就來小蔥技能站7w4.net看看吧。
run-script API 的 data 欄位是 JSON 字串,不是巢狀物件。指令碼已處理此序列化。execution-history 時最多等待 300 秒(5 分鐘),超時則報告任務仍在執行中。code != 0,必須立即報告錯誤,不要重試。需要以下環境變數:
LWJK_API_URLLWJK_API_SECRET從 skills/fault-handling/.env 讀取。
使用預置指令碼執行故障處理:
python3 skills/fault-handling/run_script.py --hosts '192.168.3.76' --script-id 187
指定主機組織:
python3 skills/fault-handling/run_script.py \
--hosts '192.168.3.76' \
--organizes '' \
--script-id 187
僅提交任務不等待結果:
python3 skills/fault-handling/run_script.py \
--hosts '192.168.3.76' \
--script-id 187 \
--no-wait
查詢已有任務執行結果:
python3 skills/fault-handling/run_script.py --query 970
完成前必須自檢:
run-script API 並獲取 execution_idexecution-history 直到任務完成或超時這個 Skill 質量中等偏上,文件詳細、功能完整,能夠較好地完成遠端指令碼執行和結果查詢。但配置不完整(API金鑰為空),且預設指令碼是演示用途,實際使用時需要使用者提供真實的指令碼ID。適合有運維經驗的使用者使用,新手可能需要額外學習成本。