简体   繁体   English

如何从 terraform 创建 ansible 库存?

[英]How to create ansible inventory from terraform?

I relay stack with that simple question.我用这个简单的问题中继堆栈。

Assume i need create few instance resources so how can i iterate from from tf variables to gather all private ips and pass it to ansible inventory file.假设我需要创建一些实例资源,那么我如何从 tf 变量中迭代以收集所有私有 ip 并将其传递给 ansible 库存文件。

As i found i have to use * like here:正如我发现我必须在这里使用 * :

k8s_master_name = "${join("\n", azurerm_virtual_machine.k8s-master.*.name)}"

But i as think for me it will look like:但我认为对我来说它看起来像:

inst_ip = "${join("\n", ${aws_instance.*.private_ip})}"

But i got error:但我得到了错误:

Error: Invalid reference
 on crc.cloud.connect.tf line 72, in resource "local_file" "servers1":
 72:       inst_ip = "${join("\n", aws_instance.*.private_ip)}"
  
 A reference to a resource type must be followed by at least one attribute
 access, specifying the resource name.

Full tf file:完整的 tf 文件:

  resource "aws_instance" "sp-1" {
  ami               = "cmi-993E674A"
  instance_type     = "c5.large"
  monitoring        = true
  source_dest_check = false
  user_data         = file("user_data.sh")
  subnet_id              = "subnet-F6C45280"
  private_ip             = "172.31.16.18"
  vpc_security_group_ids = ["sg-230C7615"]
  key_name = "mmk-key"
  #network_interface {
  #  network_interface_id = "${aws_network_interface.ni-sp-1.id}"
  #  device_index         = 0
  #}
  tags = {
    desc = "sp-1"  
    group_name = "sp"
  }

}

  resource "aws_instance" "sp-2" {
  ami               = "cmi-993E674A"
  instance_type     = "c5.large"
  monitoring        = true
  source_dest_check = false
  user_data         = file("user_data.sh")
  subnet_id              = "subnet-F6C45280"
  private_ip             = "172.31.16.19"
  vpc_security_group_ids = ["sg-230C7615"]
  key_name = "mmk-key"
  tags = {
    desc = "sp-2"  
    group_name = "sp"

  }
}

resource "local_file" "servers1" {
  content = templatefile("${path.module}/templates/servers1.tpl",
    {
      inst_ip = "${join("\n", ${aws_instance.*.private_ip})}"
    }
  )
  filename = "../ansible/inventory/servers1"
}

Per the Terraform documentation , you need to reference the resource type and its associated name.根据 Terraform 文档,您需要引用资源类型及其关联名称。

In your configuration file, you have an aws_instance resource with the name sp-1 .在您的配置文件中,您有一个名为sp-1aws_instance资源。 If you wish to access the private_ip attribute of the resource, you need to do it like so: aws_instance.sp-1[*].private_ip .如果您希望访问资源的private_ip属性,您需要这样做: aws_instance.sp-1[*].private_ip

You are creating a single instance aws_instance.sp-1 , not multiple instances.您正在创建单个实例aws_instance.sp-1 ,而不是多个实例。 To create multiple instance you would need to use count or for_each , or provision instances through aws_autoscaling_group .要创建多个实例,您需要使用countfor_each ,或通过aws_autoscaling_group配置实例。

Therefore, to access private_ip you don't really need splat * and join in your case (but still can use them if you want) as you have only one instance and will have only one private_ip .因此,要访问private_ip您实际上并不需要 splat *join您的案例(但如果您愿意,仍然可以使用它们),因为您只有一个实例并且只有一个private_ip The following should be enough instead:以下应该足够了:

inst_ip = aws_instance.sp-1.private_ip

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

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