【问题标题】:Terraform aws: [WARN] retryable error: dial tcp: lookup self.public_ip on 127.0.0.53:53: no such hostTerraform aws:[WARN] 可重试错误:拨号 tcp:在 127.0.0.53:53 上查找 self.public_ip:没有这样的主机
【发布时间】:2020-08-31 23:50:05
【问题描述】:

问题:

大家好,

感谢您的宝贵时间。所以我是 Terraform 的新手,一般来说是 devops。 我觉得我在供应商连接块下做错了什么。 我正在尝试使用 terraform 创建一个 ansible 主从配置。为了让我的主人能够与奴隶交谈,需要主节点的 ssh 公钥在 .ssh/authorized 中的所有奴隶中可用,因为我正在尝试 ssh 并在创建时传递主公钥奴隶。 出于某种原因,我在创建时无法通过 ssh 连接到从站,我尝试了所有我能想象的并浏览了很多论坛。我确定我可能在这里做错了什么。 任何帮助,将不胜感激。 问候。

Terraform 版本

Terraform v0.13.0

Terraform 配置文件

variable "region" {
  default = "us-east-1"
}

variable "type" {
  default = "t2.micro"  
}

variable "ec2LinuxAmi" {
  type = map(string)
  default = {
    us-east-1 = "ami-0bcc094591f354be2"
  }  
}

variable "keyname" {
  default = "terraformKeys"  
}

variable "privateKeyPath" {
  description = "Path to private key"
  default = "/home/userName/.ssh/id_rsa"
}

variable "awsKey" {
  default = "terraformKeys.pem"
}

variable "user_names" {
  description = "Create IAM users with these names"
  type        = list(string)
  default     = ["ansibleMaster"]
}


provider "aws" {
  region = var.region
  shared_credentials_file = "/home/userName/.aws/credentials"
  profile = "default"
}

resource "aws_security_group" "port_22_ingress_globally_accessible" {
    name = "port_22_ingress_globally_accessible"

    ingress { 
        from_port = 22    
        to_port = 22
        protocol = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
}

resource "aws_instance" "linux"{
  count = length(var.user_names)
  ami = lookup(var.ec2LinuxAmi, var.region)
  instance_type = var.type
  security_groups = [ "port_22_ingress_globally_accessible" ]
  key_name = var.keyname

  tags = {
    Name = var.user_names[count.index]
}

  provisioner "file" {
    source      = "foo"
    destination = "/tmp/foo"
  }
    connection {
      type = "ssh"
      user = "ubuntu"
      host = "self.public_ip"
      port = 22
      private_key = "${file("/home/userName/.ssh/id_rsa")}"
  }
}

调试输出

2020/08/30 19:10:30 [WARN] Provider "registry.terraform.io/hashicorp/aws" produced an unexpected new value for aws_instance.linux[0], but we are tolerating it because it is using the legacy plugin SDK.
The following problems may be the cause of any confusing errors from downstream operations:
- .disable_api_termination: was null, but now cty.False
- .ebs_optimized: was null, but now cty.False
- .hibernation: was null, but now cty.False
- .monitoring: was null, but now cty.False
- .iam_instance_profile: was null, but now cty.StringVal("")
- .credit_specification: block count changed from 0 to 1
2020/08/30 19:10:30 [TRACE] eval: *terraform.EvalMaybeTainted
2020/08/30 19:10:30 [TRACE] eval: *terraform.EvalWriteState
2020/08/30 19:10:30 [TRACE] EvalWriteState: recording 0 dependencies for aws_instance.linux[0]
2020/08/30 19:10:30 [TRACE] EvalWriteState: writing current state object for aws_instance.linux[0]
2020/08/30 19:10:30 [TRACE] eval: *terraform.EvalApplyProvisioners
2020/08/30 19:10:30 [TRACE] EvalApplyProvisioners: provisioning aws_instance.linux[0] with "file"
aws_instance.linux[0]: Provisioning with 'file'...
2020-08-30T19:10:30.228-0400 [DEBUG] plugin.terraform: file-provisioner (internal) 2020/08/30 19:10:30 using private key for authentication
2020-08-30T19:10:30.229-0400 [DEBUG] plugin.terraform: file-provisioner (internal) 2020/08/30 19:10:30 [DEBUG] Connecting to self.public_ip:22 for SSH
2020-08-30T19:10:30.248-0400 [DEBUG] plugin.terraform: file-provisioner (internal) 2020/08/30 19:10:30 [ERROR] connection error: dial tcp: lookup self.public_ip on 127.0.0.53:53: no such host

调试结束于:

"Error: timeout - last error: SSH authentication failed
(ubuntu@18.204.3.15:22): ssh: handshake failed: ssh: unable to
authenticate, attempted methods [none publickey], no supported methods
remain". 

【问题讨论】:

    标签: amazon-web-services ssh terraform terraform-provider-aws


    【解决方案1】:

    您的主机将只是一个字符串“self.public_ip”:

    host = "self.public_ip"
    

    应该是:

    host = self.public_ip
    

    connection 也应该在 provisioner 块内:

    
      provisioner "file" {
        source      = "foo"
        destination = "/tmp/foo"
    
        connection {
          type = "ssh"
          user = "ubuntu"
          host = self.public_ip
          port = 22
          private_key = "${file("/home/userName/.ssh/id_rsa")}"
        }
      }
    

    最后,aws_key_pair 资源没有被创建。但也许它被排除在问题之外。

    【讨论】:

    • 很抱歉作为一个完整的菜鸟出来。我尝试了您的建议,它仍然给了我可重试的错误。我应该补充一下,(现在将补充)大约 5 分钟后调试以“错误:超时 - 最后一个错误:SSH 身份验证失败(ubuntu@18.204.3.15:22):ssh:握手失败:ssh:无法进行身份验证,尝试的方法 [无公钥],没有支持的方法”。另外,我确实通过 AWS Web 控制台创建了一个 aws pem 密钥,是否需要使用 terraform 重新创建它?
    • 无法连接到18.204.3.15:22 似乎与您最初在此处询问的名称解析失败是一个单独的问题,因为它显示了现在大概是您的 EC2 实例的正确公共 IP 地址。出于这个原因,我建议在 Stack Overflow 上打开一个新问题,其中包括您在合并 Marcin 的建议后拥有的完整配置以及您现在看到的完整错误输出,因为这个新问题的解决方案可能与你遇到的第一个。
    • @MartinAtkins 谢谢,会这样做的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2019-06-30
    • 1970-01-01
    • 2022-11-03
    • 2021-05-12
    相关资源
    最近更新 更多