简体   繁体   中英

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 "/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/... ================================================================================

Errno::ENOENT


No such file or directory - /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. Also setting the parsed config to a variable and use it outside of the ruby_block didn't work.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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