Terraform 循环依赖挑战 [英] Terraform cyclic dependency challenge

查看:21
本文介绍了Terraform 循环依赖挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以大部分都在工作,除了......

Ok, so most of this is working except...

我们有一个用户数据模板文件,用于让每个新的 AWS 服务器注册 Chef Automate.Chef 通过用户数据脚本中设置的node_name"来引用每个客户端,默认情况下它是实例 id.但是在 Chef UI 或刀节点列表"中查看时,实例 ID 并不完全是用户友好的.我们能够使用模板写出一个有意义的 node_name.类似的东西:

We have a user data template file for getting each new AWS server to register with Chef Automate. Chef refers to each client by the "node_name" set in the user data script, which is the instance id by default. But when viewing in the Chef UI or "knife node list", the instance id isn't exactly user friendly. We were able to write out a meaningful node_name using the template. Something like:

data "template_file" "user-data-qa" {
  count = "${var.QA_numhosts}"
  template = "${file("userdata.tpl")}"
  vars {
    node_name = "${var.customer_name}-QA-${format("%d", count.index + 1)}"
  }
}

然而,如果我们重建实例,我们会收到来自 Chef 的错误消息,因为新实例尝试使用相同的名称注册,但使用新生成的密钥.

However, If we rebuild the instance, we get an error from Chef since the new instance tries to register with the same name, but a newly generated key.

因此,我们为 node_name 添加了一个随机数后缀.我们希望每次重建实例时更新这个随机数.第一次尝试是将实例 ID 设置为随机数的守护者".这导致了循环错误:( -> 表示取决于")Instance -> User Data -> Random -> Instance

So, we added a random number suffix to the node_name. We'd like this random number to be updated every time the instance is rebuilt. The first attempt was to set the instance id as the "keeper" for the random number. This resulted in the cycle error: ( -> means "depends on") Instance -> User Data -> Random -> Instance

还尝试转储随机生成并仅将实例 ID 的子字符串附加到 node_name.同样的问题,虽然周期较短:实例 -> 用户数据 -> 实例.

Also tried dumping random generation and just appending a substring of the instance id to the node_name. Same problem, although a shorter cycle: Instance -> User Data -> Instance.

有什么办法可以解决这个问题吗?简而言之,我们希望将一个字符串附加到 node_name 中,该字符串将被插入到用户数据中,并且该字符串应该在每次实例终止和重新启动时更新.没有来自 Terraform 的循环错误.

Any ideas for getting around this? In short, we want to append a string to the node_name, which gets inserted into the user data, and that string should update every time the instance is terminated and re-launched. Without cycle errors from Terraform.

推荐答案

instance-id ,如你所见,是计算出来的,所以不能在资源本身的定义中使用,或通过依赖循环.

instance-id, as you have seen, is calculated, so you can't use it in the definition of the resource itself, or through dependency cycles.

与其在 Terraform 中填充实例 ID,不如尝试让用户数据脚本本身获取实例 ID 并使用该值填充 node_name.rb.你可以用一个简单的 curl 和 echo:

Rather than populate the instance ID inside Terraform, try having the user data script itself get the instance ID and populate node_name.rb with that value. You can do this with a simple curl and echo:

echo "node_name '$(curl --silent/dev/null http://169.254.169.254/latest/meta-data/instance-id)'" >path/to/node_name.rb

这篇关于Terraform 循环依赖挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆