【问题标题】:Configure an Azure windows virtual machine using Ansible使用 Ansible 配置 Azure Windows 虚拟机
【发布时间】:2022-01-25 23:57:49
【问题描述】:

我正在尝试学习基础设施即代码,为此我使用 Terraform。 我还试图了解如何使用 Ansible 管理 VM 配置。 使用 Terraform,我可以毫无问题地创建 Azure Windows 虚拟机,但我试图了解为什么 Ansible 无法通过 SSH 连接到虚拟机。 不幸的是,我找到的所有示例都是关于 Linux 虚拟机的,但我需要创建一个 Windows 虚拟机。

这是用于创建 VM 的 Terraform 代码

resource "azurerm_network_security_group" "nsg" {
  name                = "SSH"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  security_rule {
    name                       = "SSH"
    priority                   = 1001
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }

  tags = {
    environment = "Test"
  }
}

resource "azurerm_subnet_network_security_group_association" "secgroup-assoc" {
  subnet_id                 = azurerm_subnet.subnet.id
  network_security_group_id = azurerm_network_security_group.nsg.id
}

resource "azurerm_windows_virtual_machine" "runner" {
  name                  = "runner"
  resource_group_name   = azurerm_resource_group.rg.name
  location              = azurerm_resource_group.rg.location
  size                  = "Standard_B2s"
  admin_username        = "runner_admin"
  admin_password        = "*****"
  network_interface_ids = [azurerm_network_interface.network_interface.id]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
    disk_size_gb         = 64
  }

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2022-datacenter-azure-edition-smalldisk"
    version   = "latest"
  }

  connection {
    host        = self.public_ip_address
    user        = "runner_admin"
    type        = "ssh"
    private_key = file("~/.ssh/id_rsa")
    timeout     = "4m"
    agent       = false
  }
}

这是 Ansible 的清单文件

[runners]
<VM public IP>    ansible_connection=ssh        ansible_user=runner_admin

这就是 Ansible 剧本

- name: Install Dependencies
  hosts: runners
  tasks:
    - name: Install git
      win_chocolatey:
        name: git
        state: present

提前谢谢你

【问题讨论】:

  • 您是否真的在 windows 机器上安装了 ssh 服务器(Openssh or win32-ssh)?从 ansible 到 windows 最常见的连接类型通常是WinRM。有一个full example 说明如何使用 ansible 创建 Win 主机并连接到它。您可以适应 terraform。
  • 感谢@Zeitounator 的澄清。现在的复杂工作是如何自动化 WinRM 配置。我发现this 的帖子解释了如何使用 PowerShell 配置 WinRM。我将尝试创建一个 Powershell 脚本以使用 Terraform 执行。
  • 在我上面给你的例子中,这一切都通过 ansible 进行了解释和自动化......
  • 是的,你是对的 :) 再次感谢@Zeitounator

标签: azure ansible terraform


【解决方案1】:

要通过 SSH 连接到 Windows 虚拟机,您需要在 Windows VM 中安装 SSH 服务器。

因为 Windows 虚拟机不像 Linux 虚拟机那样有任何默认的 SSH

您可以在 Windows 虚拟机上安装 OpenSSH 服务器。

然后您就可以使用 Ansible 通过 SSH 连接到您的 Windows VM

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2019-11-05
    • 2019-02-06
    • 2012-10-02
    • 1970-01-01
    相关资源
    最近更新 更多