diff --git a/lib/puppet/type/concat_file.rb b/lib/puppet/type/concat_file.rb index ddde4f01..71dabc13 100644 --- a/lib/puppet/type/concat_file.rb +++ b/lib/puppet/type/concat_file.rb @@ -8,20 +8,30 @@ Puppet::Type.newtype(:concat_file) do @doc = <<-DOC @summary - Generates a file with content from fragments sharing a common unique tag. + Generates a file with content from fragments sharing a single or a list of common unique tag(s). @example Concat_fragment <<| tag == 'unique_tag' |>> + Concat_fragment <<| tag == 'other_tag' |>> concat_file { '/tmp/file': tag => 'unique_tag', # Optional. Default to undef path => '/tmp/file', # Optional. If given it overrides the resource name owner => 'root', # Optional. Default to undef group => 'root', # Optional. Default to undef - mode => '0644' # Optional. Default to undef - order => 'numeric' # Optional, Default to 'numeric' + mode => '0644', # Optional. Default to undef + order => 'numeric', # Optional, Default to 'numeric' ensure_newline => false # Optional, Defaults to false } + concat_file { '/tmp/file2': + tag => ['unique_tag', 'other_tag'], + path => '/tmp/file2', + owner => 'root', + group => 'root', + mode => '0644', + order => 'numeric', + ensure_newline => false + } DOC ensurable do @@ -40,7 +50,7 @@ def exists? end newparam(:tag) do - desc 'Required. Specifies a unique tag reference to collect all concat_fragments with the same tag.' + desc 'Specifies a single or a list of unique tag reference(s) to collect all concat_fragments with the same tag(s).' end newparam(:path, namevar: true) do @@ -194,7 +204,7 @@ def fragments next unless resource.is_a?(Puppet::Type.type(:concat_fragment)) if resource[:target] == self[:path] || resource[:target] == title || - (resource[:tag] && resource[:tag] == self[:tag]) + ((resource[:tag] && self[:tag]) && (resource[:tag] == self[:tag] || self[:tag].include?(resource[:tag]))) resource end }.compact diff --git a/lib/puppet/type/concat_fragment.rb b/lib/puppet/type/concat_fragment.rb index 1261ee6d..3fd41d0b 100644 --- a/lib/puppet/type/concat_fragment.rb +++ b/lib/puppet/type/concat_fragment.rb @@ -75,7 +75,7 @@ next unless resource.is_a?(Puppet::Type.type(:concat_file)) resource[:path] == self[:target] || resource.title == self[:target] || - (resource[:tag] && resource[:tag] == self[:tag]) + ((resource[:tag] && self[:tag]) && (resource[:tag] == self[:tag] || resource[:tag].include?(self[:tag]))) end if found.empty? diff --git a/manifests/fragment.pp b/manifests/fragment.pp index c73ec0ce..e438c5a6 100644 --- a/manifests/fragment.pp +++ b/manifests/fragment.pp @@ -16,10 +16,14 @@ # @param target # Specifies the destination file of the fragment. Valid options: a string containing the path or title of the parent concat resource. # +# @param tagging +# Specifies a custom tag to use for the fragment. +# define concat::fragment ( String $target, Optional[Variant[Sensitive[String], String, Deferred]] $content = undef, Optional[Variant[String, Array]] $source = undef, + Optional[String[1]] $tagging = undef, Variant[String, Integer] $order = '10', ) { $resource = 'Concat::Fragment' @@ -34,7 +38,11 @@ fail("${resource}['${title}']: Can't use 'source' and 'content' at the same time.") } - $safe_target_name = regsubst($target, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'GM') + if $tagging { + $safe_target_name = $tagging + } else { + $safe_target_name = regsubst($target, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'GM') + } concat_fragment { $name: target => $target, diff --git a/manifests/init.pp b/manifests/init.pp index ef03f2e4..f8d96968 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -82,6 +82,9 @@ # @param create_empty_file # Specifies whether to create an empty file if no fragments are defined. Defaults to true. # +# @param tagging +# Specifies a custom tag or list of custom tags for gathering the fragments to combine. +# define concat ( Enum['present', 'absent'] $ensure = 'present', Stdlib::Absolutepath $path = $name, @@ -102,7 +105,8 @@ Optional[String] $seluser = undef, Boolean $force = false, Boolean $create_empty_file = true, - Enum['plain', 'yaml', 'json', 'json-array', 'json-pretty', 'json-array-pretty'] $format = 'plain', + Enum['plain', 'yaml', 'json', 'json-array', 'json-pretty', 'json-array-pretty'] $format = 'plain', + Optional[Variant[String[1], Array[String[1], 1]]] $tagging = undef, ) { $safe_name = regsubst($name, '[\\\\/:~\n\s\+\*\(\)@]', '_', 'G') $default_warn_message = "# This file is managed by Puppet. DO NOT EDIT.\n" @@ -122,9 +126,15 @@ } } + if $tagging { + $safe_names = flatten($safe_name, $tagging) + } else { + $safe_names = $safe_name + } + if $ensure == 'present' { concat_file { $name: - tag => $safe_name, + tag => $safe_names, path => $path, owner => $owner, group => $group, @@ -156,7 +166,7 @@ } else { concat_file { $name: ensure => $ensure, - tag => $safe_name, + tag => $safe_names, path => $path, backup => $backup, }