Search code examples
vagrantvagrant-plugin

vagrant-hostmanager (1.8.6) error when updating guest hosts?


I have vagrant-hostmanager 1.8.6 installed, and when I run vagrant hostmanager I end up with the following error:

[vagrant-hostmanager:guest] Updating hosts file on the virtual machine puppet_server...
sh: 1: Syntax error: "(" unexpected

...and the /etc/hosts file is not updated. Is there a way to fix this?

Here is my VagrantFile:

Vagrant.configure(2) do |config|

  config.vm.box = "blah/turnkey-lamp-14.2"

  config.vm.provider "virtualbox" do |vb|
      vb.cpus = 2
      vb.gui = true
  end

  config.vm.boot_timeout = 10000
  config.vm.network "private_network", type: "dhcp"

  #config.vm.provision :hostmanager

  config.ssh.insert_key = false
  #config.ssh.private_key_path = "/mnt/vm_lab/vagrant_box_storage/.vagrant.d/insecure_private_key"
  config.ssh.forward_agent = true

  config.hostmanager.enabled = true
  config.hostmanager.manage_guest = true
  #config.hostmanager.manage_host = true

  config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
      if vm.id
         `VBoxManage guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/1/V4/IP".split()[1]`
      end
  end

  config.vm.define :puppet_server do |srv|
      srv.vm.hostname = "puppet-server"
      srv.vm.network :private_network, ip: '10.0.3.15'
      srv.vm.provision "shell", inline: $puppetServerScript 
      srv.vm.synced_folder "src/puppet-server", "/etc/puppet", create: true

  end

  config.vm.define :bareOSdirector do |srv|
      srv.vm.hostname = "bareOSdirector"
      srv.vm.network :private_network, ip: '10.0.3.10'
      srv.vm.provision "shell", inline: $puppetClientBareOSdir
  end

  config.vm.define :webserver do |srv|
      srv.vm.hostname = "webserver"
      srv.vm.network :private_network, ip: '10.0.3.8'   
      srv.vm.provision "shell", inline: $puppetClientWebserver
  end
end

I thought it had something to do with the hosts file itself, so I emptied it and re-ran the command, but it still doesn't update it.


Solution

  • sh: 1: Syntax error: "(" unexpected turned out to be a shell error because the ruby split was contained in the shell command by accident.

    There were 2 problems...

    1. The syntax was a little bit wrong there was a ``` in the wrong spot between the shell and the ruby syntax.
    2. I was getting the IP address of the wrong card...

    I tried running the command to obtain the from the command line with:

    VBoxManage guestproperty get "puppet_server" "/VirtualBox/GuestInfo/Net/1/V4/IP
    Value: 172.x.x.x
    

    and it yielded the wrong ip address...so instead I tried:

    VBoxManage guestproperty get "puppet_server" "/VirtualBox/GuestInfo/Net/2/V4/IP
    Value: 10.0.3.15
    

    and I got the address I was expecting.

    The block:

      config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
          if vm.id
             `VBoxManage guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/1/V4/IP".split()[1]`
          end
      end
    

    Should have been:

      config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
          if vm.id
             `VBoxManage guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/2/V4/IP"`.split()[1]
          end
      end
    

    Then after bringing all the machines up, I ran vagrant hostmanager again, and all the /etc/hosts files were filled in as expected.