简体   繁体   English

使用Dir和File Ruby类在Vagrant VM上使用Chef创建多个配置文件

[英]Creating multiple config files with chef on a Vagrant VM using Dir and File Ruby classes

So I am creating a VM with vagrant and provision it with chef-client. 因此,我正在使用流浪汉创建一个VM,并为它提供Chef-Client。 On a certain step it should look through a directory and create files from .erb templates (around 50 of them). 在某个步骤中,它应该浏览目录并从.erb模板(其中约有50个)创建文件。

This is the recipe: 这是食谱:

puts("Configuring app...")
Dir.glob('/opt/app/conf/**/*.erb').each do |conf_file|
  conf_file_name = File.basename(conf_file, ".erb")
  conf_file_loc = File.dirname(conf_file)
    template "#{conf_file_loc}/#{conf_file_name}" do
      local true
      source "#{conf_file}"
      action :create
  end
end

So what it does is look for each .erb file in conf directory and in it's child directories, cut of .erb from their name and create a file based on the template. 因此,它的工作是在conf目录及其子目录中查找每个.erb文件,从它们的名称中剪切.erb并根据模板创建一个文件。

This code works, I've run it on a manually created VirtualBox VM and had no problem, but when using chef-client provisioning on the Vagrant generated VM this recipe seems to just get skipped. 这段代码有效,我已经在手动创建的VirtualBox VM上运行了它,没有问题,但是在Vagrant生成的VM上使用Chef-Client Provisioning时,这个方法似乎只是被跳过了。 No error messages, no warnings, no logs - nothing. 没有错误消息,没有警告,没有日志-什么都没有。 All there is are untouched templates in /opt/app/conf. 在/ opt / app / conf中所有都没有修改过的模板。 I can't even see "Configuring app..." string in vagrant output. 我什至看不到流浪者输出中的“ Configuring app ...”字符串。

I can't use chef-solo provisioning since the same recipe is used for generating both QA environment on our cloud server and a local environment for the devs and I want all of them to be provisioned by our chef server. 我不能使用Chef-solo置备,因为使用相同的配方在我们的云服务器上生成QA环境,在开发人员中生成本地环境,所以我希望所有这些都由我们的Chef服务器置备。

Is there any way I could fix it or at least some kind of a logging feature that is not debug level logging for chef? 有什么办法可以修复它,或者至少有某种日志功能不是Chef的调试级别日志记录?

Vagrant and VBox are on Windows7 machine and I am using chef/centos6.5 box. Vagrant和VBox在Windows7机器上,我正在使用Chef / centos6.5机器。

I think you're hit by the compile/converge problem. 我认为您受到编译/收敛问题的困扰。 See http://docs.getchef.com/essentials_nodes_chef_run.html 参见http://docs.getchef.com/essentials_nodes_chef_run.html

But maybe you're just no having the recipe in your runlist. 但是也许您只是没有配方在运行列表中。

If its the first case here is an explanation: 如果第一种情况是解释:

Your Dir.Glob() is done at compile time, at this time your remote_directory (or whatever) creating the templates has not run and the dir is empty. 您的Dir.Glob()在编译时完成,此时创建模板的remote_directory (或其他任何文件)尚未运行,并且目录为空。

Easy solution: two run of chef, the second would find the files and create the template resources for each. 简单的解决方案:两次运行Chef,第二次将查找文件并为每个文件创建template资源。

Another option would be to encapsulate your code inside a ruby_block so your code will be evaluated at converge time and should work. 另一个选择是将您的代码封装在ruby_block这样您的代码将在融合时进行评估并且可以正常工作。

Tensibai was spot on the problem - putting the recipe inside a ruby_block solved the issue. Tensibai恰好解决了这个问题-将配方放入ruby_block即可解决问题。

Here is how I edited the recipe: 这是我编辑食谱的方法:

ruby_block "Create configs for #{node['hostname']}" do
  action :create
  block do
    Dir.glob('opt/app/conf/**/*.erb').each do |conf_file|
      conf_file_name = File.basename(conf_file, ".erb")
      conf_file_loc = File.dirname(conf_file)
        t=Chef::Resource::File::Template.new("#{conf_file_loc}/#{conf_file_name}", run_context)
        t.local true
        t.source conf_file
        t.owner 'user'
        t.group 'root'
        t.run_action :create
    end
  end
end

Thank you very much, you just made at least 40 lazy developers and testers very happy. 非常感谢,您让至少40位懒惰的开发人员和测试人员感到非常高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM