- 我正在尝试使用 null_resource 通过 Terraform 的远程执行配置程序来配置多个 Windows EC2 实例。
$ terraform -v
Terraform v0.12.6
provider.aws v2.23.0
provider.null v2.1.2
- 最初,我正在使用三个远程执行配置程序(其中两个涉及重新启动实例),没有 null_resource 和对于单个实例,一切都工作得非常好。
- 然后我需要增加计数,并根据几个链接,最终使用了 null_resource。因此,我已将问题减少到甚至无法使用 null_resource 为 2 个以上 Windows EC2 实例运行一个远程执行配置程序。
用于重现错误消息的 Terraform 模板:
//VARIABLES
variable "aws_access_key" {
default = "AK"
}
variable "aws_secret_key" {
default = "SAK"
}
variable "instance_count" {
default = "3"
}
variable "username" {
default = "Administrator"
}
variable "admin_password" {
default = "Password"
}
variable "instance_name" {
default = "Testing"
}
variable "vpc_id" {
default = "vpc-id"
}
//PROVIDERS
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "ap-southeast-2"
}
//RESOURCES
resource "aws_instance" "ec2instance" {
count = "${var.instance_count}"
ami = "Windows AMI"
instance_type = "t2.xlarge"
key_name = "ec2_key"
subnet_id = "subnet-id"
vpc_security_group_ids = ["${aws_security_group.ec2instance-sg.id}"]
tags = {
Name = "${var.instance_name}-${count.index}"
}
}
resource "null_resource" "nullresource" {
count = "${var.instance_count}"
connection {
type = "winrm"
host = "${element(aws_instance.ec2instance.*.private_ip, count.index)}"
user = "${var.username}"
password = "${var.admin_password}"
timeout = "10m"
}
provisioner "remote-exec" {
inline = [
"powershell.exe Write-Host Instance_No=${count.index}"
]
}
// provisioner "local-exec" {
// command = "powershell.exe Write-Host Instance_No=${count.index}"
// }
// provisioner "file" {
// source = "testscript"
// destination = "D:/testscript"
// }
}
resource "aws_security_group" "ec2instance-sg" {
name = "${var.instance_name}-sg"
vpc_id = "${var.vpc_id}"
// RDP
ingress {
from_port = 3389
to_port = 3389
protocol = "tcp"
cidr_blocks = ["CIDR"]
}
// WinRM access from the machine running TF to the instance
ingress {
from_port = 5985
to_port = 5985
protocol = "tcp"
cidr_blocks = ["CIDR"]
}
tags = {
Name = "${var.instance_name}-sg"
}
}
//OUTPUTS
output "private_ip" {
value = "${aws_instance.ec2instance.*.private_ip}"
}
观察结果:
- 对于一个远程执行配置程序,如果计数设置为 1 或 2,则可以正常工作。如果计数设置为 3,则无法预测所有配置程序每次都会在所有实例上运行。然而有一件事是肯定的,Terraform 永远不会完成并且不显示输出变量。它不断显示“null_resource.nullresource[count.index]:仍在创建...”
- For the 本地执行供应者- 一切正常。使用 count 的值为 1、2 和 7 进行测试。
- For 文件提供者它在 1、2 和 3 中工作正常,但在 7 中无法完成,但文件已复制到所有 7 个实例上。它不断显示“null_resource.nullresource[count.index]:仍在创建...”
- 此外,在每次尝试中,remote-exec 配置程序都能够连接到实例,而不管计数的值如何,只是它不会触发内联命令并随机选择跳过该命令并开始显示“仍在创建...”消息。
- 我已经被这个问题困扰有一段时间了。在调试日志中也找不到任何重要的内容。我知道不建议将 Terraform 用作配置管理工具,但是,如果实例计数仅为 1(即使没有 null_resource),即使使用复杂的配置脚本,一切也能正常工作,这表明 Terraform 应该可以轻松处理这样的情况基本供应要求。
- TF_DEBUG 日志:
-
count=2,TF 成功完成并显示Apply Complete! https://gist.github.com/anand-swaroop-git/cd84b62226f2a3a9e8a225f8c0039ab4.
- count=3,TF 在所有三个实例上运行远程执行,但未完成且不显示输出变量。卡在“仍在创建...” https://gist.github.com/anand-swaroop-git/92015c1c8fc82ef3731d48d6258e89d4
- count=3,TF 仅在两个实例上运行远程执行并跳过 nullresource[1] ,不完成且不显示输出变量。卡在“仍在创建...” https://gist.github.com/anand-swaroop-git/0355d7a60a609dcaaf7fbb31bf096f6c
- 任何指示将不胜感激!
Update:最终的伎俩是将 Terraform 降级为v11.14
按照这个发表评论 https://github.com/hashicorp/terraform/issues/22006#issuecomment-509588621.
您可以尝试以下几件事:
- Inline
remote-exec
:
resource "aws_instance" "ec2instance" {
count = "${var.instance_count}"
# ...
provisioner "remote-exec" {
connection {
# ...
}
inline = [
# ...
]
}
}
现在你可以参考self https://www.terraform.io/docs/configuration-0-11/interpolation.html#attributes-of-your-own-resource在 - 的里面connection
阻止获取实例的私有IP。
- Add
triggers
to null_resource
:
resource "null_resource" "nullresource" {
triggers {
host = "${element(aws_instance.ec2instance.*.private_ip, count.index)}" # Rerun when IP changes
version = "${timestamp()}" # ...or rerun every time
}
# ...
}
您可以使用triggers属性 https://www.terraform.io/docs/providers/null/resource.html#triggers重新创造null_resource
从而重新执行remote-exec
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)