Skip to content

Instantly share code, notes, and snippets.

@mitchellh
Created March 28, 2012 21:39
Show Gist options
  • Save mitchellh/2230819 to your computer and use it in GitHub Desktop.
Save mitchellh/2230819 to your computer and use it in GitHub Desktop.
# A method for configuration based on partial convergence in Chef.
#
# Assuming you have knowledge that Chef is two-phased (compilation then convergence),
# then this solves the problem where you need to pass data into a resource where the
# data comes from a place that requires some convergence to have occurred. Most often
# this happens for me in templates.
#
# Below is a general pattern I use when this is necessary (it is rare). Basically, I
# use an OpenStruct to assign some data to it via `ruby_block` (which is processed at
# convergence), and then I use that data in the resource that depends on it. Then in the
# template itself I guard the entire content with an "if $data.value".
#
# Annoying, any better solutions?
$data = OpenStruct.new
ruby_block "get-data" do
block do
$data.value = source_that_requires_convergence
end
end
template "/etc/foo.conf" do
source "foo.conf.erb"
variables :data => $data
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment