Terraform 导入模块状态
Terraform import module state
terraform 模块:
resource "netbox_device" "device" {
name = var.name
site = var.site
tenant = var.tenant
rack = var.rack
position = var.position
type = var.type
role = var.role
status = var.status
}
resource "netbox_interface" "device_interface" {
for_each = var.connections
name = each.key
device = netbox_device.device.id
}
resource "netbox_cable" "device_connections" {
for_each = var.connections
device_a_name = netbox_device.device.name
interface_a_name = each.key
device_b_name = each.value.device
interface_b_name = each.value.interface
}
主要清单:
19 module "cmp1_test" {
20 source = "../../modules/device"
21 name = "cmp1"
22 site = "example.site"
23 rack = "111"
24 position = 15
25 type = "Generic-1U"
26 role = "cmp"
27 connections = {
28 "eth0" = { device = "san-s1-1", interface = "Ethernet 5" },
29 "eth1" = { device = "san-s1-2", interface = "Ethernet 6" },
30 "ipmi" = { device = "san-s1-1", interface = "Ethernet 7" }
31 }
32 }
需要导入资源状态:
- 导入设备状态 -- ok
terraform import module.cmp1_test.netbox_device.device 2828 -- works correct
- 导入相关接口:
terraform import module.cmp1_test.netbox_interface.device_interface["eth0"] 330033
遇到错误
no matches found:
module.cmp1_test.netbox_interface.device_interface[eth0]
导入所有资源的正确方法是什么?
创建后的 tfstate 如下所示:
{
"module": "module.cmp1_test",
"mode": "managed",
"type": "netbox_device",
"name": "device",
"provider": "provider.netbox",
"instances": [
{
"schema_version": 0,
"attributes": {
"face": 0,
"id": "2828",
"name": "cmp1",
"position": 15,
"rack": "111",
"role": "cmp",
"serial": "",
"site": "example.site",
"status": "Planned",
"tenant": "VPC",
"type": "Generic-1U"
},
"private": "bnVsbA=="
}
]
},
{
"module": "module.cmp1_test",
"mode": "managed",
"type": "netbox_interface",
"name": "device_interface",
"each": "map",
"provider": "provider.netbox",
"instances": [
{
"index_key": "eth0",
"schema_version": 0,
"attributes": {
"description": null,
"device": 2828,
"enabled": null,
"form_factor": null,
"id": "31303",
"lag": null,
"mgmt_only": null,
"mode": null,
"mtu": null,
"name": "eth0",
"type": 1200
},
"private": "bnVsbA==",
"dependencies": [
"module.cmp1_test.netbox_device.device"
]
},
您的 shell 正在解释资源地址中的引号,因此在 Terraform 可以解释它们之前将其删除。
如果您使用的是类 Unix 系统(例如 Linux、Mac OS X),您可以使用单引号将双引号逐字传递给 Terraform:
terraform import 'module.cmp1_test.netbox_interface.device_interface["eth0"]' 330033
如果您使用的是 Windows,请使用普通的 Windows 命令提示符( 而非 PowerShell)并使用反斜杠转义将双引号字面意思传递给地形:
terraform import module.cmp1_test.netbox_interface.device_interface[\"eth0\"] 330033
如果您使用的是 PowerShell,则必须禁用 PowerShell's parser 以确保它不会尝试使用“停止解析”符号 --%
将参数解释为 PowerShell 表达式,然后使用与上面正常 Windows 命令提示符相同的转义:
terraform --% import module.cmp1_test.netbox_interface.device_interface[\"eth0\"] 330033
terraform 模块:
resource "netbox_device" "device" {
name = var.name
site = var.site
tenant = var.tenant
rack = var.rack
position = var.position
type = var.type
role = var.role
status = var.status
}
resource "netbox_interface" "device_interface" {
for_each = var.connections
name = each.key
device = netbox_device.device.id
}
resource "netbox_cable" "device_connections" {
for_each = var.connections
device_a_name = netbox_device.device.name
interface_a_name = each.key
device_b_name = each.value.device
interface_b_name = each.value.interface
}
主要清单:
19 module "cmp1_test" {
20 source = "../../modules/device"
21 name = "cmp1"
22 site = "example.site"
23 rack = "111"
24 position = 15
25 type = "Generic-1U"
26 role = "cmp"
27 connections = {
28 "eth0" = { device = "san-s1-1", interface = "Ethernet 5" },
29 "eth1" = { device = "san-s1-2", interface = "Ethernet 6" },
30 "ipmi" = { device = "san-s1-1", interface = "Ethernet 7" }
31 }
32 }
需要导入资源状态:
- 导入设备状态 -- ok
terraform import module.cmp1_test.netbox_device.device 2828 -- works correct
- 导入相关接口:
terraform import module.cmp1_test.netbox_interface.device_interface["eth0"] 330033
遇到错误
no matches found: module.cmp1_test.netbox_interface.device_interface[eth0]
导入所有资源的正确方法是什么?
创建后的 tfstate 如下所示:
{
"module": "module.cmp1_test",
"mode": "managed",
"type": "netbox_device",
"name": "device",
"provider": "provider.netbox",
"instances": [
{
"schema_version": 0,
"attributes": {
"face": 0,
"id": "2828",
"name": "cmp1",
"position": 15,
"rack": "111",
"role": "cmp",
"serial": "",
"site": "example.site",
"status": "Planned",
"tenant": "VPC",
"type": "Generic-1U"
},
"private": "bnVsbA=="
}
]
},
{
"module": "module.cmp1_test",
"mode": "managed",
"type": "netbox_interface",
"name": "device_interface",
"each": "map",
"provider": "provider.netbox",
"instances": [
{
"index_key": "eth0",
"schema_version": 0,
"attributes": {
"description": null,
"device": 2828,
"enabled": null,
"form_factor": null,
"id": "31303",
"lag": null,
"mgmt_only": null,
"mode": null,
"mtu": null,
"name": "eth0",
"type": 1200
},
"private": "bnVsbA==",
"dependencies": [
"module.cmp1_test.netbox_device.device"
]
},
您的 shell 正在解释资源地址中的引号,因此在 Terraform 可以解释它们之前将其删除。
如果您使用的是类 Unix 系统(例如 Linux、Mac OS X),您可以使用单引号将双引号逐字传递给 Terraform:
terraform import 'module.cmp1_test.netbox_interface.device_interface["eth0"]' 330033
如果您使用的是 Windows,请使用普通的 Windows 命令提示符( 而非 PowerShell)并使用反斜杠转义将双引号字面意思传递给地形:
terraform import module.cmp1_test.netbox_interface.device_interface[\"eth0\"] 330033
如果您使用的是 PowerShell,则必须禁用 PowerShell's parser 以确保它不会尝试使用“停止解析”符号 --%
将参数解释为 PowerShell 表达式,然后使用与上面正常 Windows 命令提示符相同的转义:
terraform --% import module.cmp1_test.netbox_interface.device_interface[\"eth0\"] 330033