英文:
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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论