设置VM主机名:未定义方法`hostname'

时间:2013-09-09 14:02:56

标签: vagrant

我正在尝试为VM设置主机名。这是我的Vagrantfile:

Vagrant::Config.run do |config|
  config.vm.box = "opensuse-12.3-32"
  config.vm.define :web do |web_config|
    web_config.vm.hostname "web.my.net"
    web_config.vm.forward_port 80, 7080
    web_config.vm.provision :puppet do |puppet|
      puppet.manifests_path = "puppet"
      puppet.module_path = "puppet/modules"
      puppet.manifest_file  = "base.pp"
    end
  end
end

但它会导致以下错误:

/home/coder/vagrant/opensuse/Vagrantfile:40:in `block (2 levels) in <top (required)>': undefined method `hostname' for #<VagrantPlugins::Kernel_V1::VMConfig:0x00000002748fb8> (NoMethodError)
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/config/v1/loader.rb:37:in `call'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/config/v1/loader.rb:37:in `load'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/config/loader.rb:104:in `block (2 levels) in load'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/config/loader.rb:98:in `each'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/config/loader.rb:98:in `block in load'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/config/loader.rb:95:in `each'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/config/loader.rb:95:in `load'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/environment.rb:335:in `machine'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/plugin/v2/command.rb:134:in `block in with_target_vms'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/plugin/v2/command.rb:167:in `call'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/plugin/v2/command.rb:167:in `block in with_target_vms'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/plugin/v2/command.rb:166:in `map'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/plugin/v2/command.rb:166:in `with_target_vms'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/plugins/commands/status/command.rb:16:in `execute'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/cli.rb:38:in `execute'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/lib/vagrant/environment.rb:484:in `cli'
    from /opt/vagrant/embedded/gems/gems/vagrant-1.3.1/bin/vagrant:96:in `<top (required)>'
    from /opt/vagrant/bin/../embedded/gems/bin/vagrant:23:in `load'
    from /opt/vagrant/bin/../embedded/gems/bin/vagrant:23:in `<main>'

我在Ubuntu 12.10下使用Vagrant 1.3.1。 (64位)和OpenSuSe 12.3(32位)作为VM。

4 个答案:

答案 0 :(得分:34)

您所拥有的是您的代码与您正在使用的Vagrant API版本之间的不匹配。

  • 对于Vagrant API v1,请使用config.vm.host_name
  • 对于Vagrant API v2,请使用config.vm.hostname

答案 1 :(得分:30)

尝试:

  

web_config.vm.hostname =“web.my.net”

答案 2 :(得分:13)

根据问题#1974,设置您应该使用的主机名=&gt; config.vm.hostname = "web.my.net"

所以该块应该看起来像

  config.vm.define :web do |web_config|
    web_config.vm.hostname = "web.my.net"
    web_config.vm.forward_port 80, 7080
    web_config.vm.provision :puppet do |puppet|
      puppet.manifests_path = "puppet"
      puppet.module_path = "puppet/modules"
      puppet.manifest_file  = "base.pp"
    end
  end

您甚至可以使用以下代码生成一些随机主机名

config.vm.hostname = "devops#{rand(01..99)}.vagrant.vm}"

答案 3 :(得分:8)

修改Vagrantfile

config.vm.hostname = "WhateverHostNameYouWant"

例如:

Vagrant.configure(2) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.hostname = "vhost1.local"
  config.vm.network "private_network", ip: "192.168.50.4"
end