replacing mkdir -p

133 views
Skip to first unread message

Mike Power

unread,
Apr 4, 2013, 12:23:54 PM4/4/13
to puppet...@googlegroups.com
Puppet right now requires every element of a path to have an individual file definition.  This makes it had to take an arbitrary path as a parameter.  You are forced to require your client to make the entire path structure for you or instead you use an exec resource and call mkdir -p.  Using an exec resource does not generate an File resources so autorequire does not work.

I didn't like this, I wanted to be able to once specify a path and have puppet do that autorequire as needed.

Something like:
    path {"/blah/blah/blah/and/blah":
    }


In order to make this happen I would have to manually define each file:
    file {"/blah/":
        ensure      => directory,
    }

    file {"/blah/blah/":
        ensure      => directory,
    }

    file {"/blah/blah/blah/":
        ensure      => directory,
    }

    file {"/blah/blah/blah/and/":
        ensure      => directory,
    }

    file {"/blah/blah/blah/and/blah/":
        ensure      => directory,
    }

Of course there is a short hand for this:
    file {["/blah/", "/blah/blah/", "/blah/blah/blah/", "/blah/blah/blah/and/","/blah/blah/blah/and/blah/"]:
        ensure      => directory,
    }

Then it occurred to me I could parse the path and produce the array of elements needed.  Something like:
    $path = "/blah/blah/blah/and/blah"
    $file_list = split($path, $file_separator)
    $paths = inline_template('<% parent = nil %><%=@file_list.collect{ |file| parent.nil? ? parent = "#{@file_separator}":parent = "#{parent}#{file}#{@file_separator}"}.join(@path_separator) %>')
    $path_list = split($paths, $path_separator)
    file{$path_list:
        ensure      => directory,
    }

This works great.... once.  Then you get errors like:
    Error: Duplicate declaration: File[/]

If there anyway to trim down the produced array by removing the resources that already exist?







Mike Power

unread,
Apr 4, 2013, 4:44:14 PM4/4/13
to puppet...@googlegroups.com
Actually I found if I created a resource between path and file called element, I could give it a unique name.  Then inside the body I could check to see if the File is declared, if not I could declare it. 

Luca Gioppo

unread,
Apr 8, 2013, 4:27:11 PM4/8/13
to puppet...@googlegroups.com
Can you post a complete example please?
Thanks
Luca


2013/4/4 Mike Power <dodt...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To post to this group, send email to puppet...@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tony C

unread,
Apr 8, 2013, 5:24:02 PM4/8/13
to puppet...@googlegroups.com
I have the same issue, I basically create an array, that way it cuts it down to one FILE, and not the same thing over and over again.


file { ["/blah", "/blah/blah", "/blah/blah/blah", "/blah/blah/blah/blah", "/blah/blah/blah/blah/blah"]:
ensure => directory,
owner => "blah",
group => "blah",
mode => 0700,

John Warburton

unread,
Apr 8, 2013, 6:18:40 PM4/8/13
to puppet-users
On 5 April 2013 03:23, Mike Power <dodt...@gmail.com> wrote:
Puppet right now requires every element of a path to have an individual file definition.  This makes it had to take an arbitrary path as a parameter.  You are forced to require your client to make the entire path structure for you or instead you use an exec resource and call mkdir -p.  Using an exec resource does not generate an File resources so autorequire does not work.

I didn't like this, I wanted to be able to once specify a path and have puppet do that autorequire as needed.

People have been requesting this feature for over 7 years (http://projects.puppetlabs.com/issues/86). It won't be accepted as there is not enough consensus as how to implement it. Read the very long arguments in the ticket notes for the intricacies

John

Mike Power

unread,
Apr 9, 2013, 12:56:33 PM4/9/13
to puppet...@googlegroups.com
I'll see if I can open source the component I wrote and upload it to puppet forge.  In this way the open source community can continue the debate about what is the best way to do this, while at the same time those who want can use some solution other than mkdir -p

Klavs Klavsen

unread,
Apr 9, 2013, 3:18:47 PM4/9/13
to puppet...@googlegroups.com
pls. post if you upload it to the forge or somewhere else.. would definetely be interested in getting rid of mkdir -p :)
Reply all
Reply to author
Forward
0 new messages