Configuration d'un nom D'hôte VM: méthode indéfinie 'nom d'hôte'

J'essaie de définir le nom d'hôte pour la machine virtuelle. Voici mon fichier Vagrant:

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

Mais cela conduit à l'erreur suivante:

/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>'

J'utilise Vagrant 1.3.1 sous Ubuntu 12.10. (64 bits) et OpenSuSe 12.3 (32 bits) comme VM.

24
demandé sur Andres 2013-09-09 18:02:56

4 réponses

Essayez:

Web_config.vm.hostname = "web.my.net"

30
répondu Jahaja 2013-09-09 19:24:46

Ce que vous avez est une inadéquation entre votre code et la version de L'API Vagrant que vous utilisez.

  • pour Vagrant API v1 utiliser config.vm.host_name.
  • pour Vagrant API v2 utiliser config.vm.hostname.
34
répondu nelsonda 2014-01-31 21:14:01

Selon le numéro #1974, définition du nom d'hôte que vous devriez utiliser => config.vm.hostname = "web.my.net"

Donc le bloc devrait ressembler à

  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

Vous pouvez même générer un nom d'hôte aléatoire en utilisant le code ci-dessous

config.vm.hostname = "devops#{rand(01..99)}.vagrant.vm}"
13
répondu Terry Wang 2013-09-10 04:34:15

Modifier Vagrantfile:

config.vm.hostname = "WhateverHostNameYouWant"

Par exemple:

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
7
répondu Valter Silva 2016-09-10 23:48:22