are there iterators and loops in puppet?

后端 未结 4 759
渐次进展
渐次进展 2020-12-23 14:06

When I define(?) a resource e.g. to ensure dir structure, are there any loops available?

Like that:

  for X in [app1,app2] do:
    file { \'/opt/app/         


        
4条回答
  •  旧时难觅i
    2020-12-23 14:48

    As of Puppet 4 (and the "future parser" of late versions of Puppet 3) the Puppet DSL has iteration functions similar in form and function to some of the methods of Ruby arrays and hashes:

    • each - evaluates a block of code (formally, a lambda) for each element of an array or hash
    • filter - applies a lambda to each element of an array or hash and returns an array or hash of those for which the lambda evaluated to true
    • map - applies a lambda to each element of an array or hash, and returns an array of the results
    • reduce - applies a lambda to each element of an array or hash to build up a single result, which it returns

    There is no indexed for loop along the lines of C's or Java's, but you can combine array sectioning with any of the functions above to achieve iteration over a subset of a data structure. There is no indefinite iteration along the lines of a C or Java while loop.

    Of course, you can still use the resource-centric approaches described in other answers, and sometimes one of those is indeed the best available approach. You cannot any longer use Ruby DSL, however; it is removed altogether from Puppet 4. Among the iteration functions, the ability to define custom functions, the ascension of data-centric approaches into favor, and all Puppet's historic standard features, Ruby DSL seems not much missed.

提交回复
热议问题