IP estático público somente para um Vagrantfile

0

Eu modifiquei o Vagrantfile para o taiga-vagrant baseado em VAGRANT DOCS / Redes públicas da seguinte forma:

# -*- mode: ruby -*-
# vi: set ft=ruby :
require "./source.rb"

ROOT_PATH = File.dirname(__FILE__)

VAGRANTFILE_API_VERSION = "2"

def configure_extra(config)
end

def configure(config)
  config.vm.box = "trusty64"
  config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # 'vagrant box outdated'. This is not recommended.
  # config.vm.box_check_update = false

  # Host name
  config.vm.hostname = "taiga"

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine.
  config.vm.network :forwarded_port, host: 8000, guest: 8000
  config.vm.network :forwarded_port, host: 9001, guest: 9001

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  config.vm.network "public_network", autoconfig: false

  config.vm.provision "fix-no-tty", type: "shell" do |s|
        s.privileged = false
        s.inline = "sudo sed -i '/tty/!s/mesg n/tty -s \&\& mesg n/' /root/.profile"
  end

  # manual ip
  config.vm.provision "shell",
    run: "always",
    inline: "ifconfig eth1 xxx.yyy.zzz.153 netmask 255.255.255.0 up"

  # manual ipv6
  config.vm.provision "shell",
    run: "always",
    inline: "ifconfig eth1 inet6 add fc00::17/7"

  # default router
  config.vm.provision "shell",
    run: "always",
    inline: "route add default gw xxx.yyy.zzz.132"

  # default router ipv6
  config.vm.provision "shell",
    run: "always",
    inline: "route -A inet6 add default gw fc00::1 eth1"

# delete default gw on eth0
  config.vm.provision "shell",
    run: "always",
    inline: "eval 'route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }''"

  # If true, then any SSH connections made will enable agent forwarding.
  # Default value: false
  config.ssh.forward_agent = true

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder "./data", "/home/vagrant/data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  config.vm.provider "virtualbox" do |vb|
    # Boot with headless mode
    vb.gui = false

    host = RbConfig::CONFIG['host_os']

    # Giving a quarter of system memory to VM and access to all available cpu cores
    if host =~ /darwin/
      cpus = 'sysctl -n hw.ncpu'.to_i
      # sysctl returns Bytes, converting to MB...
      mem = 'sysctl -n hw.memsize'.to_i / 1024 / 1024 / 4
    elsif host =~ /linux/
      cpus = 'nproc'.to_i
      # meminfo returns KB, converting to MB...
      mem = 'grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//''.to_i / 1024 / 4
    else
      # hardcoding values for windows...
      cpus = 2
      mem = 2048
    end

    vb.customize ["modifyvm", :id, "--memory", mem]
    vb.customize ["modifyvm", :id, "--cpus", cpus]
  end

  # Provisioning
  config.vm.provision "shell" do |shell|
    vagrant_shell_scripts_configure(
      shell,
      File.join(ROOT_PATH, "scripts"),
      "provision.sh",
      {}
    )
  end
end

# Look for a Vagrantfile.local to load
local_vagrantfile = "#{__FILE__}.local"
if File.exists?(local_vagrantfile)
  eval File.read(local_vagrantfile)
end

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  configure config
  configure_extra config
end

Depois de vagrant up , me perguntam "Em qual interface a rede deve se conectar?" . Depois de selecionar 1 (para eth0) , a máquina virtual do VirtualBox é lançada bem e eu posso conectar via ssh. As configurações de interface de rede desta VM são:

eth0      Link encap:Ethernet  HWaddr 08:00:27:70:2a:9d  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe70:2a9d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1462 errors:0 dropped:0 overruns:0 frame:0
          TX packets:968 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:151879 (151.8 KB)  TX bytes:122611 (122.6 KB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:2e:e8:7f  
          inet addr:xxx.yyy.zzz.153  Bcast:xxx.yyy.zzz.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe2e:e87f/64 Scope:Link
          inet6 addr: fc00::17/7 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1335 errors:0 dropped:0 overruns:0 frame:0
          TX packets:683 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1078620 (1.0 MB)  TX bytes:85690 (85.6 KB)

Atualização pequena

Durante o lançamento, existem várias mensagens de

default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    ...

Finalmente, a máquina inicia. Eu posso acessar taiga muito bem da internet externa. Mas, é necessário ter ambas as interfaces? Eu não posso ter o IP público estático somente para eth0?

    
por Nikos Alexandris 09.12.2015 / 19:16

1 resposta

0

A postagem no link foi útil. A linha seguinte fez o truque!

config.vm.network "public_network", :bridge => "eth0", ip: "xxx.yyy.zzz.153", :netmask => "255.255.255.128", auto_config: false
    
por 12.12.2015 / 20:44