简体   繁体   English

Terraform templatefile条件语句生成ansible库存文件

[英]Terraform templatefile conditional statement to generate ansible inventory file

// Terraform v0.14.9

# var.tf
variable "launch_zk" {
  type        = string
  description = "Whether to launch zookeeper or not"
  default     = false
}

# main.tf
resource "aws_instance" "zk_ec2" {
  count                = var.launch_zk ? var.zk_instance_count : 0
...
} 
 
# output.tf 
output "zk_ips" {
  description = "IPs of ZK instances"
  value = {
    for vm in aws_instance.zk_ec2 :
    vm.tags.Name => vm.private_ip
  }
}

resource "local_file" "AnsibleInventoryFile" {
  content = templatefile("ansible_inventory.tpl",
    {
      zk-private-ip     = var.zk_instance_count < 10 ? slice(aws_instance.zk_ec2.*.private_ip, 0, 3) : slice(aws_instance.zk_ec2.*.private_ip, 0, 5),
      zk-private-dns    = var.zk_instance_count < 10 ? slice(aws_instance.zk_ec2.*.private_dns, 0, 3) : slice(aws_instance.zk_ec2.*.private_dns, 0, 5),
    }
  )
  filename = "ansible_inventory"
}

# ansible_inventory.tpl
[zk_servers]
%{ for index, dns in zk-private-dns ~}
${zk-private-ip[index]} server_name=${dns}
%{ endfor ~}

This is what I am using and now I want to conditionally generate output file including ansible inventory file.这就是我正在使用的,现在我想有条件地生成 output 文件,包括 ansible 库存文件。 It should include IP and DNS of zookeeper only if I am passing boolean true parameter to my "launch_zk" variable otherwise it should not print anything.仅当我将 boolean true 参数传递给我的“launch_zk”变量时,它才应该包括 Zookeeper 的 IP 和 DNS ,否则它不应该打印任何内容。 Here I am not able to perform conditional statement in my output file and ansible template tpl file.在这里,我无法在我的 output 文件和 ansible 模板 tpl 文件中执行条件语句。 Can someone tell me how can I get it working?有人能告诉我如何让它工作吗?

Here I will have to use multiple conditional statement like this but I am getting error given below在这里,我将不得不使用多个这样的条件语句,但我收到下面给出的错误

resource "local_file" "AnsibleInventoryFile" {
      content = templatefile("ansible_inventory.tpl",
        {
          zk-private-ip     = var.launch_zk ? var.zk_instance_count < 10 ? slice(aws_instance.zk_ec2.*.private_ip, 0, 3) : slice(aws_instance.zk_ec2.*.private_ip, 0, 5) : "",
          zk-private-dns    = var.launch ? var.zk_instance_count < 10 ? slice(aws_instance.zk_ec2.*.private_dns, 0, 3) : slice(aws_instance.zk_ec2.*.private_dns, 0, 5) : "",
        }
      )
      filename = "ansible_inventory"
    }


# Error
Error: Inconsistent conditional result types

  on output.tf line 67, in resource "local_file" "AnsibleInventoryFile":
  67:       zk-private-dns = var.launch_zk ? aws_instance.zk_ec2.*.private_dns : "",
    |----------------
    | aws_instance.zk_ec2 is empty tuple
    | var.launch_zk is "false"

The true and false result expressions must have consistent types. The given
expressions are tuple and string, respectively.

As docs explain, your condition must have consistent types :正如文档所解释的,您的条件必须具有一致的类型

The two result values may be of any type, but they must both be of the same type so that Terraform can determine what type the whole conditional expression will return without knowing the condition value.这两个结果值可以是任何类型,但它们必须是相同类型,以便 Terraform 可以在不知道条件值的情况下确定整个条件表达式将返回什么类型。

In your case you return a list, and string:在您的情况下,您返回一个列表和字符串:

#                              ? list                              : string    
zk-private-dns = var.launch_zk ? aws_instance.zk_ec2.*.private_dns : ""

The easiest way to ensure consistent types is by having an empty list :确保类型一致的最简单方法是使用一个空列表

zk-private-dns = var.launch_zk ? aws_instance.zk_ec2.*.private_dns : []

This change may require further changes in your code to account for the empty list此更改可能需要进一步更改您的代码以解决空列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM