简体   繁体   English

Chef-在运行时从git repo读取文件,并在配方中使用解析值

[英]Chef - Read a file from git repo at runtime and use parse value in recipe

I would like to read a file from a checkout git repository to parse a config file and use this data to perform few resources commands. 我想从签出git存储库中读取文件以解析配置文件,并使用此数据执行一些资源命令。

git "/var/repository" do
    action :sync
end

config = JSON.parse(File.read("/var/repository/config.json" ))
config.each do |job, flags|
    #do some resources stuff here
end

This will not work because the file doesn't exist at compile time: 这将不起作用,因为文件在编译时不存在:

================================================================================ Recipe Compile Error in /var/chef/cache/cookbooks/... ================================================================================ ================================================== ============================ // var / chef / cache / cookbooks /中的配方编译错误... ==== ================================================== =========================

Errno::ENOENT Errno :: ENOENT


No such file or directory - /var/repository/config.json 没有这样的文件或目录-/var/repository/config.json

I where trying to load the file in ruby_block and perform the Chef resource actions there, but this didn't worked. 我试图在ruby_block中加载文件并在那里执行Chef资源操作,但这没有用。 Also setting the parsed config to a variable and use it outside of the ruby_block didn't work. 还将解析的配置设置为变量,并在ruby_block之外使用它不起作用。

ruby_block "load config" do
    block do
        config = JSON.parse(File.read("/var/repository/config.json"))
        #node["config"] = config doesn't work - node["config"] will not be set

        config.each do |job, flags|
            #do some stuff - will not work because Chef context is missing
        end
    end
end

Any idea how I could read the file at runtime and used the parsed values in my recipe? 知道如何在运行时读取文件并在配方中使用解析后的值吗?

You may also find it helpful to use lazy evaluation in scenarios like this. 您可能还会发现在这种情况下使用惰性评估会有所帮助。

In some cases, the value for an attribute cannot be known until the execution phase of a chef-client run. 在某些情况下,直到厨师客户端运行的执行阶段才能知道属性的值。 In this situation, using lazy evaluation of attribute values can be helpful. 在这种情况下,使用属性值的惰性评估可能会有所帮助。 Instead of an attribute being assigned a value, it may instead be assigned a code block. 可以为属性分配代码块,而不是为属性分配值。

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

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