I am trying to copy files to virtual machines deployed to Azure via Terraform. No matter what I try, it constantly fails with WinRM
I have verified port 5895 is open to my IP address in Azure.
I am also using a user with local administrative privileges on the virtual machine. What else is needed to make this work? Below is my Terraform file. I have also tried doing this with Ansible, but that also fails.
resource "azurerm_network_interface" "workstation" {
count = length(local.domain.workstations)
name = "${var.prefix}-wks-${count.index}-nic"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
ip_configuration {
name = "static"
subnet_id = azurerm_subnet.workstations.id
private_ip_address_allocation = "Static"
private_ip_address = cidrhost(var.workstations_subnet_cidr, 10 + count.index)
public_ip_address_id = azurerm_public_ip.workstation[count.index].id
}
}
resource "azurerm_network_interface_security_group_association" "workstation" {
count = length(local.domain.workstations)
network_interface_id = azurerm_network_interface.workstation[count.index].id
network_security_group_id = azurerm_network_security_group.windows.id
}
resource "azurerm_virtual_machine" "workstation" {
count = length(local.domain.workstations)
name = local.domain.workstations[count.index].name
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
network_interface_ids = [azurerm_network_interface.workstation[count.index].id]
vm_size = var.workstations_vm_size
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_image_reference {
publisher = "MicrosoftWindowsDesktop"
offer = "Windows-10"
sku = "win10-22h2-pron-g2"
version = "latest"
}
storage_os_disk {
name = "wks-${count.index}-os-disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = local.domain.workstations[count.index].name
admin_username = local.domain.default_local_admin.username
admin_password = local.secret
}
os_profile_windows_config {
enable_automatic_upgrades = false
timezone = "Central European Standard Time"
winrm {
protocol = "HTTP"
}
}
tags = {
kind = "workstation"
}
}
resource "null_resource" "provision_workstation_once_dc_has_been_created" {
provisioner "local-exec" {
working_dir = "${path.root}/../ansible"
command = "/bin/bash -c 'source venv/bin/activate && ansible-playbook workstations.yml -v'"
}
depends_on = [
azurerm_virtual_machine.dc,
azurerm_virtual_machine.workstation
]
}
resource "null_resource" "copy_files" {
for_each = { for idx, nic in azurerm_network_interface.workstation : idx => nic.private_ip_address }
provisioner "file" {
source = "/home/user_name/files/setup.exe"
destination = "\\\\${each.value}\\C$\\users\\public"
}
connection {
type = "winrm"
user = "localadmin"
password = "Super_Secret_Password"
host = "${each.value}"
timeout = "5m"
insecure = true
}
depends_on = [
azurerm_virtual_machine.dc,
azurerm_virtual_machine.workstation,
azurerm_network_interface.workstation
]
}


I agree with Helder Sepulveda suggestion to use the
azurerm_virtual_machine_extension.Here is the updated script to deploy
Azure VMsand copy the files. You can use a for-each block to initiate the same process for eachVM.Terraform apply:
After running the script , the file has been copied to VM successfully.