English 中文(简体)
1. 价值对Terraform物体的同等归属
原标题:Equal attribution of value to objects with Terraform

我愿就实现“Terraform”具体变量归属的方法提出技术建议。 不管背景如何,我有n的物体,我想给这些物体以[1-3]之间的价值,以便这些数值得到平均分配(无论这种数值是按顺序还是任意的,都不是实际的)。

这里更明确的是我认为有以下两种方式:

  1. randomly selects starting value (1 or 2 or 3)
  2. selects the next index (if 2 were to be selected as starting value, 3 would be selected next)
  3. if there are 4 or more objects on total the loop should start over and over as long as there are remaining objects

<>>>>

  1. randomly selects starting value (1 or 2 or 3)
  2. selects randomly any of the remaining value
  3. if there are 4 or more objects on total, randoms values would be selected so the global distribution would be equal

我可以轻松地与random Provide实现第一个步骤,但我没有就如何着手采取其他步骤。 我指的是,我知道如何增加价值,但是,在价值为3时,如何进行,我想开始 lo,回到价值1时? 或者如何任意选择其余数值?

最佳回答

sequential

您提到,方式确实很重要...... 我认为,按顺序排列,比较简单,使用范围,使用数字trick滴(<条码>(i % 3)+1,以便在你想要的[1-3]之间获得价值。

variable "objects" {
  default = 12
}

locals {
    distrib = [ for i in range(var.objects) : (i % 3) + 1 ]
}

output "test" {
    value = local.distrib
}

a terraform plan on that:

Changes to Outputs:
  + test = [
      + 1,
      + 2,
      + 3,
      + 1,
      + 2,
      + 3,
      + 1,
      + 2,
      + 3,
      + 1,
      + 2,
      + 3,
    ]

sequential with random start

在<代码>random_integer资源的帮助下,我们可以任意开端,并在范围功能中使用开端和限参数。

variable "objects" {
  default = 12
}

resource "random_integer" "extra" {
  min = 0
  max = 2
}

locals {
  start   = random_integer.extra.result
  distrib = [for i in range(local.start, var.objects + local.start) : (i % 3) + 1]
}

output "test" {
  value = local.distrib
}

random

Randomization is also possible using the random_shuffle resource:
https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/shuffle

variable "objects" {
  default = 12
}

locals {
    distrib = [ for i in range(var.objects) : (i % 3) + 1 ]
}

resource "random_shuffle" "random" {
  input        = local.distrib
  result_count = var.objects
}

output "test" {
    value = random_shuffle.random.result
}

a) 适用于:

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

Outputs:

test = tolist([
  "3",
  "1",
  "3",
  "2",
  "2",
  "3",
  "1",
  "1",
  "2",
  "2",
  "1",
  "3",
])

more complex


我们还可以使用其他价值而不是<代码>。 [1-3],见以下图形:

variable "objects" {
  default = 9
}

variable "items" {
  default = ["red", "blue", "cyan"]
}

locals {
  distrib = [for i in range(var.objects) : element(var.items, (i % length(var.items)))]
}

resource "random_shuffle" "random" {
  input        = local.distrib
  result_count = var.objects
}

output "test" {
  value = random_shuffle.random.result
}
问题回答

暂无回答




相关问题
Terra Custom Read

我在做的是按习俗分类的提供者,对标签有选择性的投入,但我发现的挑战是,扼杀使用ValueString()从主要价值中取回。 f,不确定......

Retrieve IDs in Terraform before script execution

I m a freshman and I don t know if I have a general thinking error but how do I get around the issue that when I want to roll out a script that there are some IDs missing that I would only get with ...

The plugin.(*GRPCProvider)

I am trying to deploy a Terraform stack from a Linux EC2 and getting the following error: The plugin.(*GRPCProvider).ApplyResourceChange request was cancelled. and │ The plugin.(*GRPCProvider)....

热门标签