Terraform导入NetBox模块资源报错 咨询正确全资源导入方式
Let's break down the issue and fix the import process step by step:
The Root Cause of Your Import Error
The "no matches found" error happens because your shell (like bash) interprets square brackets [] as wildcard characters for filename matching, instead of passing them directly to Terraform. To fix this, you need to quote the resource address to protect it from shell parsing.
Step-by-Step Import Instructions
1. Import the NetBox Device (You Already Did This Successfully)
This one works as-is since there's no for_each involved:
terraform import module.cmp1_test.netbox_device.device 2828
2. Import NetBox Interfaces (for_each Resources)
For each interface instance created with for_each, wrap the entire resource address in single quotes to escape the square brackets. For example, to import the eth0 interface:
terraform import 'module.cmp1_test.netbox_interface.device_interface["eth0"]' 330033
If you have multiple interfaces to import, you can automate this with a simple shell script (adjust the interface IDs to match your NetBox data):
# Define a map of interface names to their NetBox IDs declare -A interface_map=( ["eth0"]="330033" ["eth1"]="330034" ["ipmi"]="330035" ) # Loop through each interface and import it for iface_name in "${!interface_map[@]}"; do terraform import "module.cmp1_test.netbox_interface.device_interface[\"$iface_name\"]" "${interface_map[$iface_name]}" done
3. Import NetBox Cables (Another for_each Resource)
Same rule applies here—quote the resource address to avoid shell parsing issues. Replace <cable_id> with the actual ID of each cable from your NetBox instance:
# Import cable for eth0 terraform import 'module.cmp1_test.netbox_cable.device_connections["eth0"]' 12345 # Repeat for eth1 and ipmi with their respective cable IDs
Verify Resource Addresses First
If you're unsure about the exact resource path, run this command to list all resources in the module:
terraform state list | grep module.cmp1_test
This will output the full, correct address for every resource instance, which you can copy-paste directly into your import commands (just remember to wrap it in quotes if it has square brackets).
内容的提问来源于stack exchange,提问作者I.Kulakov

