Puppet function multi-layer Path transformation array

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.