在Puppet中定义函数时收到错误。 C:\Windows\system32> puppet agent --test --server davidhromyk.test.5.puppet.lrscorp.net --masterport 8937 --ssldir C:\Temp\davidhromyk.test.5.puppet.lrscorp.net
Info: Using configured environment 'internal'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Retrieving locales
Info: Loading facts
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Unknown function: 'driver_name'. (file: /home/david.hromyk/.puppetlabs/etc/code/environments/internal/modules/windows_print_server/manifests/init.pp, line: 12, column: 41) on node ops-dev-print01.lrscorp.net
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
这是我的模块配置。
class windows_print_server (
$printers,
){
exec { 'Install-PrintServices':
command => 'Install-WindowsFeature -Name @("print-services","Print-LPD-Service")',
unless => '& { exit ((Get-WindowsFeature -Name Print-Services).installed -eq $false) }',
provider => powershell,
}
$printers.each |Integer $index, Hash $printer| {
exec { "add printer driver ${printer.driver_name} for ${printer.name}":
command => "Add-PrinterDriver -Name ${printer.driver_name}",
unless => "Get-PrinterDriver -Name ${printer.driver_name}",
}
exec { "add printer port $printer.port_name for ${printer.name}":
command => "Add-PrinterPort -Name ${printer.port_name}",
unless => "Get-PrinterPort -Name ${printer.port.name}",
}
exec { "add printer $$printer.name for $printer.name":
command => "Add-Printer -Name ${printer.name}",
unless => "Get-Printer -Name ${printer.name}",
}
}
}
这是我的Hiera配置。
{
"classes": [
"windows_print_server"
],
"windows_server::administrators::list": ["ops-security"],
"windows_print_server::printers": [
{"name": "Bldg2-Flr1-HPM426fdn", "port_name": "172.17.17.132", "driver_name": "HP Color LaserJet Pro M452 PCL 6"},
{"name": "Bldg3-Flr 4 Dell 2335dn", "port_name": "172.17.33.174", "driver_name": "Dell 2335dn MFP PCL6"}
]
}
我相信$ {printer.driver_name}的init.pp上缺少正确的语法。不确定执行此操作的正确方法。类似于$ {printer [“ name”]}}
答案 0 :(得分:0)
The syntax for accessing values from a hash在该键周围使用方括号运算符[]
:
$printer['driver_name']
在大多数情况下,键周围的引号是可选的,因为Puppet会将裸露的单词识别为字符串(这是Puppet语言的一般特征,并非特定于哈希键)。
要将值插值到字符串中,需要将$
以外的所有内容括在花括号中:
$printers.each |Integer $index, Hash $printer| {
exec { "add printer driver ${printer[driver_name]} for ${printer[name]}":
command => "Add-PrinterDriver -Name ${printer[driver_name]}",
unless => "Get-PrinterDriver -Name ${printer[driver_name]}",
}