I'm wondering how to solve the following puppet problem: I want to create several files based on an array of strings. The complication is that I want to create multiple directories with the files:


The problem is that the file resource titles must be unique. So if I keep the file names in an array, I need to iterate over the array in a custom way to be able to postfix the file names with the directory name:

$file_names = ['fileA', 'fileB']
$file_names_2 = [$file_names, 'fileC']

file {'dir1': ensure => directory }
file {'dir2': ensure => directory }

file { $file_names: path = 'dir1', ensure =>present, }
file { $file_names_2: path = 'dir2', ensure =>present, }

This wont work because the file resource titles clash. So I need to append e.g. the dir name to the file title, however, this will cause the array of files to be concatenated and not treated as multiple files...


file { "${file_names}-dir1": path = 'dir1', ensure =>present, }
file { "${file_names_2}-dir2": path = 'dir1', ensure =>present, }

How to solve this problem without the necessity of repeating the file resource itself. Thanks


You can use regsubst() function to modify an array of strings and return an array. See Puppet Function Reference.

$filenames = ['fileA', 'fileB', 'fileC']
$filepaths_dir1 = regsubst($filenames, '^', '/path/to/dir1/')

file {$filepaths_dir1:
    ensure => present,

Keep in mind that file resource title needs to be a fully qualified path, or the full path must be specified with path parameter. Specifying path => 'dir1' for the files probably does not do what you want.

  • Thanks for the answer. Concerning the fq paths: I used 'dir1' only as a 'placeholder' for the question. Thanks! – paweloque Oct 19 '12 at 7:42
  • @paweloque: I thought so, but as the path must also include the file name and the resource definition here defines multiple files, path parameter woudn't work with any value. It would be the same value for all files, and I dont' even know what that would do. Perhaps there would be many resource definitions defining the same file. – Mikko Oct 19 '12 at 15:02

It's been a while since I used this but a define should handle what you want.

define myfiles::config ($directory_name, $file_name) {
  ; add command to create directories if needed

  file { "myfiles/conf.d/$directory_name/$file_name":
    ensure  => present,


$directory_name= <from array>
$file_name= <from array>
; loop the following statement 
myfiles::config { "$directory_name-$file_name":
  directory_name => 'dir1',
  file_name   => 'file',
  • Hi, doesn't exactly this 'flatten' the arrays "$directory_name-$file_name"? in the parameter assignment insude myfiles::config you then used the constants 'dir1' and 'file'. What I want to do is to iterate over the values from the arrays. – paweloque Oct 19 '12 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.