【问题标题】:Terraform, conditionally execute some lines of code in template fileTerraform,有条件地执行模板文件中的一些代码行
【发布时间】:2021-09-01 13:27:42
【问题描述】:

在 terraform 中,有没有办法有条件地执行模板文件中的某些代码行?例如:

我有一个 test.sh.tpl 文件,定义如下,从 main.tf 调用

#!/bin/bash echo ${test_key} > /opt/test_key.properties

在上面的值中“test_key”是从变量中赋值的

现在我只想在 ${test_key} 值不为空的情况下执行上述代码。我试过了,但即使值存在也无法正常工作

{ if ${test_key} != "" }

echo ${test_key} > /opt/test_key.properties

{ endif }

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    可能是因为你缺少 %

    %{ if ${test_key} != "" }
    
    echo ${test_key} > /opt/test_key.properties
    
    %{ endif }
    

    https://www.terraform.io/docs/language/expressions/strings.html

    【讨论】:

    • 试过了,调用函数“templatefile”失败:./test.sh.tpl:3,7-8:无效字符;此字符未在该语言中使用。以及其他 2 个诊断。
    • 如果您通过 tpl 文件进行填充,您可以按照以下示例进行操作。因为在 shell 脚本中添加 if 条件很容易registry.terraform.io/providers/hashicorp/template/latest/docs/…
    • 说if ${test_key}` 也是无效的...${ 用于从文字模式切换到插值模式,但我们已经在if 模式这里可以(而且必须)说%{ if test_key != "" }
    猜你喜欢
    • 2021-09-06
    • 2017-05-13
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 2015-05-11
    相关资源
    最近更新 更多