Hello. This is Komiya.
As I automate various things in Chef, I think there will be cases where I want to use values or variables that have been changed in the middle. There will come a time when you want to solve a strange phenomenon that does not become a power of power, such as the first time it doesn't work but the second time it works. In such a case, if you can get a price with ohai, you have to use it or make your own ohai plugin, write ruby_block, or call it with :immediately in notifies.
I don't know what you're talking about! If you think so, the link below is written about the order in which chef is executed, so I think it would be a good idea to take a look. The Misconception That Chef Recipes Are Executed From Top to Bottom | Engine Yard Blog JP
This may be unkind, so please refer to the following.
template "/etc/profile.d/rbenv.sh" do
not_if "grep rbenv /etc/profile.d/rbenv.sh"
source "rbenv.sh.erb"
action :create
notifies :create, "ruby_block[initialize_rbenv]", :immediately
end
ruby_block "initialize_rbenv" do
block do
ENV['RBENV_ROOT'] = node[:rbenv][:root]
ENV['PATH'] = "#{node[:rbenv][:root]}/bin:#{node[:rbenv][:root]}/shims:#{node[:ruby_build][:bin_path]}:#{ENV['PATH']}"
end
action :nothing
end
To explain a little, it seems that the processing that assumes modification during the recipe in chef is a trick, and the processing written in ruby is done before the converge, so it seems that the modification in the middle cannot be recognized at that time. The ruby_block resource seems to move at the same time as the converge timing. If you want to run only when a specific resource is being processed, you can specify a timing for the notifies of that resource. In this case, you can specify nothing for the action of the ruby_block resource, set the profile setting in the template resource, and specify :immediately as notifies, and the variable will be reloaded immediately. (If you use notifies if you have separate recipes, it will be difficult to execute them individually with -o.) )
What's nice about the PATH is that the processing by ruby-build in the bash resource that is scheduled to be executed after this will work ^o^♪ properly ♪ from the first recipe application. However, if you do it with ruby-build or chef, it takes time because not only do you place it with package resources, but also make runs, so I personally wanted to leave it to packer or rpm self-made.
You can learn how to reload hostname in Ohai at the link below. I've never made my own Ohai plugin. chef-solo - How to use hostname for ohai - Qiita
I also used the following as a reference. Thank you very much. Tips for writing recipes in chef without knowing Ruby - running resources in the ruby_block of blog chef - Qiita seems useful if you are calling resources in a ruby script.
Thank you for reading.