devopsgroup-io / vagrant-hostmanager

:pencil: A Vagrant plugin that manages hosts files within a multi-machine environment.
Mozilla Public License 2.0
1.46k stars 147 forks source link

/etc/hosts file on guests not being updated #216

Open diogogmt opened 8 years ago

diogogmt commented 8 years ago

Can somebody please confirm if the following is the expected behavior:

With a VirtualBox provider:

1 Create VM 1 with IP 192.168.100.10 and hostname vm01

2 Create VM 2 with IP 192.168.100.20 and hostname vm02

Actual results:

Expected results:

seth-reeser commented 8 years ago

Hi @diogogmt, can you please provide a copy of your Vagrantfile? Please remove any secrets.

diogogmt commented 7 years ago

@seth-reeser

Bellow follows the Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

SETTINGS = {
  provision_script: "ubuntu-bootstrap.sh",
  mount_path: "/Users/shogunrua/projects",
  registry_url: "registry.lab.local:5000",
  dns_server: "192.168.100.2",
  lab01: {
    hostname: "lab01",
    ip: "192.168.100.2",
    cpus: 2,
    memory: 2048,
  },
  agent01: {
    hostname: "agent01",
    ip: "192.168.100.3",
    cpus: 2,
    memory: 4096,
  },
  agent02: {
    hostname: "agent02",
    ip: "192.168.100.4",
    cpus: 2,
    memory: 3072,
  },
  nfs01: {
    hostname: "nfs01",
    ip: "192.168.100.5",
    cpus: 1,
    memory: 512,
  },
  aytra01: {
    hostname: "aytra01",
    ip: "192.168.100.6",
    cpus: 2,
    memory: 4096,
    mount_path: "/Users/shogunrua/projects/work/aytra",
    provision_script: "aytra01-bootstrap.sh",
  }
}

def get_setting(setting)
  SETTINGS[setting]
end

def get_node_setting(node, setting)
  SETTINGS[node][setting]
end

Vagrant.configure(2) do |config|
  config.vm.box = "bento/ubuntu-16.04"
  config.vm.box_check_update = false

  config.vm.synced_folder ".", "/home/vagrant/sync", disabled: true

  ["lab01", "agent01", "agent02", "aytra01"].each do |hostname|
    config.vm.define hostname do |node|
      node.vm.synced_folder "#{get_setting(:mount_path)}", "/opt/projects",
        :nfs => true,
        :nfs_udp => false,
        :mount_options => ['noatime,soft']
      node.bindfs.bind_folder "/opt/projects", "/opt/projects", after: :provision
    end
  end

  config.hostmanager.enabled = true
  config.hostmanager.manage_host = true
  config.hostmanager.manage_guest = true
  config.hostmanager.ignore_private_ip = false
  config.hostmanager.include_offline = true

  config.vm.provision :shell, path: get_setting(:provision_script), args: "#{get_setting(:registry_url)} #{get_setting(:dns_server)}"

  config.vm.define "lab01" do |lab01|
    lab01.vm.provider "virtualbox" do |vb|
      vb.memory = get_node_setting(:lab01, :memory)
      vb.cpus = get_node_setting(:lab01, :cpus)
    end
    lab01.vm.network :private_network, ip: get_node_setting(:lab01, :ip), netmask: "255.255.255.0"
    lab01.vm.hostname = get_node_setting(:lab01, :hostname)
    lab01.hostmanager.aliases = %w(lab01 rancher registry dns)
  end

  config.vm.define "agent01" do |agent01|
    agent01.vm.provider "virtualbox" do |vb|
      vb.memory = get_node_setting(:agent01, :memory)
      vb.cpus = get_node_setting(:agent01, :cpus)
    end
    agent01.vm.network :private_network, ip: get_node_setting(:agent01, :ip), netmask: "255.255.255.0"
    agent01.vm.hostname = get_node_setting(:agent01, :hostname)
    agent01.hostmanager.aliases = %w(agent01 nexus nexus.lab.local)
  end

  config.vm.define "agent02" do |agent02|
    agent02.vm.provider "virtualbox" do |vb|
      vb.memory = get_node_setting(:agent02, :memory)
      vb.cpus = get_node_setting(:agent02, :cpus)
    end
    agent02.vm.network :private_network, ip: get_node_setting(:agent02, :ip), netmask: "255.255.255.0"
    agent02.vm.hostname = get_node_setting(:agent02, :hostname)
    agent02.hostmanager.aliases = %w(agent02)
  end

  config.vm.define "nfs01" do |nfs01|
    nfs01.vm.provider "virtualbox" do |vb|
      vb.memory = get_node_setting(:nfs01, :memory)
      vb.cpus = get_node_setting(:nfs01, :cpus)
    end
    nfs01.vm.network :private_network, ip: get_node_setting(:nfs01, :ip), netmask: "255.255.255.0"
    nfs01.vm.hostname = get_node_setting(:nfs01, :hostname)
    nfs01.hostmanager.aliases = %w(nfs01)
  end

  config.vm.define "aytra01" do |aytra01|
    aytra01.vm.provider "virtualbox" do |vb|
      vb.memory = get_node_setting(:aytra01, :memory)
      vb.cpus = get_node_setting(:aytra01, :cpus)
    end
    aytra01.vm.network :private_network, ip: get_node_setting(:aytra01, :ip), netmask: "255.255.255.0"
    aytra01.vm.hostname = get_node_setting(:aytra01, :hostname)
    aytra01.hostmanager.aliases = %w(aytra01)
    aytra01.vm.synced_folder "#{get_node_setting(:aytra01, :mount_path)}", "/opt/aytra",
      :nfs => true,
      :nfs_udp => false,
      :mount_options => ['noatime,soft']
    aytra01.bindfs.bind_folder "/opt/aytra", "/opt/aytra", after: :provision

    config.vm.provision :shell, path: get_node_setting(:aytra01, :provision_script)
  end

end
CruzanCaramele commented 7 years ago

i have the same problem as well, Here is my Vagrantfile:

`VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

Hostmanager configuration

config.hostmanager.enabled = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
config.vm.provision :hostmanager

# Sycnhed folder path
config.vm.synced_folder ".", "/vagrant", type: "virtualbox"

# boxes
config.vm.define "puppet_server" do |puppet_server|
    # Puppet Server
    puppet_server.vm.box = "centos/7"
    puppet_server.vm.hostname = "puppet.server.com"
    puppet_server.vm.network :private_network, ip: "10.0.0.10"
    puppet_server.hostmanager.aliases = %w(server)

    # Memory allocation
    puppet_server.vm.provider "virtualbox" do |vb|
        vb.memory = 512
    end
end

config.vm.define "centos_agent" do |centos_agent|
    # Centos agent server
    centos_agent.vm.box = "centos/7"
    centos_agent.vm.hostname = "centos.agent.com"
    centos_agent.vm.network :private_network, ip: "10.0.0.11"
    centos_agent.hostmanager.aliases = %w(agent1)
    # Memory allocation
    centos_agent.vm.provider "virtualbox" do |vb|
        vb.memory = 512
    end
end

config.vm.define "debian_agent" do |debian_agent|
    # Debian agent server
    debian_agent.vm.box = "ARTACK/debian-jessie"
    debian_agent.vm.hostname = "debian.agent.com"
    debian_agent.vm.network :private_network, ip: "10.0.0.12"
    debian_agent.hostmanager.aliases = %w(agent2)
    # Memory allocation
    debian_agent.vm.provider "virtualbox" do |vb|
        vb.memory = 512
    end
end

end`

bobweston commented 7 years ago

same problem here. I can provision the guest hosts by running vagrant hostmanager. But I would think that this ought to be executed by the vagrantfile itself.

bobweston commented 7 years ago

I have a work around that is working for me. I'll share it here in case it is useful for anyone else.

Install the vagrant-triggers plugin

vagrant plugin install vagrant-triggers

Then, add the bit below to your Vagrantfile:

config.trigger.after :up do
  run "vagrant hostmanager"
end

This runs vagrant hostmanager on your host machine after running vagrant up. It executes after all the other provisioning is complete.

rkuebert commented 1 year ago

Hi,

I know this issue is old, but I happen to have the same behaviour as in the initial post. I am wondering if I am doing something wrong?

I am using Virtualbox as a provider. I have two different Vagrantfiles for one VM each - blue and red.

I can provide minimal working examples for the Vagrantfiles, if that helps.

Expected results

Following this quote from README.md: "When a machine enters or exits the running state , all active machines with the same provider will have their hosts file updated accordingly."

The expected behaviour is as follows:

  1. The /etc/hosts file on the host has entries for blue and red
  2. The /etc/hosts file on blue has entries for blue and red
  3. The /etc/hosts file on red has entries for blue and red

Actual results

  1. The /etc/hosts file on the host has entries for blue and red [OK]
  2. The /etc/hosts file on blue only has an entry for blue
  3. The /etc/hosts file on red only has an entry for red

Running "vagrant hostmanager" after both VMs are up does not change anything.