hashicorp / vagrant

Vagrant is a tool for building and distributing development environments.
https://www.vagrantup.com
Other
26.24k stars 4.43k forks source link

WinRM error when vm.hostname not provided in the Vagrantfile - (10,8):UserId: $username = 'vagrant' #12644

Open pauby opened 2 years ago

pauby commented 2 years ago

Vagrant version

2.2.19

Host operating system

Windows 10 Pro 2004 build 19041.1415

Guest operating system

Windows Server 2019 (but occurs with Windows Server 2016)

Vagrantfile

Vagrant.configure("2") do |config|

  config.vm.define "win2019" do |cfg|

    cfg.vm.box = 'StefanScherer/windows_2019'
    cfg.vm.box_version = '2020.02.12'
    #cfg.vm.hostname = 'abc123'

    cfg.windows.halt_timeout = 120
    cfg.winrm.retry_limit = 30
    cfg.winrm.retry_delay = 10

    cfg.winrm.username = "vagrant"
    cfg.winrm.password = "vagrant"
    cfg.vm.guest = :windows
    cfg.vm.communicator = "winrm"

    cfg.windows.set_work_network = true

    cfg.vm.network :forwarded_port, guest: 5985, host: 5985, id: "winrm", auto_correct: true #, host_ip: "127.0.0.1"
    cfg.vm.network :forwarded_port, guest: 3389, host: 3389, id: "rdp", auto_correct: true #, host_ip: "127.0.0.1"

    cfg.vm.provision "Test!", type: 'shell', run: 'once', privileged: true, inline: 'write-host "hello world!"'

    cfg.vm.provider :hyperv do |v, override|
      override.vm.network "public_network", bridge: "Default Switch"
      v.enable_virtualization_extensions = true
      v.maxmemory = 4096
      v.cpus = 2
      v.ip_address_timeout = 130
      v.linked_clone = true
      v.vm_integration_services = {
        guest_service_interface: true,
        heartbeat: true,
        key_value_pair_exchange: true,
        shutdown: true,
        time_synchronization: true,
        vss: true
      }
    end
  end
end

Debug output

https://gist.github.com/pauby/3c77bb7c9e40f014e44d72d403ed3782

Expected behavior

The PowerShell code write-host "hello world!"' runs as it should with privileged: true regardless of the vm.hostname being present or not.

Actual behavior

When the vm.hostname is specified in the Vagrantfile, and privileged: true or privileged: false is specified when running the PowerShell code write-host "hello world!"', it runs normally with no errors.

When the vm.hostname is NOT specified in the Vagrantfile, and privileged: false is specified when running the PowerShell code write-host "hello world!"', it runs normally with no errors.

When the vm.hostname is NOT specified in the Vagrantfile, and privileged: true is specified when running the PowerShell code write-host "hello world!"' does not run and instead this error:

    win2019: (10,8):UserId:
    win2019: At line:1 char:1
    win2019: + $username = 'vagrant'
    win2019: + ~~~~~~~~~~~~~~~~~~~~~
    win2019:     + CategoryInfo          : OperationStopped: (:) [Write-Error], ArgumentException
    win2019:     + FullyQualifiedErrorId : System.ArgumentException

This can also been seen starting on line 3558 of the debug output

Steps to reproduce

  1. With the Vagrantfile above, using Hyper-V as the provider, run vagrant up

Notes

kaitlincart commented 1 year ago

Hi,

I'm sorry we weren't able to respond sooner. Were you able to resolve your hostname error?

Thanks, Kait

pauby commented 1 year ago

@kaitlincart This is a bug that remains in Vagrant.

Stromweld commented 1 year ago

im running into this as well with latest version of vagrant, macos, and virtualbox. I'm testing with test-kitchen and boxes I created like stomweld/windows-11 in the vagrant app store. If I add vm.hostname to the test-0kitchen generated Vagrant file then winrm works.