1. This function is used to split a path of indeterminate length into an array, which can accept two parameters:
1.1 Arg1: A valid absolute path string [not currently checked for data type validity]
1.2 arg2: A valid number to confirm which part is required for puppet creation [[optional] does not make a logical validity judgment, such as passing a number large and the effective level of the path is not reasonable]
2, directly placed in the module corresponding to the Lib/puppet/parser/functions, where the author directly placed in the Stdlib lib/puppet/parser/functions directory
3, the data type is not done and logic error judgment (such as the value range of arg[2])
4. The code and simple use instructions are as follows:
Code:
Module puppet::P arser::functions newfunction (:p ath_to_array, :type => : Rvalue) do |args| res = args[0] array = [] array.push (Args[0]) if args.size == 1 while res != '/' do res = file.dirname (RES) array.push (res) if res != '/' End elsif args.size == 2 e_ count = args[1] - 1 e_count.times do res = file.dirname (RES) array.push (RES) if res != '/' end end return array endend# vim: set ts=2 sw=2 et :
Usage:
1. Vim test.pp
$aaa =['/aaa/bbb/ccc/ddd ', 2] $bbb =path_to_array ($aaa [0]) $CCC =path_to_array ($aaa [0], $aaa [1]) notify{' result_$bbb ': message = [$BBB]}notify{' RESULT_$CCC ': message = [$CCC]}
2, puppet apply TEST.PP
notice:compiled Catalog for puppet.mos.com in environment production in 1.03 Secondsnotice:/AAA/BBB/CCC/DDD/AAA/BBB/CCC /aaa/bbb/aaanotice:/stage[main]/main/notify[result_$bbb]/message:defined ' message ' as '/aaa/bbb/ccc/ddd/aaa/bbb/ Ccc/aaa/bbb/aaa ' Notice:/aaa/bbb/ccc/ddd/aaa/bbb/cccnotice:/stage[main]/main/notify[result_$ccc]/message: Defined ' message ' as '/AAA/BBB/CCC/DDD/AAA/BBB/CCC ' notice:finished catalog run in 0.60 seconds
This article from "Self-reliance" blog, declined reprint!
Puppet function multi-layer Path transformation array