如何在 null-resource 中使用 count?

huangapple go评论83阅读模式
英文:

How to use count with null-resource?

问题

我已经定义了一个名为null_resource的资源,如下所示:

resource "null_resource" "mynull-resource" {
  triggers = {
    always-update = timestamp()
  }

  # Connection Block for Provisioners to connect to EC2 Instance
  connection {
    type        = "ssh"
    host        = aws_instance.web[count.index].public_ip  #Giving error
    user        = "ec2-user"
    password    = ""
    private_key = file("ec2keypair.pem")
  }
}

错误信息:

The "count" object can only be used in "module", "resource", and "data" blocks, and only when the "count" argument is set.

我该如何更新connection block以连接到多个主机?

我尝试使用spat operator,但是host = aws_instance.web.*.public_ip不起作用。

英文:

I have defined a null resource which looks as below:

resource "null_resource" "mynull-resource" {
  triggers = {
    always-update =  timestamp()
  }

  # Connection Block for Provisioners to connect to EC2 Instance
  connection {
    type = "ssh"
    host = aws_instance.web[count.index].public_ip  #Giving error
    user = "ec2-user"
    password = ""
    private_key = file("ec2keypair.pem")
  }

Error:

 The "count" object can only be used in "module", "resource", and "data" blocks, and only when the "count" argument is set.

How can I update the connection block to connect to multiple hosts?

I have tried using spat operator but it is not working host = aws_instance.web.*.public_ip

答案1

得分: 1

resource下使用count

resource "null_resource" "mynull-resource" {

  count = length(aws_instance.web)
 
  triggers = {
    always-update =  timestamp()
  }

  # Provisioners连接到EC2实例的连接块
  connection {
    type = "ssh"
    host = aws_instance.web[count.index].public_ip  #出现错误
    user = "ec2-user"
    password = ""
    private_key = file("ec2keypair.pem")
  }
}

请注意,我只提供了代码的翻译部分,不包括其他内容。

英文:

Use count under resource:

resource "null_resource" "mynull-resource" {

  count = length(aws_instance.web)
 
  triggers = {
    always-update =  timestamp()
  }

  # Connection Block for Provisioners to connect to EC2 Instance
  connection {
    type = "ssh"
    host = aws_instance.web[count.index].public_ip  #Giving error
    user = "ec2-user"
    password = ""
    private_key = file("ec2keypair.pem")
  }

huangapple
  • 本文由 发表于 2023年8月9日 12:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定