Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ressource "nxos_ipv4_static_route" import forces replacement #305

Open
Pidpad opened this issue Feb 24, 2025 · 1 comment
Open

Ressource "nxos_ipv4_static_route" import forces replacement #305

Pidpad opened this issue Feb 24, 2025 · 1 comment

Comments

@Pidpad
Copy link

Pidpad commented Feb 24, 2025

Hey guys, I try to manage my nxos (non fabric/aci) switches via terraform and ran into the following issue.
When I try to import my existing static ipv4 routes, terraform wants to replace these routes instead of importing them.

Is there any way to import my existing routes without terraform recreating those?

switch model: N9K-C93360YC-FX2
nxos version: 10.2(5)

my research so far

visore.html page on the switch, to view the dn information on the switch.
I guess the switch is not providing more information on the api call? Maybe a switch update could fix this?

Image

Terraform plan output:

  # module.vlan["VLAN"].nxos_ipv4_static_route.static_route["switchname"] must be replaced
  # (imported from "sys/ipv4/inst/dom-/rt-[cidr/24]")
  # Warning: this will destroy the imported resource
-/+ resource "nxos_ipv4_static_route" "static_route" {
      + device    = "switchname"
      ~ id        = "sys/ipv4/inst/dom-[default]/rt-[cidr/24]" -> (known after apply)
      + next_hops = [
          + {
              + address      = "nexthop" # forces replacement
              + description  = "FW-INT.terraform.mananged"
              + interface_id = "unspecified" # forces replacement
              + object       = 10
              + preference   = 123
              + tag          = 10
              + vrf_name     = "default" # forces replacement
            },
        ]
      ~ prefix    = "" -> "cidr/24" # forces replacement
      + vrf_name  = "default" # forces replacement
    }

My terraform code looks like this

resource "nxos_ipv4_static_route" "static_route" {
  for_each   = toset([for switch in var.switches : switch.name if (switch.static_routes && var.layer3_vlan)])
  device       = each.key
  vrf_name = "default"
  prefix   = var.subnet
  next_hops = [
    {
    interface_id = "unspecified"
    address      = "nexthop"
    vrf_name     = "default"
    description  = "FW-INT.${var.vlan_name}"
    object       = 10
    preference   = 123
    tag          = 10
  }
  ]
}

The route in the show run on the switch looks like this:

ip route cidr/24 nexthop name terraform.mananged

---

IP Route Table for VRF "default"
'*' denotes best ucast next-hop
'**' denotes best mcast next-hop
'[x/y]' denotes [preference/metric]
'%<string>' in via output denotes VRF <string>

cidr/24, ubest/mbest: 1/0
    *via nexthop, [1/0], 3w5d, static
@Pidpad
Copy link
Author

Pidpad commented Feb 24, 2025

As I use a custom variable for my switches and use a for_each I thought I should try a more simple test against the DevNet AlwaysOn Sandbox (Nexus C9300v version 10.3(3)) and got this results

terraform {
  required_version = ">= 1.1.0"

  required_providers {
    nxos = {
      source  = "CiscoDevNet/nxos"
      version = "0.5.7"
    }
  }
}

provider "nxos" {
  alias    = "nxos"
  username = "Username"
  password = "Password"
  url      = "https://sbx-nxos-mgmt.cisco.com"
}

#STATIC ROUTE TEST
import {
    to = nxos_ipv4_static_route.static_route
    id = "sys/ipv4/inst/dom-[management]/rt-[0.0.0.0/0]"
}

#STATIC ROUTE TEST
resource "nxos_ipv4_static_route" "static_route" {
  provider             = nxos.nxos
  vrf_name = "management"
  prefix   = "0.0.0.0/0"
  next_hops = [
    {
    interface_id = "unspecified"
    address      = "10.10.20.254/32"
    vrf_name     = "management"
    object       = 0
    preference   = 0
    tag          = 0
  }
  ]
}

TF still wants to replace the route

❯ tf plan
nxos_ipv4_static_route.static_route: Preparing import... [id=sys/ipv4/inst/dom-[management]/rt-[0.0.0.0/0]]
nxos_ipv4_static_route.static_route: Refreshing state... [id=sys/ipv4/inst/dom-[management]/rt-[0.0.0.0/0]]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # nxos_ipv4_static_route.static_route must be replaced
  # (imported from "sys/ipv4/inst/dom-[management]/rt-[0.0.0.0/0]")
  # Warning: this will destroy the imported resource
-/+ resource "nxos_ipv4_static_route" "static_route" {
      ~ id        = "sys/ipv4/inst/dom-[management]/rt-[0.0.0.0/0]" -> (known after apply)
        next_hops = [
            {
                address      = "10.10.20.254/32"
                description  = null
                interface_id = "unspecified"
                object       = 0
                preference   = 0
                tag          = 0
                vrf_name     = "management"
            },
        ]
        prefix    = "0.0.0.0/0"
      + vrf_name  = "management" # forces replacement
    }

Plan: 1 to import, 1 to add, 0 to change, 1 to destroy.

show run on the devnet sandbox

nxos# show ip route 0.0.0.0/0 vrf management
IP Route Table for VRF "management"
'*' denotes best ucast next-hop
'**' denotes best mcast next-hop
'[x/y]' denotes [preference/metric]
'%<string>' in via output denotes VRF <string>

0.0.0.0/0, ubest/mbest: 1/0
    *via 10.10.20.254, [1/0], 5d17h, static

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant