简体   繁体   中英

How to pass two or more variables to Define in Puppet

I struck to pass multiple arguments in define.

The following is my code. I would like to pass two array inside the define, But I'm able to pass only one as like the following.

 class test {   
    $path = [$path1,$path2]
    $filename = [$name1,$name2]
    define testscript { $filename: } // Can able to pass one value. 
 }

 define testscript () {
     file {"/etc/init.d/${title}": //Can able to receive the file name.
           ensure  => file,
           content => template('test/test.conf.erb'), 
 }

From my above code, I could retrieve the filename inside the define resource. I also need path to set the value in the template. I`m not able to send / retrieve second argument in template.

Is there any way to improve my code to pass two values ( $path and $filename ) inside define resource ?

Any help is much appreciated.

Is there any way to improve my code to pass the two values ( $path and $filename ) inside define resource ?

Puppet has good documentation, which covers this area well.

To begin, you need to appreciate that a defined type is a resource type, in almost every way analogous to any built-in or extension type. If your defined type accepts parameters, then you bind values to those parameters just as you would in any other resource declaration. For example:

class mymodule::test {   
   mymodule::testscript { $name1: path => $path1 }
   mymodule::testscript { $name2: path => $path2 }
}

define mymodule::testscript ($path) {
  file {"${path}/${title}":
    ensure  => 'file',
    content => template('test/test.conf.erb')
  }
}

Additionally, because defined types are resource types, you should discard the concept of "passing" values as to them as if they were instead functions. That mental model is likely to betray you. In particular, it will certainly give you the wrong expectation about what would happen if you specify an array or a hash as your resource title.

In particular, you need to understand that in any resource declaration, if you give the resource title as an array, then that means a separate resource for each array member, with the array member as that resource's title. In that case, every one of those resources receives the same parameter values, as declared in the body of the declaration. Moreover, resource titles are always strings. Except for one level of arrays, as described above, if you give anything else as a resource title then it will be converted to a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM