Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 流氓文件在流氓销毁上的执行_Ruby_Date_Vagrant_Chef Infra - Fatal编程技术网

Ruby 流氓文件在流氓销毁上的执行

Ruby 流氓文件在流氓销毁上的执行,ruby,date,vagrant,chef-infra,Ruby,Date,Vagrant,Chef Infra,每次你打电话给vagrant或vagrant销毁vagrant文件都会被执行 这是因为我有以下问题。在启动时,我使用以下参数配置机器: config.vm.provision "chef_client" do |chef| chef.node_name = 'living-dev-'+Time.new.strftime("%H%M%S") config.vm.hostname = chef.node_name end 当我执行vagrant destroy时,执行以下代码: c

每次你打电话给vagrant或vagrant销毁vagrant文件都会被执行

这是因为我有以下问题。在启动时,我使用以下参数配置机器:

config.vm.provision "chef_client" do |chef|
    chef.node_name = 'living-dev-'+Time.new.strftime("%H%M%S")
    config.vm.hostname = chef.node_name
end
当我执行vagrant destroy时,执行以下代码:

config.trigger.after :destroy do
    info "Attempting to remove node #{config.vm.hostname}"
    run "knife node show #{config.vm.hostname} -c c:/chef/knife.rb"
    if $?.to_i == 0
        info "Removing node #{config.vm.hostname}"
        "knife node delete #{config.vm.hostname} -y -c c:/chef/knife.rb"
    end
end
我在这里获得的config.vm.hostname与在chef上定义的主机名不同,因此我无法删除chef服务器上的vm计算机

如何解决它?

是的,当您执行vagrant命令时,它会读取整个vagrant文件并将其放入内存,以了解如何使用机器。因此,在您的情况下,即使是配置块也会被读取和执行,然后当您重新启动VM时,主机名也会发生变化

我相信您可以找到关于处理动态主机名的一些问题,但基本上您需要保留这些信息,因此您需要在文件中定义主机名后编写主机名,然后从该文件中读取

If File.exist?(".vagrant/machines/default/virtualbox/hostname")
  File.open(".vagrant/machines/default/virtualbox/hostname", "rb")
  hostname = File.read
else 
  hostname = 'living-dev-'+Time.new.strftime("%H%M%S")
  File.open(".vagrant/machines/default/virtualbox/hostname" ,'w') do |f|
    f.write "#{hostname}]"
  end
end

我已将File.exists更改为File.exist?因为此错误:文件类NoMethodError的未定义方法“存在”