puppet epp 模板错误(无效的 EPP:语法错误位于)

puppet epp template error (Invalid EPP: Syntax error at)

我会尝试将 erb 迁移到 epp,并将 hiera_hash 迁移到 lookup。 当前错误:

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"
        }
      ]

如果可能请帮忙。

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
<% } -%>

请记住,在 Puppet DSL(EPP 很大程度上基于它)中,这些看起来像:

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

所以你必须在 EPP 中使用相同的大括号。 (Documentation: Conditional statements).