2017-11-15 1 views
0

これは私が以前に尋ねた以下の内容の続きです。terraformの外部データを使ってスクリプトを実行するためにvarable.tfを入れる方法

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

次のように私のテラフォームの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

にモジュールのオプションを使用して、モジュール/ AWSディレクトリの下.tfファイルを読み込むと、私は、私の地元のスクリプトを実行したいとテラフォームを走ったパブリックIPを特定し、その結果を使用しますssh.tfファイル

私はローカルスクリプトを実行するためにdata externalオプションを使用しています。

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 

エラーが発生したときの設定ファイルは次のとおりです。テラフォームにtf.file最も急速に実行する方法は

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

ありますか?

、このような方法がある場合は、私が最初にそれを実行するために、データの外部オプションを使用するようにしたい、とssh.tf

でそれを使用するか、私は特定のへのデータの外部実行結果を入れたいと思いますvar.tfの値

この問題を解決する他の方法がある場合は教えてください。 私の質問をお読みいただきありがとうございます。

+0

あなたのtest.shの内容を教えてください。 –

+0

@AnshuPrateekコメントしていただきありがとうございます。私はそれを加えた。 – sukho

答えて

1

これが正しく機能するためには、いくつかの変更が必要です。 test.shスクリプトは結果としてjson形式のみを出力する必要がありますが、現在(または私がそれを実行しているとき)curlコマンドからも出力が得られます。私はcurlコマンドをラップして出力を無視し、次にjsonを完了するために変数を使用しなければならなかった:

test。今SH

#!/bin/sh 

{ 
    IP_ADDRESS=$(curl ipinfo.io/ip) 
} &> /dev/null 

echo {\"ip\":\""$IP_ADDRESS"\"} 

あなたは私たちがtest.shスクリプト結果の「IP」の出力を等しくするssh_ip_address変数を設定することになるでしょうmain.tfであなたのAWSモジュールを呼び出しています。
また、あなたのssh.tfファイルで、awsモジュール内の "$ {data.external.example.result}"を参照しようとしていますが、そのデータはモジュール外で定義されているため、作業。私は、あなたがmain.tfで設定できるvariables.tfに型 "map"の追加変数を作成することをお勧めします。私の例では、 "command_output"変数を追加しました。

ここでは、私はcommand_output変数を追加見ることができます:

variable.tfここ

​​

は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}" 
} 

・ホープこのことができます!

+0

すごい!私はあなたのおかげでそれを解決しました。 – sukho

関連する問題