[英]vagrant manage hosts dhcp
I want to automatically add/remove an entry alias to/from my /etc/hosts
file based on the project's dirname and VM's IP address every time I run vagrant up
/ vagrant destroy
. 我想每次运行
vagrant up
/ vagrant destroy
时,根据项目的目录名和VM的IP地址自动向/ /etc/hosts
文件中添加条目别名/从中删除条目别名。 There are some plugins doing this but without support for dhcp. 有一些插件可以执行此操作,但不支持dhcp。
I'm building the dirname using machine_name = File.basename(File.expand_path('..', Dir.pwd)) + '.local'
. 我正在使用
machine_name = File.basename(File.expand_path('..', Dir.pwd)) + '.local'
. machine_name = File.basename(File.expand_path('..', Dir.pwd)) + '.local'
。
The other piece of the puzzle that I'm missing is a way to get the VM's dhcp assigned IP address in the Vagrantfile and update /etc/hosts file on the host machine. 我缺少的另一个难题是一种在Vagrantfile中获取VM的dhcp分配的IP地址并更新主机上的/ etc / hosts文件的方法。
My goal is to spin-up a VM without editing it's name and/or IP in the Vagrantfile and /etc/hosts. 我的目标是启动虚拟机,而无需在Vagrantfile和/ etc / hosts中编辑其名称和/或IP。
I'm using Vagrant 1.7.4, VirtualBox 5.x and Puppet 4.2. 我正在使用Vagrant 1.7.4,VirtualBox 5.x和Puppet 4.2。
Any ideas? 有任何想法吗?
I've installed vagrant-hostmanager which has a very helpful config.hostmanager.ip_resolver
option. 我安装了vagrant-hostmanager ,它具有一个非常有用的
config.hostmanager.ip_resolver
选项。
Add the following code to your Vagrantfile
to make sure your /etc/hosts
file is updated when you run the vagrant up
command: 将以下代码添加到您的
Vagrantfile
,以确保在运行vagrant up
命令时更新/etc/hosts
文件:
config.vm.hostname = "add-name-here"
unless Vagrant.has_plugin?('vagrant-hostmanager')
raise 'vagrant-hostmanager is not installed!'
else
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if hostname = (vm.ssh_info && vm.ssh_info[:host])
`vagrant ssh -c "hostname -I"`.split()[1]
end
end
end
Credits to: https://github.com/smdahlen/vagrant-hostmanager/issues/86#issuecomment-107052823 归功于: https : //github.com/smdahlen/vagrant-hostmanager/issues/86#issuecomment-107052823
you should be able to do this. 您应该能够做到这一点。
Remember that the vagrant file is ruby? 还记得流浪汉文件是红宝石吗? write a provisioning shell script that sets up /etc/hosts and pass it as an arg in the Vagrant file the location of the file (sort of like the File.exapand above).
编写一个设置外壳程序脚本,以设置/ etc / hosts并将其作为arg传递给Vagrant文件中文件的位置(类似于上面的File.exapand)。
This way when vagrant runs the script it will generate the parameter and the provision script will pick it up and use it. 这样,当vagrant运行脚本时,它将生成参数,而配置脚本将选择它并使用它。
for reference: https://docs.vagrantup.com/v2/provisioning/shell.html 供参考: https : //docs.vagrantup.com/v2/provisioning/shell.html
You should be able to use vagrant-dns for this purpose, it looks it does most of what you're trying to achieve (although as said by Mircea, almost everything can be done with scripting) 您应该能够为此目的使用vagrant-dns ,它看起来可以完成您想要实现的大部分功能(尽管正如Mircea所说,几乎所有操作都可以通过脚本完成)
The doc gives an example using a private network but you can as well use the following config 该文档提供了一个使用专用网络的示例,但您也可以使用以下配置
config.vm.box = "ubuntu-12.04"
config.dns.tld = "dev"
config.vm.hostname = "test-machine"
config.vm.network "public_network"
config.vm.network "forwarded_port", guest: 80, host: 8080
After vagrant-dns has been installed (run vagrant dns --install
) you will be able to access your machine with www.test-machine.dev:8080 安装了vagrant-dns后(运行
vagrant dns --install
),您将能够使用www.test-machine.dev:8080访问您的计算机
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.