【问题标题】:Calling a shell script within local-exec-terraform在 local-exec-terraform 中调用 shell 脚本
【发布时间】:2020-07-09 19:26:49
【问题描述】:

我正在尝试从 Terraform 的本地 exec 块中调用 shell 脚本。 shell 脚本基本上使用 terraform 输出(大约 8 个输出)并生成一个 YML 文件(作为变量),我稍后使用 cat > 方法使用该文件。在脚本中,我还使用例如 Ssh 私钥进行了一些格式化。这真的行不通。 最好的方法是什么?我可以在 local-exec 中使用任何 Linux 命令吗?有没有更好的方法来利用 terraform 输出? 我主要想使用来自不同模块的某些输出并创建一个 YML 文件(如键值对)。

【问题讨论】:

  • 您能否编辑您的问题以显示您尝试过的内容,然后清楚地解释哪些内容不适合您?理想情况下,这应该是minimal reproducible example 的形式。如果出错,则还包括完整的错误输出。

标签: terraform terraform-provider-aws


【解决方案1】:

为什么不使用 template_file 代替:

data "template_file" "kube_config" {
  template = "${file("${path.module}/kubeconfig.tpl")}"

  vars {
    vpc_name     = "${var.vpc_name}"
    eks_name     = "${aws_eks_cluster.eks_cluster.id}"
    eks_endpoint = "${aws_eks_cluster.eks_cluster.endpoint}"
    eks_cert     = "${aws_eks_cluster.eks_cluster.certificate_authority.0.data}"
  }
}

其中用于模板的文件如下:

apiVersion: v1
clusters:
- cluster:
    server: ${eks_endpoint}
    certificate-authority-data: ${eks_cert}
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: aws
  name: aws-${vpc_name}
current-context: aws-${vpc_name}
kind: Config
preferences: {}
users:
- name: aws
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1alpha1
      command: heptio-authenticator-aws
      args:
        - "token"
        - "-i"
        - "${eks_name}"
        #- "-r"
        #- "<role ARN>"
      #env:
        #- name: AWS_PROFILE
        #  value: "<profile>"

如果您在生成文件之前不需要对变量做任何事情,那么模板可能是更好的选择。

然后你可以使用渲染文件运行命令:

resource "null_resource" "config_setup" {
   triggers {
    kubeconfig_change  = "${data.template_file.kube_config.rendered}"
    configmap_change   = "{local.config-map-aws-auth}"
  }

  provisioner "local-exec" {
    command = "mkdir -p ${var.vpc_name}_output_EKS; echo '${data.template_file.kube_config.rendered}' >${var.vpc_name}_output_EKS/kubeconfig"
  }
}

【讨论】:

  • 感谢 Sevillo,感谢您的回复。将尝试使用 template_file 进行测试
  • 嗨@sevillo 你能在这里看看我的问题吗:stackoverflow.com/questions/65938396/…,它也与 local-exec 有关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 2015-11-26
  • 2020-12-24
  • 2019-11-21
  • 2019-08-20
  • 2021-05-03
  • 1970-01-01
相关资源
最近更新 更多