How to pass dhcp details of my local network in my vagrant file

419 views
Skip to first unread message

Sunil Gajula

unread,
Jan 16, 2019, 9:07:08 AM1/16/19
to Vagrant
I have a vagrant file, where the IP of my virtual box is hardcoded. 

    # The IP address of the first server
    primary_ip = "172.17.8.101"

I wanted to dynamically assign the ip based on my local network (corporate network). 

In vagrant documentation, I see we can use

The easiest way to use a private network is to allow the IP to be assigned via DHCP.

    Vagrant.configure("2") do |config|
      config.vm.network "private_network", type: "dhcp"
    end


In the same vagrant file, during the configuration I see that the hardcoded ip is referred. Now since, I am using the 'type: dhcp', how can I pass the IP details for the below configuration?

    host.vm.provision :shell, inline: %Q|echo 'export ETCD_AUTHORITY="#{primary_ip}:2379"' >> /home/vagrant/.profile|

Alvaro Miranda Aguilera

unread,
Jan 16, 2019, 10:18:45 AM1/16/19
to vagra...@googlegroups.com
Hello.

private_network will create a interface without access to the real network.

If you want to create a interface that have access to the real network, then public_network is what you may need.

You can find more information here:

Alvaro.



--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/c7a3a705-123d-48be-83da-4eca34ada5c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Alvaro

Sunil Gajula

unread,
Jan 17, 2019, 12:53:53 AM1/17/19
to Vagrant
Hello Alvaro,

Thanks for your help.

Below is my vagrant file, I will have to pass the ip details for my etcd configuration. I do not want to hardcode the IP, hence using 

config.vm.network "public_network",
    use_dhcp_assigned_default_route: true

Could you let me know how can I get the ip details to pass to etcd configuration?

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

# The calicoctl download URL.

# The version of the calico docker images to install.  This is used to pre-load
# the calico/node and calico/node-libnetwork images which slows down the
# install process, but speeds up the tutorial.
#
# This version should match the version required by calicoctl installed from
# calicoctl_url.
calico_node_ver = "v0.22.0"
calico_libnetwork_ver = "v0.9.0"

# Size of the cluster created by Vagrant
num_instances=2

# Change basename of the VM
instance_name_prefix="calico"

Vagrant.configure(2) do |config|
  # always use Vagrants insecure key
  config.ssh.insert_key = true
  config.ssh.username = "vagrant"
  config.vm.network "public_network",
    use_dhcp_assigned_default_route: true


  # Use Bento Ubuntu 16.04 box (officially-recommended box by Vagrant)
  config.vm.box = "bento/ubuntu-16.04"

  # Workaround 16.04 issue with Virtualbox where Box waits 5 minutes to start
  # if network "cable" is not connected: https://github.com/chef/bento/issues/682
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--cableconnected1", "on"]
  end

  # Set up each box
  (1..num_instances).each do |i|
    vm_name = "%s-%02d" % [instance_name_prefix, i]
    config.vm.define vm_name do |host|
      host.vm.hostname = vm_name

      config.vm.network "public_network",
          use_dhcp_assigned_default_route: true
      # Fix stdin: is not a tty error (http://foo-o-rama.com/vagrant--stdin-is-not-a-tty--fix.html)
      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

      # The docker provisioner installs docker.
      host.vm.provision :docker, images: [
          "busybox:latest",
          "calico/node-libnetwork:#{calico_libnetwork_ver}",
          "calico/node:#{calico_node_ver}"
      ]

      # Calico uses etcd for calico and docker clustering. Install it on the first host only.
      if i == 1
        # Download etcd and start.
        host.vm.provision :shell, inline: <<-SHELL
          # sudo apt-get install -y unzip
          curl -L --silent https://github.com/coreos/etcd/releases/download/v2.2.0/etcd-v2.2.0-linux-amd64.tar.gz -o etcd-v2.2.0-linux-amd64.tar.gz
          tar xzvf etcd-v2.2.0-linux-amd64.tar.gz
          nohup etcd-v2.2.0-linux-amd64/etcd --addr=#{primary_ip}:2379 > etcd.log &
        SHELL
      end

      # Set Docker to use etcd for multihost, then reload systemctl and restart Docker.
      host.vm.provision :shell, inline: "mkdir -p /etc/systemd/system/docker.service.d/"
      host.vm.provision :shell, inline: %Q|sudo sh -c 'printf "[Service]\nExecStart=\nExecStart=/usr/bin/dockerd -H fd:// --cluster-store=etcd://#{primary_ip}:2379" > /etc/systemd/system/docker.service.d/10-execstart.conf'|
      host.vm.provision :shell, inline: "systemctl daemon-reload"
      host.vm.provision :shell, inline: "systemctl restart docker.service"

    end
  end
end

Regards,
Sunil

Alvaro Miranda Aguilera

unread,
Jan 17, 2019, 1:01:53 PM1/17/19
to vagra...@googlegroups.com
hello

doesn't seems a Vagrant question, looks like more a linux question

you can check the ip with

ip addr show

or 

ifconfig -a

then some shell script could get you to the ip

Alvaro.


For more options, visit https://groups.google.com/d/optout.


--
Alvaro

Reply all
Reply to author
Forward
0 new messages