trethaller
trethaller

Reputation: 587

Setting the ip of a RHEL box from a Vagrant script

I have a RHEL image already preconfigured, I don't know how it was originally setup. By default, it is configured with a local network interface on the ip 192.168.50.50. What I am trying to do is configure its ip from the Vagrant script.

This doesn't seem to do anything:

config.vm.network "private_network", ip: "192.168.50.10"

This does change the ip:

sudo nmcli con mod bond0 ipv4.addresses 192.168.50.10/24
service network restart

But after that apparently Vagrant doesn't automatically detect the ip to connect to, so I need to add:

config.ssh.host = LOCAL_IP

But here's the problem: on the first time, the ip is the default one (.50.50). So I can't already set config.ssh.host to my desired ip. If I omit the config.ssh.host line, it runs the first time but not after, and vagrant ssh fails as well.

Is there a way to set the box ip without editing the Vagrant script between the first and second vagrant up?


Edit: Result of vagrant up --debug command: http://pastebin.com/BTccc4NT


Edit: The problem was that the Vagrant file from the default box (on Windows, it's at C:\Users\user\.vagrant.d\boxes\nameofbox\virtualbox\Vagrantfile) itself had this line:

config.vm.network "private_network", ip: "192.168.50.50", auto_config: false

Upvotes: 0

Views: 532

Answers (1)

Frederic Henri
Frederic Henri

Reputation: 53703

hum, its weird, it creates 2 interfaces

DEBUG network: Normalized configuration: {:adapter_ip=>"192.168.50.1", :auto_config=>false, :ip=>"192.168.50.50", :mac=>nil, :name=>nil, :netmask=>"255.255.255.0", :nic_type=>nil, :type=>:static, :adapter=>2}
 INFO network: Searching for matching hostonly network: 192.168.50.50
 INFO subprocess: Starting process: ["C:/Program Files/Oracle/VirtualBox/VBoxManage.exe", "list", "hostonlyifs"]

......

DEBUG network: Normalized configuration: {:adapter_ip=>"192.168.50.1", :auto_config=>true, :ip=>"192.168.50.10", :mac=>nil, :name=>nil, :netmask=>"255.255.255.0", :nic_type=>nil, :type=>:static, :adapter=>3}
 INFO network: Searching for matching hostonly network: 192.168.50.10
 INFO subprocess: Starting process: ["C:/Program Files/Oracle/VirtualBox/VBoxManage.exe", "list", "hostonlyifs"]

so on on adapter2 you have 192.168.50.50 and on adapter3 you have 192.168.50.10

The possible reason for this is that the box you're using has a specific Vagrantfile which defines already a network on the static address.

I am not fully familiar with windows but on mac, the box definition is under ~/.vagrant/boxes/<yourbox>/<theprovider>/Vagrantfile (note this is not the Vagrantfile from your project, this is really a Vagrantfile which will be applied to any VM built from this box); check the file and remove the network configuration if you see it

As documented the Vagrantfile are merged from the different locations

At each level, settings set will be merged with previous values. What this exactly means depends on the setting. For most settings, this means that the newer setting overrides the older one. However, for things such as defining networks, the networks are actually appended to each other. By default, you should assume that settings will override each other. If the behavior is different, it will be noted in the relevant documentation section.

so by default Vagrant will create additional network interface and will not replace the one coming from the box Vagrantfile.

Upvotes: 1

Related Questions