人偶不能使用 keys() 和 hiera_hash()

puppet not working with keys() and hiera_hash()

我在 hiera 节点变量中有 solr_enabled = true。我在 fstab 挂载点的这个节点列表中也有这样的:

fstab_homes:
'/home1':
device: 'UUID=ac2ca97e-8bce-4774-92d7-051482253089'
'/home2':
device: 'UUID=d9daaeed-4e4e-40e9-aa6b-73632795e661'
'/home3':
device: 'UUID=21a358cf-2579-48cb-b89d-4ff43e4dd104'
'/home4':
device: 'UUID=c68041de-542a-4f72-9488-337048c41947'
'/home16':
device: 'UUID=d55eff53-3087-449b-9667-aeff49c556e7'

在 solr.pp 中,我想获取第一个挂载的主磁盘,在那里创建文件夹并使符号 link 变为 /home/cpanelsolr。

为此我写了代码/etc/puppet/environments/testing/modules/cpanel/manifests/solr.pp:

# Install SOLR - dovecot full text search plugin

class cpanel::solr(
    $solr_enable = hiera('solr_enabled',false),
    $homes = hiera_hash('fstab_homes', false),
    $homesKeys = keys($homes),
  )
{
if $solr_enable == true {
    notify{"Starting Solr Installation ${homesKeys[0]}":}
    if $homes != false and $homesKeys[0] != '/home' {
        file { "Create Solr home symlink to ${homesKeys[0]}":
           path => '/home/cpanelsolr',
           ensure => 'link',
           target => "${homesKeys[0]}/cpanelsolr",
        }
    }

    exec { 'cpanel-dovecot-solr':
        command => "/bin/bash -c 
'/usr/local/cpanel/scripts/install_dovecot_fts'",
      }
  }
}

但是当我在开发节点中 运行 时出现错误:

root@webcloud2 [/home1]# puppet agent -t --no-use_srv_records --server=puppet.development.internal --environment=testing --tags=cpanel::solr
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
2018-08-03  6:04:54 140004666824672 [Note] libgovernor.so found
2018-08-03  6:04:54 140004666824672 [Note] All governors functions found too
2018-08-03  6:04:54 140004666824672 [Note] Governor connected
2018-08-03  6:04:54 140004666824672 [Note] All governors lve functions found too
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: keys(): Requires hash to work with at 
/etc/puppet/environments/testing/modules/cpanel/manifests/solr.pp:6 on node webcloud2.development.internal
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

怎么了?

你至少有两个问题。

第一个问题是 $home 根本不会在该上下文中设置。您需要重写为:

class cpanel::solr(
    $solr_enable = hiera('solr_enabled',false),
    $homes = hiera_hash('fstab_homes', false),
  )
{
  $homes_keys = keys($homes)
  ...
}

第二个问题是您的 YAML 缩进不正确,因此 fstab_homes 实际上不会 return 哈希。应该是:

fstab_homes:
  '/home1':
    device: 'UUID=ac2ca97e-8bce-4774-92d7-051482253089'
  '/home2':
    device: 'UUID=d9daaeed-4e4e-40e9-aa6b-73632795e661'
  '/home3':
    device: 'UUID=21a358cf-2579-48cb-b89d-4ff43e4dd104'
  '/home4':
    device: 'UUID=c68041de-542a-4f72-9488-337048c41947'
  '/home16':
    device: 'UUID=d55eff53-3087-449b-9667-aeff49c556e7'

最后,请注意在 Puppet 的参数名称中使用 camelCase 可能会在某些情况下导致问题,因此最好使用 snake_case.