The puppet equivalent of mkdir -p

10,181 views
Skip to first unread message

Jeff

unread,
Oct 8, 2008, 1:30:13 PM10/8/08
to Puppet Users
Hi all,

I'd like to add a several directories and I can't seem to do it with a
single "file" directive.

Say /home/jeff exists and I want to add /home/jeff/src/my/dir/path

I tried:

file { "/home/jeff/src/my/dir/path":
path => "/home/jeff/src/my/dir/path",
mode => 0755,
owner => jeff,
group => jeff,
ensure => directory,
recurse => true,
}

But puppet complains the parent directory doesn't exist. How can I do
the equivalent of

mkdir -p /home/jeff/src/my/dir/path

TIA,
Jeff

Ashley Penney

unread,
Oct 8, 2008, 1:40:49 PM10/8/08
to puppet...@googlegroups.com
You pretty much have to use exec { "mkdir -p /path" }, sorry!

Peter Meier

unread,
Oct 8, 2008, 1:42:45 PM10/8/08
to puppet...@googlegroups.com
Hi

> file { "/home/jeff/src/my/dir/path":
> path => "/home/jeff/src/my/dir/path",
> mode => 0755,
> owner => jeff,
> group => jeff,
> ensure => directory,
> recurse => true,
> }
>
> But puppet complains the parent directory doesn't exist. How can I do
> the equivalent of
>
> mkdir -p /home/jeff/src/my/dir/path

with the file resource only with:

file { [ '/home/jeff/src/', '/home/jeff/src/my/',
'/home/jeff/src/my/dir', "/home/jeff/src/my/dir/path" :
[...]
}

this is due to various reasons. the main reason for that is imho as
puppet has to now about the things its manages. so to which point it
should go back? and what then?

recurse is only true from the managing point on deeper into the
filesystem. (usefull for purging or source synching)

another thing would be:

exec{'mkdir -p /home/jeff/src/my/dir/path':
unless => 'test -d /home/jeff/src/my/dir/path',
}

which is indeed ugly, but quick.

greets pete

Teyo Tyree

unread,
Oct 8, 2008, 1:47:01 PM10/8/08
to puppet...@googlegroups.com
Hey Jeff,


Jeff wrote:
Hi all,

I'd like to add a several directories and I can't seem to do it with a
single "file" directive.

  
You can do this with syntax, but puppet will still be explicitly managing each directory as a separate resource:

$base = "/home/jeff"

file {["$basedir", "$basedir/src", "$basedir/src/my", "$basedir/src/my/dir", "$basedir/src/my/dir/path" ]:  #Just pass the file resource an array of files.

    mode => 0755,
    owner => jeff,
    group => jeff,
    ensure => directory,
    recurse => true
}   
Say /home/jeff exists and I want to add /home/jeff/src/my/dir/path

I tried:

file { "/home/jeff/src/my/dir/path":
  path    => "/home/jeff/src/my/dir/path",
  mode    => 0755,
  owner   => jeff,
  group   => jeff,
  ensure  => directory,
  recurse => true,
}

But puppet complains the parent directory doesn't exist. How can I do
the equivalent of

mkdir -p /home/jeff/src/my/dir/path
  
Puppet will implicitly order the creation of the directories, so it will behave like mkdir -p.  A little more typing, still explicitly managed.
TIA,
Jeff

  


--

Jeff

unread,
Oct 8, 2008, 2:00:07 PM10/8/08
to Puppet Users
Thanks all.

One question: How do I reference the last directory in the array in a
require statement?

If I do this:

file {["$basedir", "$basedir/src", "$basedir/src/my",
"$basedir/src/my/dir", "$basedir/src/my/dir/path" ]:

Can I do this:

require => File["$basedir/src/my/dir/path"]

TIA,
Jeff

Teyo Tyree

unread,
Oct 8, 2008, 2:16:54 PM10/8/08
to puppet...@googlegroups.com
Yup, like I said the array stuff is just syntax.  You are still managing individual file resources.
TIA,
Jeff

  


--
What is now proved was once only imagin'd. -- William Blake

Teyo Tyree ::: http://reductivelabs.com ::: +1.615.275.5066

zoniguana

unread,
Oct 9, 2008, 8:12:06 AM10/9/08
to Puppet Users
You also could do this by having your directory structure on your
fileserver, of src/path/my
Then file { "/home/jeff/src":
mode => 0755,
owner => jeff,
group => jeff,
ensure => directory,
recurse => true;
}

If that directory structure is to be common across multiple users, it
can save you some typing in the long run, if you set up a custom
definition so that you can substitute $name everywhere there is a
jeff... just a thought...

Francois Deppierraz

unread,
Oct 11, 2008, 9:56:39 AM10/11/08
to puppet...@googlegroups.com
Teyo Tyree wrote:

> file {["$basedir", "$basedir/src", "$basedir/src/my",
> "$basedir/src/my/dir", "$basedir/src/my/dir/path" ]: #Just pass the
> file resource an array of files.
> mode => 0755,
> owner => jeff,
> group => jeff,
> ensure => directory,
> recurse => true
> }

Because resources do not have implicit ordering, you might run into
dependency problems with this example.

If you're not lucky enough, Puppet might try to create
"$basedir/src/my/dir/path" before "$basedir".

Peter Meier

unread,
Oct 11, 2008, 11:47:42 AM10/11/08
to puppet...@googlegroups.com
Hi


no. afair puppet manages ordering within fileresources automagically.

greets pete

Aj

unread,
Oct 11, 2008, 7:34:30 PM10/11/08
to puppet...@googlegroups.com
That's correct - if you have parent directories in your manifest,
puppet will have child files/folders autorequire the parent(s).

The file { [ '/foo', '/foo/bar' ]: } syntax is currently the best way
to achieve this, although with a Parser function you could split a
full path into an array of resources to be managed, to cut down on
typing/repetition.

I recall Paul Lathrop was working on upwards recursion a while back..
There's a ticket open with some details in Redmine.

Regards,

AJ

Luke Kanies

unread,
Oct 13, 2008, 11:17:41 AM10/13/08
to puppet...@googlegroups.com

Files will automatically require their parent directories, if you're
managing them, so you don't need to worry about setting up these
relationships.

--
I can't understand why a person will take a year to write a novel when
he can easily buy one for a few dollars. -- Fred Allen
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com

Reply all
Reply to author
Forward
0 new messages