recursively make directory tree in puppet without purging

The recurse parameter does not allow you to create parent directories. It is used to enforce property values such as owner, mode etc. on directory contents and subdirectories recursively.

file { '/var/www':
    owner   => 'www-data',
    recurse => true,
}

As a matter of fact, Puppet currently cannot automatically create all parent directories. You should add all relevant directories as resources instead.

file { [ '/var/www/apps',
         '/var/www/apps/example',
         '/var/www/apps/example/current',
         '/var/www/apps/example/current/public', ]:
           ensure => directory,
           ...
}

Existing content will remain unmolested. There is no need to pass the purge parameter.


  exec { "Create ${mydir}":
    creates => $mydir,
    command => "mkdir -p ${mydir}",
    path => $::path
  } -> file { $mydir : }

The last line is so that other resources (e.g., files to create inside $mydir) can depend on File[$mydir] as though it was possible to create it with a plain old file {} block, which it really should.


If you use "define", you can have something like this:

mymodule::recursive_dir { "My Directory" :
   drive => "C:",
   path  => "/path/to/folder",
}

Where I define the "define" in mymodule.rb:

define mymodule::recursive_dir ($drive, $path) {
  $folders = split($path, "/")
  $folders.each |$index, $folder| {
    $calculated_folder = inline_template("<%= @folders[0, @index + 1].join('/') %>")
    $full_path = "${drive}${calculated_folder}"
    if (! defined(File[$full_path]) and $full_path != $drive) {
      file { $full_path :
        ensure => directory,
      }
    }
  }
}

This splits apart the path and creates each directory as it puts the path back together, making sure not to try to create the drive itself.

Tags:

Puppet