首页 > 解决方案 > Terraform:flatcar OS 的容器 linux 配置的存储部分中的 YAML 文件渲染问题

问题描述

我正在尝试通过模板渲染生成文件以传递给 ec2 实例的用户数据。我正在使用第三方 terraform 提供程序从 YAML 生成点火文件。

data "ct_config" "worker" {
  content      = data.template_file.file.rendered
  strict       = true
  pretty_print = true
}
data "template_file" "file" {
  ...
  ...
  template = file("${path.module}/example.yml")
  vars = {
    script = file("${path.module}/script.sh")
  }
}

例子.yml

storage:
  files:
    - path: "/opt/bin/script"
      mode: 0755
      contents: 
        inline: |
          ${script}

错误:

Error: Error unmarshaling yaml: yaml: line 187: could not find expected ':'

  on ../../modules/launch_template/launch_template.tf line 22, in data "ct_config" "worker":
  22: data "ct_config" "worker" {

如果我更改${script}为示例数据,那么它可以工作。另外,无论我在 script.sh 中放什么,我都会遇到同样的错误。

标签: terraformcoreos-ignition

解决方案


我有这个确切的问题ct_config,今天解决了。您需要确保脚本在没有base64encode换行符的情况下正确编写- 否则,脚本中的换行符将进入 CT,CT 会尝试构建一个不能有换行符的 Ignition 文件,从而导致您最初遇到的错误。

编码后,您只需告诉 CT!!binary文件以确保 Ignition 在部署时正确地对其进行 base64 解码:

data "template_file" "file" {
  ...
  ...
  template = file("${path.module}/example.yml")
  vars = {
    script = base64encode(file("${path.module}/script.sh"))
  }
}
storage:
  files:
    - path: "/opt/bin/script"
      mode: 0755
      contents: 
        inline: !!binary |
          ${script}

推荐阅读