首页 文章

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

提问于
浏览
0

这是我之前提出的以下内容的继续 .

I want to identify the public ip of the terraform execution environment and add it to the security group

我的terraform direcotry结构如下 .

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"

}

variables.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}",
  }
}

我使用main.tf中的module选项加载.tf文件在module / aws目录下

我想运行我的本地脚本,识别运行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

我遇到错误时的配置文件如下 .

main.tf

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

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

ssh.tf

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的某个值

如果有其他方法可以解决这个问题,请告诉我 . 感谢您阅读我的问题 .

1 回答

  • 1

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

    test.sh

    #!/bin/sh
    
    {
      IP_ADDRESS=$(curl ipinfo.io/ip)
    } &> /dev/null
    
    echo {\"ip\":\""$IP_ADDRESS"\"}
    

    现在,当您在main.tf中调用aws模块时,我们需要将ssh_ip_address变量设置为等于test.sh脚本结果的"ip"输出 .
    另外,我注意到在你的ssh.tf文件中你试图引用aws模块中的"$
    ",但由于该数据是在模块之外定义的,所以它不起作用 . 我建议在variables.tf中创建一个"map"类型的加法变量,然后可以在main.tf中设置;在我的例子中,我添加了"command_output"变量 .

    在这里你可以看到我添加了command_output变量:

    variable.tf

    variable ssh_ip_address {
      default     = "xxx.xx.xx.xx"
    }
    
    variable "command_output" {
      type = "map"
    }
    
    variable name {
      default = "test"
    }
    

    这是main.tf看起来不像(注意我们正在设置aws模块中定义的变量):

    main.tf

    data "external" "example" {
      program = ["sh", "test.sh" ]
    }
    
    module "aws" {
      source = "./modules/aws"
    
      ssh_ip_address = "${data.external.example.result.ip}"
    
      command_output = "${data.external.example.result}"
    }
    

    最后,这是ssh.tf文件:

    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 = "${var.command_output}"
    }
    

    希望这可以帮助!

相关问题