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

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

How to use count with null-resource?

问题

I have defined a null resource which looks as below:

  1. resource "null_resource" "mynull-resource" {
  2. triggers = {
  3. always-update = timestamp()
  4. }
  5. # Connection Block for Provisioners to connect to EC2 Instance
  6. connection {
  7. type = "ssh"
  8. host = aws_instance.web[count.index].public_ip #Giving error
  9. user = "ec2-user"
  10. password = ""
  11. private_key = file("ec2keypair.pem")
  12. }
  13. }

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:

  1. resource "null_resource" "mynull-resource" {
  2. triggers = {
  3. always-update = timestamp()
  4. }
  5. # Connection Block for Provisioners to connect to EC2 Instance
  6. connection {
  7. type = "ssh"
  8. host = aws_instance.web[count.index].public_ip #Giving error
  9. user = "ec2-user"
  10. password = ""
  11. private_key = file("ec2keypair.pem")
  12. }

Error:

  1. 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下面:

  1. resource "null_resource" "mynull-resource" {
  2. count = length(aws_instance.web)
  3. triggers = {
  4. always-update = timestamp()
  5. }
  6. # 用于Provisioners连接到EC2实例的连接块
  7. connection {
  8. type = "ssh"
  9. host = aws_instance.web[count.index].public_ip #产生错误
  10. user = "ec2-user"
  11. password = ""
  12. private_key = file("ec2keypair.pem")
  13. }
  14. }
英文:

Use count under resource:

  1. resource "null_resource" "mynull-resource" {
  2. count = length(aws_instance.web)
  3. triggers = {
  4. always-update = timestamp()
  5. }
  6. # Connection Block for Provisioners to connect to EC2 Instance
  7. connection {
  8. type = "ssh"
  9. host = aws_instance.web[count.index].public_ip #Giving error
  10. user = "ec2-user"
  11. password = ""
  12. private_key = file("ec2keypair.pem")
  13. }

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:

确定