Thanks for that additional context, Dirk Heinrichs. That's helpful. I'm hearing that the simple case, copy files as-is from module to target, is achievable today with the File resource and recurse => remote. I'm also hearing that for a more complex, probably less common use case involving templates, it would be useful to have a general capability of inspecting module content dynamically from Puppet DSL code. Given the complexity and non-generality of the second use case, I don't think it would make sense to implement a series of content-type-specific functions to enable this. That is, I don't think extending or modifying find_template, find_file, etc. would be generally beneficial. It could make sense to extend a capability similar to the existing module_directory() function. For example, pairing it with a new module_files() function. A function like that could take a list of module-relative glob paths, and return an array of file paths. E.g.
call: module_files('module1/files/*', 'module2/templates/**/*.epp', 'module3/metadata.json') |
return: [ |
"/etc/puppetlabs/code/environments/production/modules/module1/files/file1", |
"/etc/puppetlabs/code/environments/production/modules/module1/files/file2", |
"/etc/puppetlabs/code/environments/production/modules/module2/templates/tmp1.txt.epp", |
"/etc/puppetlabs/code/environments/production/modules/module2/templates/tmp2.txt.epp", |
"/etc/puppetlabs/code/environments/production/modules/module2/templates/subdir/tmp3.txt.epp", |
"/etc/puppetlabs/code/environments/production/modules/module3/metadata.json", |
] |
If we provide additional functionality for this kind of use case out-of-box, the template-specific use case could be built on top of it using something like:
$module_directory = module_directory('xampl') |
|
module_files('xampl/templates/**/*.epp').each |$template| { |
$relative_path = regsubst($template, "${module_directory}/templates/(.*).epp", '\\1') |
file { "/target/path/${relative_path}": |
ensure => file, |
content => epp($template, { |
template_param_1 => 'value1', |
template_param_2 => 'value2', |
}), |
} |
} |
The technical complexity of a function like this is very low. If this doesn't make it into product for a time, it's probably something you could write yourself. For example:
Puppet::Functions.create_function(:'custom::module_files') do |
dispatch :module_files do |
repeated_param 'String', :patterns |
end |
|
def module_files(*patterns) |
patterns.reduce([]) do |memo, pattern| |
module_name = pattern.split('/').first |
glob = pattern.split('/').drop(1).join('/') |
module_directory = call_function('module_directory', module_name) |
memo + Dir.glob("#{module_directory}/#{glob}").select { |path| File.file?(path) } |
end |
end |
end |
|