2017-05-05 100 views
1

我會嘗試遷移erbepp並棄用hiera_hashlookup。 當前出錯:木偶epp模板錯誤(無效的EPP:語法錯誤在)

Error: Could not retrieve catalog from remote server: 
Error 500 on SERVER: Server Error: Evaluation Error: 
Error while evaluating a Function Call, epp(): Invalid EPP: 
Syntax error at 'Port ' at /etc/puppetlabs/code/environments/production/modules/sshd/templates/sshd_config.epp:4:24 
at /etc/puppetlabs/code/environments/production/modules/sshd/manifests/init.pp:23:16 on node puppettestnode 

init.pp

class sshd { 
    #$sshd_config = hiera_hash('sshd') 
    $sshd=lookup('sshd', {merge => 'hash'}) 

    package { 'openssh-server': 
    ensure => present, 
    before => Service['sshd'], 
    } 

    file { '/etc/ssh': 
    ensure => directory, 
    owner => 'root', 
    group => 'root', 
    mode => '0755', 
    require => Package['openssh-server'] 
    }-> 

    file { '/etc/ssh/sshd_config': 
    owner => 'root', 
    group => 'root', 
    mode => '0644', 
    ensure => file, 
    content => epp("${module_name}/sshd_config.epp"), 
    }~> 

    service { 'sshd': 
    ensure => running, 
    require => [ 
     Package['openssh-server'], 
     File['/etc/ssh/sshd_config'], 
    ], 
    } 
} 

sshd_config.epp

###Managed by Puppet### 

# What ports, IPs and protocols we listen for 
<% if $sshd["port"] -%> 
Port <%= $sshd["port"] %> 
<% else -%> 
Port 22 
<% end -%> 

# Use these options to restrict which interfaces/protocols sshd will bind to 
#ListenAddress :: 
#ListenAddress 0.0.0.0 
<% if $sshd['listen'] -%> 
ListenAddress <%= $sshd['listen'] %> 
<% else -%> 
ListenAddress 0.0.0.0 
<% end -%> 

common.yml

從傀儡服務器
--- 
sshd: 
    port: '22' 
    listen: '0.0.0.0' 

木偶查找:

puppet lookup sshd --merge unique --environment production --explain 
Searching for "sshd" 
    Global Data Provider (hiera configuration version 5) 
    Using configuration "/etc/puppetlabs/puppet/hiera.yaml" 
    Merge strategy unique 
     Hierarchy entry "Per-node data" 
     Path "/etc/puppetlabs/code/environments/production/hiera/nodes/puppettestserver" 
      Original path: "nodes/%{::fqdn}" 
      Path not found 
     Hierarchy entry "Common data" 
     Path "/etc/puppetlabs/code/environments/production/hiera/common.yaml" 
      Original path: "common.yaml" 
      Found key: "sshd" value: { 
      "port" => "22", 
      "listen" => "0.0.0.0" 
      } 
     Merged result: [ 
     { 
      "port" => "22", 
      "listen" => "0.0.0.0" 
     } 
     ] 

請幫助,如果這是可能的。

回答

4

if語句使用的是Ruby語法而不是Puppet DSL語法,該語法導致了不尋常的錯誤消息。

模板應該是:

###Managed by Puppet### 

# What ports, IPs and protocols we listen for 
<% if $sshd["port"] { -%> 
Port <%= $sshd["port"] %> 
<% } else { -%> 
Port 22 
<% } -%> 

# Use these options to restrict which interfaces/protocols sshd will bind to 
#ListenAddress :: 
#ListenAddress 0.0.0.0 
<% if $sshd['listen'] { -%> 
ListenAddress <%= $sshd['listen'] %> 
<% } else { -%> 
ListenAddress 0.0.0.0 
<% } -%> 

請記住,在木偶DSL(其中EPP是非常基礎上),這些看起來像:

if $sshd["port"] { 
    # Port 
} else { 
    # Port 22 
} 

,所以你必須使用相同的EPP中的花括號。 (Documentation: Conditional statements)。

+0

非常感謝,但現在我有另一個錯誤'評估錯誤:運算符'[]'不適用於Undef值。在/etc/puppetlabs/code/environments/production/modules/sshd/templates/sshd_config.epp:4:7節點puppettestnode' – beliy

+1

'$ sshd'未定義,您需要使用完全限定的變量名與類名稱前綴('$ sshd:sshd')或者在epp函數調用中傳遞:'epp(「$ {module_name} /sshd_config.epp」,{「sshd」=> $ sshd})''。有關詳細信息,請參閱https://docs.puppet.com/puppet/4.10/lang_template_epp.html#accessing-variables。 –

+0

非常感謝。 Variant'epp(「$ {module_name} /sshd_config.epp」,{「sshd」=> $ sshd})可以正常工作,但對於使用類前綴,需要使用''sshd :: sshd' – beliy