如何将cookbook_file与bash结合使用?

时间:2016-01-05 05:21:58

标签: ruby bash chef chef-recipe chef-solo

我正在使用以下逻辑编写厨师食谱:

if grep -q -i "release 6" /etc/redhat-release  
then       
  upload **file1** using cookbook_file resource  
else if grep -q -i "release 7" /etc/redhat-release  
then    
  upload **file2** using cookbook_file resource  
fi

请让我知道具有上述逻辑的厨师配方会是什么样子? 我可以利用哪些厨师资源?

3 个答案:

答案 0 :(得分:1)

我建议您使用Ohai automatic attributes获取底层操作系统的信息。

#inner {
    height: 75px;
    width: 75px;
    background-color: #547980;
    position: absolute;
    margin-left: 20px;
  left:10px;
}

如果您愿意,也可以使用Ruby # On all Fedora and RedHat based platforms if ['fedora', 'rhel'].include?(node['platform_family']) if node['platform_version'].to_i == 6 cookbook_file '/path/to/file1' do source 'file1' end # ... elsif node['platform_version'].to_i == 7 cookbook_file '/path/to/file2' do source 'file2' end end end 语句:

case

您还可以使用变量保存文件以上传as Vineeth Guna said

case node['platform_family']
# On all Fedora and RedHat based platforms
when 'fedora', 'rhel'

  case node['platform_version'].to_i
  when 6
    cookbook_file '/path/to/file1' do
      source 'file1'
    end
    # ...
  when 7
    cookbook_file '/path/to/file2' do
      source 'file2'
    end
  end

end

有关更多信息和示例,请参阅recipe DSL documentation

答案 1 :(得分:1)

使用cookbook_file资源,您不会上传文件,您可以在本地将其复制,因为它已经与节点上的食谱一起下载(或者可以下载'按需使用,具体取决于您的client.rb配置。

files中的cookbook_file目录允许file_specificity用于此确切的用例,因此在您的上下文中,您的配方将仅包含:

cookbook_file '/path/to/target' do
   source 'my_source_file'
   action :create
end

您的cookbook文件目录将如下所示(当没有其他匹配目录时,将使用默认文件,请参阅上面链接中的完整文档):

cookbook/
├── files
│   └── default
│       └── my_source_file
│   └── redhat_6.4
│       └── my_source_file
│   └── redhat_7.1
│       └── my_source_file

如果你真的只想使用主要版本,那么你可以删除目录结构中的minor并在source属性中使用Ohai属性,如下所示(使用双引号进行变量插值):

cookbook_file '/path/to/target' do
   source "#{node[platform]}-#{node[platform_version][/(\d).\d/,1]}/my_source_file"
   action :create
end

答案 2 :(得分:-1)

由于我们可以在厨师食谱中使用红宝石,我们可以利用它并上传文件,如下面的代码所示

确保您在食谱的档案目录中有file1, file2

file_to_upload = nil
if system("grep -q -i \"release 6\" /etc/redhat-release")
  file_to_upload = <file1_name>
elsif  system("grep -q -i \"release 7\" /etc/redhat-release")
  file_to_upload = <file2_name>
fi

cookbook_file "<directory_where_file_needs_to_be_uploaded>/#{file_to_upload}"
  source file_to_upload
  action :create
end