Terraform-Azure Windows VM Winrm连接问题

时间:2019-04-15 10:06:06

标签: windows virtual-machine winrm terraform-provider-azure

我想创建Windows Azure虚拟机,复制一些文件并使用Terraform脚本在该VM上运行一些简单命令。 问题是:我可以创建虚拟机,但不能通过winrm连接。

provider "azurerm" {

  subscription_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  tenant_id       = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}    
resource "azurerm_virtual_network" "vnet" {
  name                = "cmTFVnet"
  address_space       = ["10.0.0.0/16"]
  location            = "South India"
  resource_group_name = "cservice"
}    
resource "azurerm_subnet" "subnet" {
  name                 = "cmTFSubnet"
  resource_group_name  = "cservice"
    virtual_network_name = "${azurerm_virtual_network.vnet.name}"
  address_prefix       = "10.0.2.0/24"
}    
resource "azurerm_public_ip" "publicip" {

    name                         = "cmTFPublicIP"
    location                     = "South India"
    resource_group_name          = "cservice"
    public_ip_address_allocation = "dynamic"
 }    
resource "azurerm_network_security_group" "nsg" {
    name                = "cmTFNSG"
    location            = "South India"
    resource_group_name = "cservice"

    security_rule {
        name                       = "SSH"
        priority                   = 340
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    }
    security_rule {  
    name                       = "winrm"  
    priority                   = 1010  
    direction                  = "Inbound"  
    access                     = "Allow"  
    protocol                   = "Tcp"  
    source_port_range          = "*"  
    destination_port_range     = "5985"  
    source_address_prefix      = "*"  
    destination_address_prefix = "*"  
  }  
  security_rule {   
    name                       = "winrm-out"  
    priority                   = 100  
    direction                  = "Outbound"  
    access                     = "Allow"  
    protocol                   = "*"  
    source_port_range          = "*"  
    destination_port_range     = "5985"  
    source_address_prefix      = "*"  
    destination_address_prefix = "*"  
  }         
}    
resource "azurerm_network_interface" "nic" {
  name                = "cmNIC"
  location            = "South India"
  resource_group_name = "cservice"
 network_security_group_id = "${azurerm_network_security_group.nsg.id}"
  ip_configuration {
    name                          = "compilerNICConfg"
    subnet_id                     = "${azurerm_subnet.subnet.id}"
    private_ip_address_allocation = "dynamic"
   public_ip_address_id          = "${azurerm_public_ip.publicip.id}"
  }
}
 resource "azurerm_virtual_machine" "vm" {
  name                  = "cmTFVM"
  location              = "South India"
  resource_group_name   = "cservice"
 network_interface_ids = ["${azurerm_network_interface.nic.id}"]
 vm_size            = "Standard_D2s_v3"

   storage_image_reference 
  {
    id =  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  }
    storage_os_disk {
    name          = "cmOsDisk"
    managed_disk_type = "Premium_LRS"
    create_option = "FromImage"

  }

  os_profile {
    computer_name  = "hostname"
    admin_username = "test"
    admin_password = "test@123"
  }
   os_profile_windows_config {
        enable_automatic_upgrades = "true"
        provision_vm_agent ="true"
        winrm = {  
                  protocol = "http"  
        }
   }
    provisioner "remote-exec" {
     connection   = {
     type        = "winrm"
     user        = "test"
     password    = "test@123"
     agent       = "false"
     https    = false
     insecure = true
    } 
    inline = [
      "cd..",
      "cd..",
      "cd docker",
      "mkdir test"
    ]
  }
 }

VM创建成功,但无法通过WINRM连接 但我在“远程执行”中遇到以下错误:

  

azurerm_virtual_machine.vm:超时-上一个错误:未知错误发布   http://:5985/wsman:拨打tcp:5985:connectex:尝试连接   失败,因为关联方在   一段时间,或由于连接而建立的连接失败   主机未响应。                       或http响应错误:401-无效的内容类型

1 个答案:

答案 0 :(得分:0)

创建Windows Azure VM时,默认情况下未配置WINRM。因此,如果要通过WINRM连接虚拟机,则应在创建虚拟机之后或在创建时间内配置WINRM。

您可以按照Configure WinRM after virtual machine creation中的步骤进行操作。您也可以在创建时对其进行配置。有一个例子显示了通过Azure模板。它还将提供一些帮助。参见Deploys a Windows VM and Configures a WinRM Https listener