Static IP for each VM

56 views
Skip to first unread message

Jim Jim

unread,
Nov 12, 2016, 6:35:25 AM11/12/16
to vagra...@googlegroups.com
Hello Alvero,

Please find below my contents of my "Vagrantfile"

Vagrant.configure("2") do |config|
  config.ssh.insert_key=true
  config.vm.provider "virtualbox" do |vb|
    vb.gui = true
    vb.memory = "2048"
  end
  config.vm.define "lb1" do |lb1|
    lb1.vm.box="centos/7"
    config.vm.network "public_network", ip: "192.168.1.21"
    config.vm.provision "shell", inline: <<-SHELL
    hostnamectl set-hostname loadbalancer
    SHELL
  end
  config.vm.define "web1" do |web1|
    web1.vm.box="centos/7"
    config.vm.network "public_network", ip: "192.168.1.22"
    config.vm.provision "shell", inline: <<-SHELL
    hostnamectl set-hostname web1
    SHELL
  end
  config.vm.define "web2" do |web2|
    web2.vm.box="centos/7"
    config.vm.network "public_network", ip: "192.168.1.23"
    config.vm.provision "shell", inline: <<-SHELL
    hostnamectl set-hostname web2
    SHELL
  end
end

Issue is my web2 machine has three IPs(192.168.1.21,22,23) web1 has two (192.168.21,22) and loadbalancer has one (192.168.1.21)

What I want is a static IP for each machine like below
loadbalancer  192.168.1.21
web1             192.168.1.22
web2             192.168.1.23

How to do that ? My each config loop is getting closed with "end" so I am not sure why each machine is having IP for its own plus previous declared IP of previous machines?

Your guidance would be sincerely appreciated.

Kind Regards,
Jim

Alvaro Miranda Aguilera

unread,
Nov 12, 2016, 7:34:37 AM11/12/16
to vagra...@googlegroups.com

On Sat, Nov 12, 2016 at 12:35 PM, Jim Jim <jiminfote...@gmail.com> wrote:
  config.vm.define "lb1" do |lb1|
    lb1.vm.box="centos/7"
    config.vm.network "public_network", ip: "192.168.1.21"
    config.vm.provision "shell", inline: <<-SHELL
    hostnamectl set-hostname loadbalancer
    SHELL
  end

Hello


You need a change, but I will suggest 2.

This is what you are missing on the IP part. inside the define, don't use config. use the name you defined.

ie.

  config.vm.define "lb1" do |lb1|
    lb1.vm.box="centos/7"
    lb1.vm.network "public_network", ip: "192.168.1.21"
    lb1.vm.provision "shell", inline: <<-SHELL

    hostnamectl set-hostname loadbalancer
    SHELL
  end

2nd change, Vagrant can set the VM name for you

  config.vm.define "lb1" do |lb1|
    lb1.vm.box="centos/7"
    lb1.vm.network "public_network", ip: "192.168.1.21"
    lb1.vm.hostname = "loadbalancer"
  end


--
Alvaro

jiminfote...@gmail.com

unread,
Nov 15, 2016, 8:14:32 AM11/15/16
to Vagrant
Thank you very much Alvero, I tried the command you suggested. It worked for first VM for second one it gave the error below:
==> web1: Setting hostname...
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

# Update sysconfig
sed -i 's/\(HOSTNAME=\).*/\1web1/' /etc/sysconfig/network

# Update DNS
sed -i 's/\(DHCP_HOSTNAME=\).*/\1"web1"/' /etc/sysconfig/network-scripts/ifcfg-*

# Set the hostname - use hostnamectl if available
echo 'web1' > /etc/hostname
if command -v hostnamectl; then
  hostnamectl set-hostname --static 'web1'
  hostnamectl set-hostname --transient 'web1'
else
  hostname -F /etc/hostname
fi

# Remove comments and blank lines from /etc/hosts
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts

# Prepend ourselves to /etc/hosts
grep -w 'web1' /etc/hosts || {
  sed -i'' '1i 127.0.0.1\tweb1\tweb1' /etc/hosts
}

# Restart network
service network restart


Stdout from the command:

/bin/hostnamectl
Restarting network (via systemctl):  [FAILED]


Stderr from the command:

Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.

C:\Users\jim\Virtual-VMs\load_balancer_lab>

Any idea why ?

Kind Regards,
Jim

Alvaro Miranda Aguilera

unread,
Nov 16, 2016, 3:46:54 PM11/16/16
to vagra...@googlegroups.com
please share a gist of your vagrantfile


--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/c1e503dd-c4fe-4dd9-8daa-cd06d50f9e6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Reply all
Reply to author
Forward
0 new messages