The problem is that the function is being defined inside a recipe, and can only be used within that recipe. The include_recipe
method ensures that a given recipe is loaded, but it doesn't import anything into the recipe doing the including.
Since your function is being used to declare a Chef resource with some calculated parameters, the closest thing to look at is the Definition (Chef Docs). Definitions look similar to Resources, having a name and a set of optional parameters, but are actually simple macros that are expanded into the recipe when it is compiled.
In your cookbook directory, create definitions/my_iis_website.rb
containing something like:
define :my_iis_website do
iis_site websiteName do
protocol :http
port 80
path "#{node['iis']['docroot']}/#{websiteName}"
host_header "#{websiteName}.test.kermit.a-aws.co.uk"
action [:add,:start]
end
end
Then, replace the loop in your recipe with:
for websiteName in websiteNames do
my_iis_website websiteName
end
If your recipes for each cluster of server would be identical but for the list of sites, you might want to consider storing this data in attributes
or data bags
instead. This helps you to avoid repeating yourself in your recipes, and will also allow you to add sites without updating your cookbook.