Terraform,如何在配置时忽略失败的服务器?

时间:2016-06-20 18:15:41

标签: provisioning terraform

我正在使用私有openstack云运行terraform以引导新服务器。当我在最繁忙的操作时间(下午的工作日)尝试创建新服务器(使用任何方法)时,通常有一半的服务器发生故障(这与terraform无关)。问题是当我尝试配置的其中一个服务器无法完成provisioner "remote-exec"块而没有错误(因为我的私有云)时,我的整个terraform apply停止了。

我希望terraform在运行terraform apply时完全忽略这些失败的服务器,这样如果我尝试配置20台服务器并且其中只有1台成功启动,那么那台服务器将运行我指定的所有命令我的资源块。

是否有类似ignore_failed_resources = true行的内容我可以添加到我的资源中,以便terraform会忽略失败的服务器并运行成功的服务器完成?

2 个答案:

答案 0 :(得分:0)

There's no simple config switch that you can enable to achieve this. Could you be more specific about the problem that's causing the "remote-exec" to fail?

If it's because it's simply refusing to connect, you could switch out your "remote-exec" for a "local-exec" and wrap your command in a script, passing in the hostname of your server as a parameter. The script would then handle initiating the SSH connection and running the required commands. Make sure the script fails gracefully with an exit code of 0 if the error occurs, so that terraform will think that the script worked correctly.

resource "aws_instance" "web" {
    provisioner "local-exec" {
        command = "./myremotecommand.sh ${aws_instance.web.private_ip}"
    }
}

答案 1 :(得分:0)

我猜您已经找到了解决方案,但是在这里添加我的解决方案给将来会遇到此问题的任何人。

为什么不直接在命令部分中这样做呢?

provisioner "local-exec" {
    command = "my command || true"
}

这样,它将始终返回代码0,因此,如果shell忽略故障,terraform也将忽略它。

相关问题