如何在userdata中使用Terraform变量

huangapple go评论44阅读模式
英文:

How to use a terraform variable in userdata

问题

以下是翻译好的部分:

userdata.yaml

#cloud-config
users:
  - default
  - name: testuser
    sudo: ALL=(ALL) NOPASSWD:ALL
    ssh_authorized_keys:
      - $public_key
    shell: /bin/bash

packages:
  - jq

main.tf

...
resource "vsphere_virtual_machine" "master_nodes" {
  count = length(var.master_ips)
  ...
  extra_config = {
    "guestinfo.userdata" = base64encode(templatefile("./userdata.yaml", {
      public_key = master_public_keys[count.index]
    }))
    "guestinfo.userdata.encoding" = "base64"
  }
  ...
}

注意:这些代码片段中已经删除了 HTML 编码 ("),以便更容易阅读。

英文:

I am trying to pass a variable containing an ssh_public_key from my main.tf file to userdata, but it is not being added to the users authorized_keys file (The user is created however). If I hard code the key in the userdata.yaml file, it works fine, so I am wondering if I am passing variables correctly to the templatefile() function or perhaps I am using them incorrectly in the userdata file.

userdata.yaml

#cloud-config
users:
  - default
  - name: testuser
    sudo: ALL=(ALL) NOPASSWD:ALL
    ssh_authorized_keys:
      - $public_key
    shell: /bin/bash

packages:
  - jq

main.tf

...
resource "vsphere_virtual_machine" "master_nodes" {
  count = length(var.master_ips)
  ...
  extra_config = {
    "guestinfo.userdata" = base64encode(templatefile("./userdata.yaml", {
      public_key = master_public_keys[count.index]
    }))
    "guestinfo.userdata.encoding" = "base64"
  }
  ...
}

答案1

得分: 1

我相信你遗漏了:

${public_key}
英文:

I believe you're missing:

${public_key}

huangapple
  • 本文由 发表于 2023年7月12日 23:35:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定