You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Ansible Tower迁移至AWX:JSON数据导入困境及迁移方案咨询

迁移路径合理性判断

你的API导出路径完全可行——Ansible Tower和AWX的v2版本API在核心资源(清单、主机、项目、作业模板)上高度兼容,通过Python脚本调用API导出JSON的方式,能精准捕获你需要迁移的资源数据,是轻量化迁移指定资源的合理方案。


JSON数据导入AWX的实现方法

导入需遵循依赖顺序:项目 → 清单 → 主机 → 作业模板(后续资源依赖前置资源的存在),且要过滤AWX自动生成的字段(如id、created),处理关联资源的ID映射。以下是核心导入示例:

1. 导入清单

import requests
import json
from requests.auth import HTTPBasicAuth

# AWX连接配置
awx_config = {
    "url": "https://your-awx-instance-url",
    "username": "admin",
    "password": "your-awx-password"
}

# 读取导出的清单JSON
with open('/Users/username/AWX/exported_inventories.json', 'r', encoding='utf-8') as f:
    inventories = json.load(f)

# 清理AWX不需要的字段,映射组织ID(需确保目标AWX中存在对应组织)
for inv in inventories:
    # 移除自动生成/无关字段
    for key in ['id', 'url', 'created', 'modified', 'related', 'summary_fields', 'created_by', 'modified_by']:
        inv.pop(key, None)
    # 替换为目标AWX的组织ID(示例为1,根据实际调整)
    inv['organization'] = 1

# 批量导入
for inv in inventories:
    resp = requests.post(
        f"{awx_config['url']}/api/v2/inventories/",
        auth=HTTPBasicAuth(awx_config['username'], awx_config['password']),
        json=inv
    )
    if resp.status_code == 201:
        print(f"成功导入清单: {inv['name']}")
    else:
        print(f"导入失败 {inv['name']}: {resp.text}")

2. 导入主机(需先建立清单名称与AWX ID的映射)

# 构建AWX中清单名称到ID的映射
inventory_map = {}
resp = requests.get(f"{awx_config['url']}/api/v2/inventories/", auth=HTTPBasicAuth(awx_config['username'], awx_config['password'])).json()
for inv in resp['results']:
    inventory_map[inv['name']] = inv['id']
# 处理分页
next_page = resp['next']
while next_page:
    resp = requests.get(f"{awx_config['url']}{next_page}", auth=HTTPBasicAuth(awx_config['username'], awx_config['password'])).json()
    for inv in resp['results']:
        inventory_map[inv['name']] = inv['id']
    next_page = resp['next']

# 读取导出的主机JSON
with open('/Users/username/AWX/exported_hosts.json', 'r', encoding='utf-8') as f:
    hosts = json.load(f)

# 清理字段并关联AWX清单ID
for host in hosts:
    for key in ['id', 'url', 'created', 'modified', 'related', 'summary_fields', 'created_by', 'modified_by']:
        host.pop(key, None)
    # 替换为目标清单ID
    original_inv_name = host.pop('inventory_name', host['summary_fields']['inventory']['name'])
    host['inventory'] = inventory_map[original_inv_name]

# 批量导入主机
for host in hosts:
    resp = requests.post(
        f"{awx_config['url']}/api/v2/hosts/",
        auth=HTTPBasicAuth(awx_config['username'], awx_config['password']),
        json=host
    )
    if resp.status_code == 201:
        print(f"成功导入主机: {host['name']}")
    else:
        print(f"导入失败 {host['name']}: {resp.text}")

项目、作业模板的导入逻辑类似,需注意作业模板要关联AWX中已存在的项目ID、清单ID、凭据ID等资源。


其他迁移方法

1. 官方备份恢复法(完整迁移)

Tower和AWX基于相同的PostgreSQL数据库结构,版本差异不大时(建议同大版本,如Tower 3.8对应AWX 17+),可直接迁移数据库:

  • 停止Tower服务,执行pg_dump tower > tower_backup.sql导出数据库
  • 在AWX服务器创建同名数据库,执行psql awx < tower_backup.sql导入
  • 启动AWX服务,执行awx-manage migrate确保Schema兼容
  • 注意:此方法会迁移所有数据(用户、凭据、作业历史等),适合全量迁移

2. AWX CLI工具导入

使用官方awx CLI批量导入资源,无需编写脚本:

# 先配置CLI连接
awx config set host https://your-awx-url
awx config set username admin
awx config set password your-password

# 导入清单示例
awx inventory import --inventory-name "目标清单名称" --json exported_inventories.json

3. UI手动导入

进入AWX Web界面对应资源列表页(如「清单」),点击右上角「导入」按钮,选择导出的JSON文件,按提示完成导入,适合少量资源迁移。

内容的提问来源于stack exchange,提问作者DrunkenAvenger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:10:47