我正在尝试从 Terraform 中的本地 exec block 内调用 shell 脚本。 shell 脚本基本上使用 terraform 输出(大约 8 个输出)并生成一个 YML 文件(作为变量),我稍后使用 cat << EOT...>> 方法使用该文件。在脚本中,我还使用 Ssh 私钥等进行了一些格式化。这确实行不通。 最好的方法是什么?我可以在 local-exec 中使用任何 Linux 命令吗?有没有更好的方法来利用 terraform 输出? 我主要想使用不同模块的某些输出并创建一个 YML 文件(如键值对)。
最佳答案
为什么不使用 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"
}
}
关于terraform - 在 local-exec-terraform 中调用 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60913412/