2015-10-06 88 views
2

如何將參數傳遞給puppet類。 例如,我有這樣的代碼:如何將數組作爲參數傳遞給puppet類

class testlogging { 
    file { '/home/amo/testloggingfile': 
    ensure => 'present', 
    source => 'puppet:///modules/testlogging/testlogging', 
    } 
} 

我想要做的是通過文件名/路徑的陣列作爲參數此類。

回答

2

John Bollinger的回答是推薦的答案。如果你想不用Hiera yaml後端,你可以寫一個define類型

class myclass { 
# Create a define type, that will take $name as the file 
# You provide $src for each file 
define mydefine ($src) { 
    file { $name : 
    ensure => present, 
    source => "puppet:///modules/<mymodule>/$src", 
    } 
    } 

    # Use two arrarys, one for file names and other for source 
    $filearray=["/path/to/file1","/path/to/file2"] 
    $filesrc=["source1","source2"] 

    # Loop over the array 
    each($filearray) | $index, $value | { 
    mydefine{ $value : 
     src => $filesrc[$index], 
    } 
} 
1

你真的應該讓自己熟悉文檔,特別是Language Reference。它的section on classes討論瞭如何定義你的類,以便它接受參數,以及如何指定所需的參數值。

簡言之,然而,在模塊mymodule接受必需的參數的一類myclass的定義採用這種形式:

class mymodule::myclass($param) { 
    # ... 
} 

您通常會通過自動化數據綁定,一個值的那類的參數所結合所有意圖和目的都意味着Hiera。要指定Hiera默認的YAML後端處理數組值,該數據將包含這些方針的東西:

--- 
mymodule::myclass::param: 
    - '/path/to/file1' 
    - '/path/to/file2' 

如何配置木偶Hiera將是這個論壇太寬泛,完整的解釋。