Ask Your Question
1

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 -0500

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

Comments

1

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 -0500 )edit

2 Answers

Sort by » oldest newest most voted
1

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

DarylW gravatar image

The workaround 'module' that I have seen to solve this problem is https://forge.puppet.com/pltraining/d... 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.

Examples

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

dirtree('C:\\windows\\system32\\drivers')
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',
              '/var/lib/puppet/modules']

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.

Examples

 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
0

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

Vlad gravatar image

I have been using this define: https://github.com/ghoneycutt/puppet-...

edit flag offensive delete link more

Comments

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 -0500 )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 -0500 )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 -0500 )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

2 followers

Stats

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

Seen: 86 times

Last updated: Jan 30