4

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
  • 4,926
  • 6
  • 31
  • 34
  • 1
    I found you had to create each layer explicitly, and I assume it's a matter of principle on the part of the designers. But I'm keen to learn otherwise. – slim Feb 19 '14 at 14:37
  • http://stackoverflow.com/questions/6399922/are-there-iterators-and-loops-in-puppet shows the solution but I thought could find a way to iterate over $i somehow – DrFalk3n Feb 19 '14 at 14:39

3 Answers3

4

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.

Mateusz M.
  • 382
  • 7
  • 17
2
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" :)

drAlberT
  • 22,059
  • 5
  • 34
  • 40
  • 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 – DrFalk3n Feb 20 '14 at 08:41
  • 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 – drAlberT Feb 20 '14 at 09:04
  • 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. – DrFalk3n Feb 20 '14 at 12:34
  • 1
    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, http://stackoverflow.com/questions/12958114/how-to-iterate-over-an-array-in-puppet just confirm my approach afaics – drAlberT Feb 20 '14 at 15:16
  • 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! – DrFalk3n Feb 20 '14 at 16:22
0

so far what I found, and it works, is coming from this other SO Question, I can create and be sure a file is present in each directory, in this way, but I hoped to find a way for a loop over [some-1,some-2,some-3,some-4]

file{[ "/some-1/pool" , "/some-2/pool" , "/some-3/pool" , "/some-4/pool" ] :
    ensure   => "directory",
    recurse  => "true",
     [ "/some-1/pool/setup" , "/some-2/pool/setup" , "/some-3/pool/setup" , "/some-4/pool/setup" ] :
    ensure   => "present",
    content  => template("some/setupPool.erb");
}
Community
  • 1
  • 1
DrFalk3n
  • 4,926
  • 6
  • 31
  • 34