如何将varable.tf放入terraform中运行外部数据的脚本

如何将varable.tf放入terraform中运行外部数据的脚本,terraform,Terraform,这是我之前问过的以下内容的延续 我的地形定向结构如下 root/  ├ main.tf  ├ test.sh  ├ modules/aws  │  └ ssh.tf/  │  └ variables.tf/ test.sh #!/bin/sh -xe echo {\"ip\":\""`curl ipinfo.io/ip`"\"} main.tf module "aws" { source = "./modules/aws" } data "ext

这是我之前问过的以下内容的延续

我的地形定向结构如下

root/
 ├ main.tf
 ├ test.sh
 ├ modules/aws
 │          └ ssh.tf/
 │          └ variables.tf/
test.sh

#!/bin/sh -xe

echo {\"ip\":\""`curl ipinfo.io/ip`"\"}
main.tf

module "aws" {

  source = "./modules/aws"

}
data "external" "example" {
  program = ["sh", "test.sh" ]
}

module "aws" {    
 source = "./modules/aws"    
}
变量.tf

variable ssh_ip_address {
  default     = "xxx.xx.xx.xx"
}
ssh.tf

resource "aws_security_group" "ssh" {
  name        = "${var.name}-ssh"
  description = "Allow connection by ssh"
  vpc_id      = "${aws_vpc.main.id}"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["${var.ssh_ip_address}/32"]
  }

  egress {
    from_port   = 0 
    to_port     = 0 
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = { 
    Name = "${var.name}",
  }
}
output "commandout" {
  value = "${data.external.example.result}"
}

resource "aws_security_group" "ssh" {
      name        = "${var.name}-ssh"
      description = "Allow connection by ssh"
      vpc_id      = "${aws_vpc.main.id}"

      ingress {
        from_port   = 22
        to_port     = 22
        protocol    = "tcp"
        cidr_blocks = ["${data.external.external.result}/32"]
      }

      egress {
        from_port   = 0 
        to_port     = 0 
        protocol    = "-1"
        cidr_blocks = ["0.0.0.0/0"]
      }

      tags = { 
        Name = "${var.name}",
      }
    }
我使用main.tf中的module选项在module/aws目录下加载.tf文件

我想运行我的本地脚本,识别运行terraform的公共IP,并在ssh.tf文件中使用结果

我正在使用数据外部选项运行本地脚本

data "external" "example" {
  program = ["sh", "test.sh" ]
}
但是,在我自己的数据验证中, 数据外部选项未正确执行,main.tf除外

在main.tf中运行外部数据并在ssh.tf中使用时, 我们得到的错误如下,这样的值不存在

output 'commandout': unknown resource 'data.external.example' referenced in variable data.external.example.result
我遇到错误时的配置文件如下所示

root/
 ├ main.tf
 ├ test.sh
 ├ modules/aws
 │          └ ssh.tf/
 │          └ variables.tf/
main.tf

module "aws" {

  source = "./modules/aws"

}
data "external" "example" {
  program = ["sh", "test.sh" ]
}

module "aws" {    
 source = "./modules/aws"    
}
ssh.tf

resource "aws_security_group" "ssh" {
  name        = "${var.name}-ssh"
  description = "Allow connection by ssh"
  vpc_id      = "${aws_vpc.main.id}"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["${var.ssh_ip_address}/32"]
  }

  egress {
    from_port   = 0 
    to_port     = 0 
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = { 
    Name = "${var.name}",
  }
}
output "commandout" {
  value = "${data.external.example.result}"
}

resource "aws_security_group" "ssh" {
      name        = "${var.name}-ssh"
      description = "Allow connection by ssh"
      vpc_id      = "${aws_vpc.main.id}"

      ingress {
        from_port   = 22
        to_port     = 22
        protocol    = "tcp"
        cidr_blocks = ["${data.external.external.result}/32"]
      }

      egress {
        from_port   = 0 
        to_port     = 0 
        protocol    = "-1"
        cidr_blocks = ["0.0.0.0/0"]
      }

      tags = { 
        Name = "${var.name}",
      }
    }
有没有办法在terraform中以最快的速度运行tf.file

如果有这样的方法,我想首先使用data external选项来执行它,并在ssh.tf中使用它

或者我想将数据外部执行的结果放入var.tf的某个值中

请让我知道是否有其他方法来解决这个问题。
感谢您阅读我的问题。

要使其正常工作,您需要进行一些更改。因此test.sh脚本只需要输出json格式,但现在(或运行它时)我也从curl命令获得输出。我必须包装curl命令并忽略输出,然后使用变量完成json,如下所示:

test.sh 现在,当您在main.tf中调用aws模块时,我们希望将ssh_ip_address变量设置为等于test.sh脚本结果的“ip”输出。
另外,我注意到,在ssh.tf文件中,您试图在aws模块内部引用“${data.external.example.result}”,但由于该数据是在模块外部定义的,因此无法工作。我建议在variables.tf中创建一个类型为“map”的加法变量,然后可以在main.tf中设置该变量;在我的示例中,我添加了“command\u output”变量

在这里您可以看到我添加了命令输出变量:

变量.tf 以下是main.tf的外观(注意,我们正在设置aws模块中定义的变量):

main.tf 最后,这里是ssh.tf文件:

ssh.tf
希望这有帮助

你能给出你考试的内容吗。sh@AnshuPrateek谢谢你的评论。我加上去了。