使用terraform数量缩放实例,而不替换旧实例

问题描述

这是我今天的问题。 我有一个terraform部署的ovh基础架构。 我使用了一个计数来部署三个相同的实例,但是当我要扩展另一个实例时,terraform希望替换这三个实例,因为某些配置已更改。

这里是地形计划的输出

    An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
-/+ destroy and then create replacement

terraform will perform the following actions:

  # openstack_compute_instance_v2.swarmnode[0] must be replaced
-/+ resource "openstack_compute_instance_v2" "swarmnode" {
      ~ access_ip_v4        = "***" -> (kNown after apply)
      ~ access_ip_v6        = "[***]" -> (kNown after apply)
      ~ all_Metadata        = {} -> (kNown after apply)
      ~ all_tags            = [] -> (kNown after apply)
      ~ availability_zone   = "nova" -> (kNown after apply)
      ~ flavor_id           = "***" -> (kNown after apply)
        flavor_name         = "b2-15"
        force_delete        = false
      ~ id                  = "***" -> (kNown after apply)
      ~ image_id            = ""***" -> (kNown after apply)
      ~ image_name          = "Image not found" -> "Ubuntu 18.04" # forces replacement
        key_pair            = "key_pair"
        name                = "int-swarmnode-001"
        power_state         = "active"
      ~ region              = "GRA7" -> (kNown after apply)
        security_groups     = [
            "int-backend",]
        stop_before_destroy = false
      - tags                = [] -> null
      ~ user_data           = "7a3c7f112e97010e2e5dc37f3b86ce15920df5e0" -> "9b9be33f6e07597350ed760b4e19b2bc4fd59da3" # forces replacement

      ~ network {
            access_network = false
          ~ fixed_ip_v4    = "***" -> (kNown after apply)
          ~ fixed_ip_v6    = "***" -> (kNown after apply)
          + floating_ip    = (kNown after apply)
          ~ mac            = "***" -> (kNown after apply)
            name           = "Ext-Net"
          + port           = (kNown after apply)
          ~ uuid           = "***" -> (kNown after apply)
        }
      ~ network {
            access_network = true
          ~ fixed_ip_v4    = "***" -> (kNown after apply)
          + fixed_ip_v6    = (kNown after apply)
          + floating_ip    = (kNown after apply)
          ~ mac            = ""***" -> (kNown after apply)
            name           = "privnet_saas_int"
          + port           = (kNown after apply)
          ~ uuid           = "***" -> (kNown after apply)
        }
    }


  # openstack_compute_instance_v2.swarmnode[3] will be created
  + resource "openstack_compute_instance_v2" "swarmnode" {
      + access_ip_v4        = (kNown after apply)
      + access_ip_v6        = (kNown after apply)
      + all_Metadata        = (kNown after apply)
      + all_tags            = (kNown after apply)
      + availability_zone   = (kNown after apply)
      + flavor_id           = (kNown after apply)
      + flavor_name         = "b2-15"
      + force_delete        = false
      + id                  = (kNown after apply)
      + image_id            = (kNown after apply)
      + image_name          = "Ubuntu 18.04"
      + key_pair            = "key_pair"
      + name                = "int-swarmnode-004"
      + power_state         = "active"
      + region              = (kNown after apply)
      + security_groups     = [
          + "int-backend",]
      + stop_before_destroy = false
      + user_data           = "9b9be33f6e07597350ed760b4e19b2bc4fd59da3"

      + network {
          + access_network = false
          + fixed_ip_v4    = (kNown after apply)
          + fixed_ip_v6    = (kNown after apply)
          + floating_ip    = (kNown after apply)
          + mac            = (kNown after apply)
          + name           = "Ext-Net"
          + port           = (kNown after apply)
          + uuid           = (kNown after apply)
        }
      + network {
          + access_network = true
          + fixed_ip_v4    = (kNown after apply)
          + fixed_ip_v6    = (kNown after apply)
          + floating_ip    = (kNown after apply)
          + mac            = (kNown after apply)
          + name           = "privnet_saas_int"
          + port           = (kNown after apply)
          + uuid           = (kNown after apply)
        }
    }

  # openstack_compute_keypair_v2.ssh_keypair_*** must be replaced
-/+ resource "openstack_compute_keypair_v2" "ssh_keypair_***" {
      ~ fingerprint = "***" -> (kNown after apply)
      ~ id          = "***" -> (kNown after apply)
        name        = "***"
      + private_key = (kNown after apply)
      ~ public_key  = <<~EOT # forces replacement
            ***
        EOT
        region      = "GRA7"
    }

Plan: 5 to add,0 to change,4 to destroy.

Warning: Resource targeting is in effect

You are creating a plan with the -target option,which means that the result
of this plan may not represent all of the changes requested by the current
configuration.
    
The -target option is not for routine use,and is provided only for
exceptional situations such as recovering from errors or mistakes,or when
terraform specifically suggests to use it as part of an error message.


------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan,so terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

我不知道为什么未设置图像,因为设置了我的地形状态(远程到aws bucket):

{
      "mode": "managed","type": "openstack_compute_instance_v2","name": "swarmnode","each": "list","provider": "provider.openstack.ovh","instances": [
        {
          "index_key": 0,"schema_version": 0,"attributes": {
            "access_ip_v4": "***","access_ip_v6": "***","admin_pass": null,"all_Metadata": {},"all_tags": [],"availability_zone": "nova","block_device": [],"config_drive": null,"flavor_id": "***","flavor_name": "b2-15","floating_ip": null,"force_delete": false,"id": "****","image_id": "****","image_name": "Ubuntu 18.04","key_pair": "****","Metadata": null,"name": "int-swarmnode-001","network": [
              {
                "access_network": false,"fixed_ip_v4": "****","fixed_ip_v6": "***","floating_ip": "","mac": "***","name": "Ext-Net","port": "","uuid": "***"
              },{
                "access_network": true,"fixed_ip_v4": "***","fixed_ip_v6": "","name": "privnet_saas_int","uuid": "***"
              }

user_data也已更改,但不知道为什么。

我尝试进行terraform导入以获取最后的值,但什么都没有改变。

如何在不破坏实例的情况下解决冲突?

这里是我的terraform代码,以count部署swarmnode实例:

resource "openstack_compute_instance_v2" "swarmnode" {
  provider = openstack.ovh
  count = var.ovh_nb_swarmnodes
  name = "${var.ovh_env}-swarmnode-${format("%03d",count.index + 1)}"
  image_name = "Ubuntu 18.04"
  # flavor_name = "s1-2" # 1 vCores 2.4GHz / 2Go RAM / 10Go SSD
  flavor_name = var.ovh_swarm_instance_type
  key_pair = openstack_compute_keypair_v2.ssh_keypair.name
  user_data = templatefile("cloud-init.swarmnode.conf",{ssh_public_key = file("vars/${var.ovh_env}/${var.ovh_env}.pub")})

  security_groups = [
    "${openstack_compute_secgroup_v2.sg-backend.name}"
  ]

  network {
    name = "Ext-Net"
  }
  network {
    access_network = true
    name = ovh_cloud_network_private.network.name
  }
}

感谢您的帮助

最诚挚的问候,

BDT

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)