Making directory trees equivalent to mkdir -p in puppet

‹ Install ruby gems without root access in your home directory | Version in which .vagrant changed from a file to a directory ›

When you create a directory in Puppet using the file type, you may get this error if you haven't already created all parents of the directory in question first:

err: /Stage[main]//File[/path/to]/ensure: change from absent to directory failed: Cannot create /path/to; parent directory /path does not exist

This puppet module defines a custom function all_parents() which you can use to create a directory and all of its ancestors simultaneously.

Example:
(creates a symbolic link from /some/path/to/a/file pointing at /vagrant)

$path = '/some/path/to/a/file'
$path_parents = all_parents($path)
file { $path_parents:
  ensure => 'directory',
  mode => 775,
}
file { $path:
  ensure => 'link',
  target => '/vagrant',
  require => File[ $path_parents ],
}

Installation

puppet module install wtanaka-mkdir --version 0.0.1

Download

https://forge.puppetlabs.com/wtanaka/mkdir/0.0.1

Source code

https://github.com/wtanaka/puppet-wtanaka-mkdir

Comments

According to the variable you set ($path_parents = ...), this should be :

file { $path_parents:

...

}

instead of

file { $path_to_project_parents:

...

}

Let's fix the example completely to avoid confusing the reader :

    $path = '/some/path/to/a/file'

    $path_parents = all_parents($path)

    file { $path_parents:
      ensure => 'directory',
      mode   => 775,
    }

    file { $path:
      ensure  => 'link',
      target  => '/vagrant',
      require => File[ $path_parents ],
    }

thanks, updated

Subscribe to All Posts - Wesley Tanaka