简体   繁体   English

带有 terraform 的 cloud-init 无法循环用户

[英]cloud-init with terraform cannot loop users

So I have the following Terraform code: `所以我有以下 Terraform 代码:`

I have the following templatefile:我有以下模板文件:

#cloud-config
users:
  %{ for user in user_list ~}
  - name: ${user.username}
    gecos: ${user.username}
    ssh-authorized-keys:
      - ${user.ssh_public_key}
    lock_passwd: true
    groups: sudo
    shell: /bin/bash
  %{ endfor } 
packages:
    jp

The following Terraform code:以下Terraform代码:

...
output "users" {
  value = [for k in local.external_users : k.username]
}

data "template_cloudinit_config" "config" {
  gzip          = true
  base64_encode = true
  part {
    filename     = "cloud-init.cfg"
    content_type = "text/cloud-config"
    content = templatefile("${path.module}/templates/userdataloop.yaml", {
      user_list = local.external_users
    })
  }
…
resource "azurerm_virtual_machine" "jphostvm" {
...
os_profile {
    computer_name  = "${var.environment}-jp-${format("%02d", count.index + 1)}"
    admin_username = "deploy"
    custom_data    = data.template_cloudinit_config.config.rendered
  }

and finally the following users (declared in a different tf file): `最后是以下用户(在不同的 tf 文件中声明):`

locals {
  external_users = [
    {
      username       = "ian"
      ssh_public_key = data.vault_generic_secret.ian.data["key"]
    },
    {
      username       = "fred"
      ssh_public_key = data.vault_generic_secret.fred.data["key"]
    },
    {
      username       = "ken"
      ssh_public_key = data.vault_generic_secret.ken.data["key"]
    }
  ]
}

I simply cannot get more than one user to be created.我根本无法创建多个用户。 Note the output loop displays all the usernames.请注意 output 循环显示所有用户名。 Passing single user as follows works perfectly:如下传递单个用户非常有效:

data "template_cloudinit_config" "config" {
  gzip          = true
  base64_encode = true
  part {
    filename     = "cloud-init.cfg"
    content_type = "text/cloud-config"
    content = templatefile("${path.module}/templates/userdataloop.yaml", {
      username       = "fred"
      ssh_public_key = data.vault_generic_secret.fred.data["key"]
    })
  }

and using the following template:并使用以下模板:

#cloud-config
users:
  - default
  - name: ${username}
    gecos: ${username}
    ssh-authorized-keys:
      - ${ssh_public_key}
    lock_passwd: true
    groups: sudo
    shell: /bin/bash
}

Can anyone help with this?有人能帮忙吗? I just can't figure out what is wrong and the code is failing silently so no error message to point me.我只是无法弄清楚出了什么问题,代码正在无声地失败,所以没有错误消息指向我。 It's a debian image the code uses for the vm.这是代码用于 vm 的 debian 图像。 Thanks.谢谢。

When configuring a system with cloud-init, any errors during processing will be written into the cloud-init logs inside your virtual machine's filesystem.使用 cloud-init 配置系统时,处理过程中的任何错误都将写入虚拟机文件系统内的 cloud-init 日志中。 Where exactly you'd find those logs will depend on how your AMI is configured, but the default location is /var/log/cloud-init.log .您可以在何处找到这些日志取决于您的 AMI 是如何配置的,但默认位置是/var/log/cloud-init.log


You've shown two different templates in your question, one of which contains a for directive while the other contains only interpolation.您在问题中展示了两个不同的模板,其中一个包含for指令,而另一个仅包含插值。

Both of these are not following the advice in Generating JSON or YAML from a template , so it's possible that your attempt to generate YAML by string concatenation is producing something that isn't valid YAML syntax or doesn't have the meaning you intended.这两个都没有遵循从模板生成 JSON 或 YAML中的建议,因此您尝试通过字符串连接生成 YAML 可能会产生一些无效的 YAML 语法或没有您想要的含义。

The following template uses yamlencode instead, to guarantee that the result will always be valid YAML syntax:以下模板改用yamlencode ,以保证结果始终是有效的 YAML 语法:

#cloud-config
${yamlencode({
  users = [
    for user in user_list : {
      name                = user.username
      gecos               = user.username
      ssh_authorized_keys = [user.ssh_public_key]
      lock_passwd         = true
      groups              = "sudo"
      shell               = "/bin/bash"
    }
  ]
  packages = [
    "jp",
  ]
})}

I also changed your ssh-authorized-keys key to be ssh_authorized_keys instead, because that seems to be the name used in the relevant cloud-config example .我还将您的ssh-authorized-keys密钥更改为ssh_authorized_keys ,因为这似乎是相关云配置示例中使用的名称。

If you use a template like the above so that the result is guaranteed to be valid YAML syntax and it still doesn't work then that suggests that there's something wrong with the data represented by the YAML rather than the syntax of the YAML. In that case you'll need to refer to the cloud-init log in your virtual machine to see what happened when it tried to interpret your cloud-config data structure.如果你使用像上面这样的模板,保证结果是有效的 YAML 语法,但它仍然不起作用,那么这表明 YAML 表示的数据有问题,而不是 YAML 的语法。在那在这种情况下,您需要参考虚拟机中的 cloud-init 日志,以查看当它试图解释您的 cloud-config 数据结构时发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Terraform 和 cloud-init 安全地允许访问 AWS Secrets Manager - How to securely allow access to AWS Secrets Manager with Terraform and cloud-init 来自 Cloud-Init 执行的日志不存在 - Logs from Cloud-Init Execution are not present cloud-init 文件跳过 Github 注册 - Cloud-init File skip Github registration 如何手动运行 cloud-init? - How to run cloud-init manually? 使用 cloud-init 和实例标签在 EC2 上设置主机名? - Set hostname on EC2 using cloud-init and instance tags? EC2 cloud-init 脚本未在用户数据更新时更新 - EC2 cloud-init script not updated on userdata update Cloud-init 文件命令行选项“S”[from -fsSL] 无法与其他选项结合使用 - Cloud-init File Command line option 'S' [from -fsSL] is not understood in combination with the other options 为什么 Azure VM 不运行 userData 参数中指定的 cloud-init 脚本? - Why does Azure VM not run cloud-init script specified in userData parameter? 无法通过 Terraform 使用 GCP Cloud Build 对 GitHub 存储库进行身份验证 - Cannot authenticate GitHub repository with GCP Cloud Build via Terraform Pycharm 在 google.cloud 的“__init__.pyi”中找不到引用“cloud”,但代码确实执行 - Pycharm cannot find reference `cloud` in "__init__.pyi" for google.cloud, but code does execute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM