For_each keeps changing the order of a list that has not changed

We are creating registry entries using the rancher2_registry provider and are running into an issue where when we run terraform apply again, without any changes, it reorders the list of registries in the object. This appears to be completely random.
Here’s what the data looks like:

variable "containerRepositories" {
  type = list(object({
    url      = string
    username = string
    password = string
  }))
  default     = []
  description = "A list of Docker container repositories."
}

 containerRepositories = [
    {
      url      = "dcc-docker-release.<redacted>"
      username = "bld_dcc"
      password = "<redacted>"
    },
    {
      url      = "dcc-docker-dev.<redacted>"
      username = "bld_dcc"
      password = "<redacted>"
    },
    {
      url      = "dcsws-docker-release.<redacted>"
      username = "bld_dcc"
      password = "<redacted>"
    },
    {
      url      = "dcsws-docker-dev.<redacted>"
      username = "bld_dcc"
      password = "<redacted>"
    }
  ]

resource "rancher2_registry" "regcred-build" {
  count        = length(var.containerRepositories) > 0 ? 1 : 0
  name         = "regcred"
  description  = "Registry entry for k8s to talk to customers Artifactory registries"
  project_id   = rancher2_project.jenkins-project-build.id
  namespace_id = rancher2_namespace.jenkins-namespace-build.id
  dynamic "registries" {
    for_each = var.containerRepositories
    content {
      address  = registries.value["url"]
      username = registries.value["username"]
      password = registries.value["password"]
    }
  }
}

The result of running terraform apply when there have been no updates. If we apply this, then run it again, they will often “switch back”

  ~ resource "rancher2_registry" "regcred" {
        id           = "jenkins-dcs:regcred"
        name         = "regcred"
        # (5 unchanged attributes hidden)

      ~ registries {
          ~ address  = "dcsws-docker-release.<redacted>" -> "dcc-docker-release.<redacted>"
            # (2 unchanged attributes hidden)
        }
      ~ registries {
          ~ address  = "dcc-docker-release.<redacted>" -> "dcsws-docker-release.<redacted>"
            # (2 unchanged attributes hidden)
        }
        # (2 unchanged blocks hidden)
    }

We are running Terraform v1.1.9 and rancher/rancher2 v1.23.0

Is there a way to fix this or is it a bug in the provider?

Hi @dsargent3220,

Yes, the provider would be responsible for maintaining the correct order of the objects in the registries list. If you still have the problem with the latest version of the provider, I would suggest filing an issue in their GitHub repository. In the meantime, if the order is at least consistent from the provider, the easiest course of action may be to update the configuration to match.

It’s not consistent, they flip flop back and forth with each terraform apply
I’m on the latest version of the Rancher 2 provider, so that’s not it.
I’ll open a ticket. Thanks!