首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过WinRM连接远程主机在Terraform中失败

通过WinRM连接远程主机在Terraform中失败
EN

Stack Overflow用户
提问于 2021-01-15 17:10:10
回答 1查看 1.8K关注 0票数 0

我试图在Windows上使用terraform在provisioner下面运行

代码语言:javascript
复制
provisioner "remote-exec" {
    connection {
      type = "winrm"
      user     = "${local.admin_username}"
      password = "${local.admin_password}"
      port     = 5986
      https    = true
      timeout  = "10m"
      host = azurerm_public_ip.example.ip_address        
      insecure = true
    }

    inline = [
      "powershell.exe New-Item -Path c:\\ -Name testfile1.txt -ItemType file -Value This is a text string."
    ]
  }

当提供VM时,我在尝试使用远程-exec建立连接时遇到错误。

代码语言:javascript
复制
azurerm_virtual_machine.example (remote-exec): Connecting to remote host via WinRM...
azurerm_virtual_machine.example (remote-exec):   Host: 52.172.xxx.xxx
azurerm_virtual_machine.example (remote-exec):   Port: 5986
azurerm_virtual_machine.example (remote-exec):   User: testadmin
azurerm_virtual_machine.example (remote-exec):   Password: true
azurerm_virtual_machine.example (remote-exec):   HTTPS: true
azurerm_virtual_machine.example (remote-exec):   Insecure: true
azurerm_virtual_machine.example (remote-exec):   NTLM: false
azurerm_virtual_machine.example (remote-exec):   CACert: false
azurerm_virtual_machine.example: Still creating... [11m50s elapsed]
azurerm_virtual_machine.example: Still creating... [12m0s elapsed]


Error: timeout - last error: unknown error Post "https://52.172.xxx.xxx:5986/wsman": dial tcp 52.172.xxx.xxx:5986: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

供应器是否以正确的格式提供?

EN

回答 1

Stack Overflow用户

发布于 2021-01-18 02:55:17

如果您想使用WinRM访问Azure,我们需要配置一些东西。有关更多细节,请参阅这里

例如

  1. 创建一个密钥库
代码语言:javascript
复制
New-AzKeyVault -VaultName "<vault-name>" -ResourceGroupName "<rg-name>" -Location "<vault-location>" -EnabledForDeployment -EnabledForTemplateDeployment
  1. 创建证书
代码语言:javascript
复制
$certificateName = "somename"

$thumbprint = (New-SelfSignedCertificate -DnsName $certificateName -CertStoreLocation Cert:\CurrentUser\My -KeySpec KeyExchange).Thumbprint

$cert = (Get-ChildItem -Path cert:\CurrentUser\My\$thumbprint)

$password = Read-Host -Prompt "Please enter the certificate password." -AsSecureString

Export-PfxCertificate -Cert $cert -FilePath ".\$certificateName.pfx" -Password $password
  1. 将证书上载到Azure密钥库
代码语言:javascript
复制
$fileName = "<Path to the .pfx file>"
$fileContentBytes = Get-Content $fileName -Encoding Byte
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)

[System.Collections.HashTable]$TableForJSON = @{
    "data"     = $filecontentencoded;
    "dataType" = "pfx";
    "password" = "<password>";
}
[System.String]$JSONObject = $TableForJSON | ConvertTo-Json

$secret = ConvertTo-SecureString -String $jsonEncoded -AsPlainText –Force
Set-AzKeyVaultSecret -VaultName "<vault name>" -Name "<secret name>" -SecretValue $secret
  1. 引用您的自签名证书URL
代码语言:javascript
复制
"osProfile": {
      ...
      "secrets": [
        {
          "sourceVault": {
            "id": "<resource id of the Key Vault containing the secret>"
          },
          "vaultCertificates": [
            {
              "certificateUrl": "<URL for the certificate you got in Step 4>",
              "certificateStore": "<Name of the certificate store on the VM>"
            }
          ]
        }
      ],
      "windowsConfiguration": {
        ...
        "winRM": {
          "listeners": [
            {
              "protocol": "http"
            },
            {
              "protocol": "https",
              "certificateUrl": "[reference(resourceId(resourceGroup().name, 'Microsoft.KeyVault/vaults/secrets', '<vault-name>', '<secret-name>'), '2015-06-01').secretUriWithVersion]"
            }
          ]
        },
        ...
      }
    },
  1. 连接到Azure以启用winRm服务
代码语言:javascript
复制
Enable-PSRemoting -Force
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65740510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档