【发布时间】:2020-06-25 16:45:06
【问题描述】:
- 我正在尝试使用 null_resource 使用 Terraform 的 remote-exec 供应器供应多个 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]:仍在创建...”
- 对于 local-exec 供应商 - 一切正常。使用 count 的值为 1、2 和 7 进行测试。
- 对于 file provisioner,它在 1、2 和 3 上工作正常,但在 7 上没有完成,但文件已复制到所有 7 个实例上。它一直显示“null_resource.nullresource[count.index]:仍在创建...”
- 此外,在每次尝试中,remote-exec 配置程序都能够连接到实例,而不管 count 的值是多少,只是它不会触发内联命令,而是随机选择跳过该命令并开始显示“仍在创建... ”消息。
- 我已经被这个问题困扰了很长一段时间了。在调试日志中也找不到任何重要的东西。我知道不建议将 Terraform 用作配置管理工具,但是,如果实例计数仅为 1(即使没有 null_resource),即使使用复杂的配置脚本,一切都可以正常工作,这表明 Terraform 应该很容易处理这样的基本配置要求。
- TF_DEBUG 日志:
- count=2, TF completes successfully and shows Apply complete!。
- count=3, TF runs the remote-exec on all the three instances however does not complete and doesn't not show the outputs variables. Stuck at "Still creating..."
- count=3, TF runs the remote-exec only on two instances and skips on nullresource[1] , does not complete and doesn't not show the outputs variables. Stuck at "Still creating..."
- 任何指针将不胜感激!
【问题讨论】:
-
请不要过于简单化,找出错误的地方是没有帮助的。将您的示例简化为 minimal reproducible example,其他人可以运行但仍会看到与您相同的错误。
-
感谢您的反馈。我已经修改了代码,现在可以复制了。
-
@ydaetskcoR 添加了模板,这样比之前的 sn-p 更容易复制。
标签: terraform terraform-provider-aws winrm remote-execution