A simple solution to the "deep directory tree" problem

70 views
Skip to first unread message

Amos Shapira

unread,
Apr 26, 2012, 3:55:59 AM4/26/12
to puppet...@googlegroups.com
Hello,

Like many others, I once again had to deal with creation of deep directory trees with puppet. I did the usual:

file { [ "/a", "/a/b/", "/a/b/c"...]:
  ensure => directory
}

And again got sick of it. I still didn't find a good answer for it on the web so here is what I came up with in a moment of despair:

define core::mkdirp($owner = undef, $group = undef) {
  exec { "mkdir -p ${name}":
    creates => $name,
  }

  file { $name:
    ensure => 'directory',
    require => Exec["mkdir -p ${name}"],
    owner => $owner,
    group => $group,
  }
}

Usage:

core::mkdirp { "/a/b/c/d":
  owner => $user,
  group => $group,
}

file { "/a/b/c/d/file.txt":
 require => Core::Mkdirp["/a/b/c/d"],
...
}

So far it works beautifully for me but I want to hear what the community thinks of it and whether there is a nicer solution I'm missing.

Thanks.

--Amos

Christopher Wood

unread,
Apr 26, 2012, 9:46:56 AM4/26/12
to puppet...@googlegroups.com
I hope you have more moments of despair because that's nifty and I'll use it.

You could possibly use ruby's dirname and basename (http://ruby-doc.org/core-1.8.7/File.html) inside an inline template to do the directory/file split for you in a single resource. The parameters for permissions and ownership for file vs directory might get messy, though.
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To view this discussion on the web visit
> [1]https://groups.google.com/d/msg/puppet-users/-/ozFNn2oUGCQJ.
> To post to this group, send email to puppet...@googlegroups.com.
> To unsubscribe from this group, send email to
> puppet-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/puppet-users?hl=en.
>
> References
>
> Visible links
> 1. https://groups.google.com/d/msg/puppet-users/-/ozFNn2oUGCQJ
Reply all
Reply to author
Forward
0 new messages