在Terraform中,我如何在for循环中添加if条件?

3

我有一个terraform模板文件,其中有一个部分用于填写主机信息。我想在其中添加逻辑,仅在另一个变量(即type =“ocp”)时填写此部分。因此,如果 $type =“ocp”,则运行此循环。我该如何做到这一点?

ocp_hosts:
%{ for host in host_names }
  - name: ${host}
    device: "eth0"
    management:
      ip: ${ip_addrs[index(host_names, host)]}
%{ endfor }

我首先尝试在for循环中添加一个"if contains(${type}, "ocp")"的条件语句,也尝试添加了一个条件块,但由于格式或函数理解不正确,导致出现错误。

1个回答

2

我不太常使用模板,但从文档中看来,这个方法可能适合您。

%{if type == "ocp" }
ocp_hosts:
%{ for host in host_names }
  - name: ${host}
    device: "eth0"
    management:
      ip: ${ip_addrs[index(host_names, host)]}
%{ endfor }
%{ endif }

2
从文档中获取 - 哪一个?有链接吗? - Marcin
指令 - 在文档中找到了这个。 - Slava
如果我们知道你的变量,就能给你一个更精确的例子。 - Derek Williams

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接