Skip to content

Instantly share code, notes, and snippets.

@christinedraper
Last active May 19, 2022 12:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christinedraper/334b45edb8e52f4e9dfe to your computer and use it in GitHub Desktop.
Save christinedraper/334b45edb8e52f4e9dfe to your computer and use it in GitHub Desktop.
Chef derived attribute
default["app"]["name"] = "app"
# Attribute to show the problem
default["app"]["install_dir"] = "/home/#{node["app"]["name"]}"
# Attribute used in delayed evaluation approach
default["app"]["install_dir2"] = "/home/%{name}"
# Attribute used in conditional assignment approach
# default["app"]["install_dir3"] = "/home/#{node["app"]["name"]}"
#
# Cookbook Name::app
# Recipe:: conditional
#
install_dir = node['app']['install_dir3'] || "/var/#{node['app']['name']}"
# Alternative to line above that will set the node attribute
#node.default['app']['install_dir3'] = "/var/#{node['app']['name']}" unless node['app']['install_dir3']
#install_dir = node['app']['install_dir3']
ruby_block "Executing resource in recipe" do
block do
Log.info "Executing recipe, application name is: #{node['app']['name'] };" +
" install_dir is #{install_dir}"
end
end
#
# Cookbook Name:: app
# Recipe:: default
#
ruby_block "Executing resource in recipe" do
block do
Log.info "Executing recipe, application name is: #{node['app']['name'] };" +
" install_dir is #{node['app']['install_dir']}"
end
end
#
# Cookbook Name:: app
# Recipe:: delayed
#
install_dir = node['app']['install_dir2'] % { name: node['app']['name'] }
ruby_block "Executing resource in recipe" do
block do
Log.info "Executing recipe, application name is: #{node['app']['name'] };" +
" install_dir is #{install_dir}"
end
end
default["app"]["name"] = "my_app"
# Uncomment these to test that the install_dir can be overridden
# default["app"]["install_dir"] = "/tmp"
# default["app"]["install_dir2"] = "/tmp"
# default["app"]["install_dir3"] = "/tmp"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment