Ask Your Question

How do I use the type file to create diectories like a mkdir -p without having to code all levels

asked 2017-01-27 14:06:02 -0600

jrbast gravatar image

With file type, you can do an ensure => directory, but I want to automate the creation of all lower level directories, line mkdir -p does to create /opt/level1/level2/level3. or how do I build a list of directory entries to create based on the full final directory I want

The only way I know how in puppet is : file { '[/opt", '/opt/level1','/opt/level1/level2']: ensure => directory, }

edit retag flag offensive close merge delete



You can't. The `file` resource complies with the PP philosophy describing a state _explicitly_. Probably, there's already a custom type, which takes a string, explodes it at each slash, and creates `file` resources for each directory using a loop. But still, it'd be explicit declaration of the dirs.

Kai Burghardt gravatar imageKai Burghardt ( 2017-01-28 04:41:53 -0600 )edit

If you're describing a state _explicitly_, then should not PP make that state happen as _described_? i.e. "I have a blank /tmp. I want to create /tmp/why/do/I/need/parent/dirs. That directory should be recursively created by default, or there should be a built-in mechanism to make that happen."

wraukon gravatar imagewraukon ( 2018-03-07 19:32:05 -0600 )edit

2 Answers

Sort by » oldest newest most voted

answered 2017-01-30 09:14:41 -0600

DarylW gravatar image

The workaround 'module' that I have seen to solve this problem is The below text is copied from it's readme

dirtree This module provides the dirtree function and resource type, both used for recursive directory management.


Will return: ['/usr', '/usr/share', '/usr/share/puppet']

Will return: ["C:\\windows", "C:\\windows\\system32", "C:\\windows\\system32\\drivers"]

dirtree(['/usr/share/puppet', '/var/lib/puppet/ssl', '/var/lib/puppet/modules'])
Will return: ['/usr', '/usr/share', '/usr/share/puppet',
              '/var', '/var/lib', '/var/lib/puppet', '/var/lib/puppet/ssl',

dirtree('/usr/share/puppet', '/usr')
Will return: ['/usr/share', '/usr/share/puppet']

dirtree('/usr/share', '/usr/share/puppet')
Will return: []

dirtree('C:\\windows\\system32\\drivers', 'C:\\windows')
Will return: ['C:\\windows\\system32', 'C:\\windows\\system32\\drivers']

You can use the dirtree function in a class to enumerate all required directories if needed.

 class dirtree {
  # rubysitedir = /usr/lib/ruby/site_ruby/1.8
  $dirtree = dirtree("/var/lib/puppet/ssl", '/var/lib')

  # $dirtree = ['/usr/lib/puppet', '/var/lib/puppet/ssl']
  ensure_resource('file', $dirtree, {'ensure' => 'directory'})

dirtree resource type This resource type will simply ensure the existence of a directory. It cannot and will not manage ownership or permissions. You should use the file resource type for that. It's simply for use in the edge case in which you must use a directory which you cannot fully manage, for one reason or another.


 dirtree { 'a temp dir':
  ensure  => present,
  path    => '/tmp/foo/bar/baz',
  parents => true,

dirtree { 'another temp dir with the same path':
  ensure  => present,
  path    => '/tmp/foo/bar/baz',

file { '/tmp/foo/bar/baz':
  ensure => directory,
  owner  => 'root',
  group  => 'root',
  mode   => '0755',
edit flag offensive delete link more

answered 2017-01-28 07:49:18 -0600

Vlad gravatar image

I have been using this define:

edit flag offensive delete link more


Well, I cherish having proper log messages tellin the specific changes. Outright calling mkdir -p doesn't tell what directories have been created and doesn't change the file mode. Also the autorequire feature of the file resource doesn't work (automatic require => File[…] to any managed parent dirs.

Kai Burghardt gravatar imageKai Burghardt ( 2017-01-28 14:34:23 -0600 )edit

There is also a custom type out there that you give it a path, and it will automatically produce an array of directory elements and ensure each one is present. I forget what it is called :/

DarylW gravatar imageDarylW ( 2017-01-29 20:46:59 -0600 )edit

DarylW, If you come across the custom type for automatically producing an array of directory elements, please apps it on... I will try to find it as well. otherwise, an exec with mkdir -p is my work-around for now

jrbast gravatar imagejrbast ( 2017-01-29 21:03:47 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2017-01-27 14:06:02 -0600

Seen: 1,797 times

Last updated: Jan 30 '17