[英]vagrant manage hosts dhcp
我想每次运行vagrant up
/ vagrant destroy
时,根据项目的目录名和VM的IP地址自动向/ /etc/hosts
文件中添加条目别名/从中删除条目别名。 有一些插件可以执行此操作,但不支持dhcp。
我正在使用machine_name = File.basename(File.expand_path('..', Dir.pwd)) + '.local'
. machine_name = File.basename(File.expand_path('..', Dir.pwd)) + '.local'
。
我缺少的另一个难题是一种在Vagrantfile中获取VM的dhcp分配的IP地址并更新主机上的/ etc / hosts文件的方法。
我的目标是启动虚拟机,而无需在Vagrantfile和/ etc / hosts中编辑其名称和/或IP。
我正在使用Vagrant 1.7.4,VirtualBox 5.x和Puppet 4.2。
有任何想法吗?
我安装了vagrant-hostmanager ,它具有一个非常有用的config.hostmanager.ip_resolver
选项。
将以下代码添加到您的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
归功于: https : //github.com/smdahlen/vagrant-hostmanager/issues/86#issuecomment-107052823
您应该能够做到这一点。
还记得流浪汉文件是红宝石吗? 编写一个设置外壳程序脚本,以设置/ etc / hosts并将其作为arg传递给Vagrant文件中文件的位置(类似于上面的File.exapand)。
这样,当vagrant运行脚本时,它将生成参数,而配置脚本将选择它并使用它。
供参考: https : //docs.vagrantup.com/v2/provisioning/shell.html
您应该能够为此目的使用vagrant-dns ,它看起来可以完成您想要实现的大部分功能(尽管正如Mircea所说,几乎所有操作都可以通过脚本完成)
该文档提供了一个使用专用网络的示例,但您也可以使用以下配置
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
安装了vagrant-dns后(运行vagrant dns --install
),您将能够使用www.test-machine.dev:8080访问您的计算机
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.