如何在使用null-resource时使用count?

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

How to use count with null-resource?

问题

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

英文:

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

使用countresource下面:

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-2.html
匿名

发表评论

匿名网友

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

确定