puppet recursive directories creation

11,704

Solution 1

define create_pool {
  file { "/some-$title":
    ensure => "directory"
  }
  file { "/some-$title/pool":
    ensure => "directory"
  }
}

create_pool { ["a", "b", "c", "d"]: }

a define automagically loops over its "arguments" :)

Solution 2

You can first create an array of directories you need either manually or:

$directories = split('/some/path/to/somewhere', '/')

and then create them in a loop:

each($directories) |$directory| {
  if ! defined (File[$directory]) {
    file { $directory: ensure => directory }
  }
}

I'm using if not defined to make sure I don't have conflicts between different modules/classes that reuse parts of the path.

Edit: You might be required to add parser=future to your puppet.conf for the loop to work.

Share:
11,704
DrFalk3n
Author by

DrFalk3n

Physics Degree at "Tor Vergata" University Roma Italy. System Engineer for Grid Computing on distributed data sharing over virtual file systems (DCache) for LHC Atlas experiment. Provide a system for storing and retrieving huge amounts of data, distributed among a large number of heterogenous nodes, under a single virtual filesystem tree with a variety of standard access methods.

Updated on June 13, 2022

Comments

  • DrFalk3n
    DrFalk3n almost 2 years

    I am trying to create recursively directories with same structure:

    I have the followig dirs:

    /some-1 /some-2 /some-3 /some-4

    and inside each of them I'd like to create the same structure, let name it pool:

    /some-1/pool /some-2/pool /some-3/pool /some-4/pool

    as suggested by Albert an elegant solution could be via the "define" method of puppet.

    define create_pool {
    file { "/some-$title/pool":
        ensure => "directory",
        recurse => "true",
     }
    }   
    
    create_pool { [1,2,3,4]: }
    

    Fortunately this solution is "looping" through the list:

  • DrFalk3n
    DrFalk3n about 10 years
    As stated, passing such a list won't work, simply 'cause will create the wrong tree: Create_pool[3]/File[/some-1234/pool]/ensure: change from absent to directory failed: Cannot create /some-1234/pool; parent directory /some-1234 does not exists. What I need is /some-1/pool, /some-2/pool, /some-3/pool, /some-4/pool
  • drAlberT
    drAlberT about 10 years
    This is a simple and obvious change to a sample code .. it suffice to add the creation of the parent directory first .. in the same define .. the question was the loop, not the creation of files or dirs .. or am I missing something? Anyway I edited to create the parent dir before the child, tnx
  • DrFalk3n
    DrFalk3n about 10 years
    No doubt by the fact that your solution is elegant ( ;) as usual) but did you had time to give it a try!? I'll edit the question including your proposal that unfortunately is not "looping" too. What it tries to create is something like /some-1234/pool and not just 4 different dirs. That's why the question exist, indeed not that obvious, I suspect the task is a bit out of scope/philosophy for puppet.
  • drAlberT
    drAlberT about 10 years
    I don't have a way to try now, but it should work .. are u sure the problem is not the use of numbers? try using 4 strings create_pool { ["one", "two", "three", "four"]: } BTW, stackoverflow.com/questions/12958114/… just confirm my approach afaics
  • DrFalk3n
    DrFalk3n about 10 years
    Well numbers where coming from a tertiary application and not validated! So thnks to your last comment I fixed the issue...what else... Ad majora! Bis nächstes Mal!