1
votes

terraform module:

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
}

main manifest:

 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 }

Needs to import resource state:

  1. Import device state -- ok

terraform import module.cmp1_test.netbox_device.device 2828 -- works correct

  1. import related interfaces:

terraform import module.cmp1_test.netbox_interface.device_interface["eth0"] 330033

Got an error

no matches found: module.cmp1_test.netbox_interface.device_interface[eth0]

Which is correct way to import all resources?


tfstate after creation looks like:

 {
      "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"
          ]
        },

1
Do you have to import? Why not destroy all the old resources and let Terraform create them again?Helder Sepulveda
Hi, in my case terraform used with DCIM system. Nowtime a lot of infrastructure were added to DCIM. Sometimes changes occure from web-interface by users. We need terraform to simplify procces such a add/replace/ form-factor changes. Also i cant rebuild device cause we use dynamic inventory based on DCIM.I.Kulakov
From that comment Sometimes changes occur from web-interface by users maybe a terraform resource is not the best for that, have you consider using a data source instead? _ _ _ terraform.io/docs/configuration/data-sources.htmlHelder Sepulveda

1 Answers

2
votes

Your shell is interpreting the quotes in the resource address and so removing them before Terraform can interpret them.

If you are on a Unix-like system (e.g. Linux, Mac OS X) you can use single quotes to pass the double quotes literally to Terraform:

terraform import 'module.cmp1_test.netbox_interface.device_interface["eth0"]' 330033

If you are using Windows, use the normal Windows Command Prompt (not PowerShell) and use backslash escaping to pass the double quotes literally to Terraform:

terraform import module.cmp1_test.netbox_interface.device_interface[\"eth0\"] 330033

If you are using PowerShell you must disable PowerShell's parser to ensure that it doesn't try to interpret the arguments as a PowerShell expression, using the "Stop Parsing" symbol --%, and then use the same escaping as for normal Windows Command Prompt above:

terraform --% import module.cmp1_test.netbox_interface.device_interface[\"eth0\"] 330033