gpt4 book ai didi

terraform - 有没有办法为 Terraform 存档提供程序定义多个 source_file?

转载 作者:行者123 更新时间:2023-12-03 21:18:52 26 4
gpt4 key购买 nike

我正在使用 Terraform archive_file provider将多个文件打包成一个 zip 文件。当我像这样定义存档时,它工作正常:

data "archive_file" "archive" {
type = "zip"
output_path = "./${var.name}.zip"
source_dir = "${var.source_dir}"
}

但是我不希望存档包含 var.source_dir 中的所有文件,我只想要其中的一个子集。我注意到 archive_file 提供程序有一个 source_file属性,所以我希望我可以提供这些文件的列表并将它们打包到存档中,如下所示:
locals {
source_files = ["${var.source_dir}/foo.txt", "${var.source_dir}/bar.txt"]
}

data "archive_file" "archive" {
type = "zip"
output_path = "./${var.name}.zip"
count = "2"
source_file = "${local.source_files[count.index]}"
}

但这不起作用,文件是为 local.source-files 中定义的每个文件构建的。因此,我有一个“最后一个获胜”的场景,其中构建的存档文件仅包含 bar.txt。

我试过这个:
locals {
source_files = ["${var.source_dir}/main.py", "${var.source_dir}/requirements.txt"]
}

data "archive_file" "archive" {
type = "zip"
output_path = "./${var.name}.zip"
source_file = "${local.source_files}"
}

但不出所料,失败了:

data.archive_file.archive: source_file must be a single value, not a list



有没有办法实现我在这里之后的目标,即将文件列表传递给 archive_file 提供程序并将它们全部打包到存档文件中?

最佳答案

----谢谢jamiet,我修改为你的评论----

  • 将文件复制到临时目录并存档
  • locals {
    source_files = ["${var.source_dir}/main.py", "${var.source_dir}/requirements.txt"]
    }

    data "template_file" "t_file" {
    count = "${length(local.source_files)}"

    template = "${file(element(local.source_files, count.index))}"
    }

    resource "local_file" "to_temp_dir" {
    count = "${length(local.source_files)}"
    filename = "${path.module}/temp/${basename(element(local.source_files, count.index))}"
    content = "${element(data.template_file.t_file.*.rendered, count.index)}"
    }

    data "archive_file" "archive" {
    type = "zip"
    output_path = "${path.module}/${var.name}.zip"
    source_dir = "${path.module}/temp"

    depends_on = [
    "local_file.to_temp_dir",
    ]
    }
  • 使用 archive_file 的来源
  • locals {
    source_files = ["${var.source_dir}/main.py", "${var.source_dir}/requirements.txt"]
    }

    data "template_file" "t_file" {
    count = "${length(local.source_files)}"

    template = "${file(element(local.source_files, count.index))}"
    }


    data "archive_file" "archive" {
    type = "zip"
    output_path = "./${var.name}.zip"

    source {
    filename = "${basename(local.source_files[0])}"
    content = "${data.template_file.t_file.0.rendered}"
    }

    source {
    filename = "${basename(local.source_files[1])}"
    content = "${data.template_file.t_file.1.rendered}"
    }
    }
  • 创建 shell 脚本并使用外部数据资源调用它。
  • locals {
    source_files = ["${var.source_dir}/main.py", "${var.source_dir}/requirements.txt"]
    }

    data "template_file" "zip_sh" {
    template = <<EOF
    #!/bin/bash
    zip $* %1>/dev/null %2>/dev/null
    echo '{"result":"success"}'
    EOF
    }

    resource "local_file" "zip_sh" {
    filename = "${path.module}/zip.sh"
    content = "${data.template_file.zip_sh.rendered}"
    }

    data "external" "zip_sh" {
    program = ["${local_file.zip_sh.filename}", "${var.name}", "${join(" ", local.source_files)}"]

    depends_on = [
    "data.template_file.zip_sh",
    ]
    }

    关于terraform - 有没有办法为 Terraform 存档提供程序定义多个 source_file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56916719/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com