Puppet 不拉动 Hiera 值

Puppet 不拉动 Hiera 值

在学习 Puppet 和 Hiera 时,我遇到了障碍。如果这很简单,我提前道歉。在我的 GitLab 中为 PuppetClass es_strat 提供了以下文件:

hiera.yaml

    ---
version: 5
defaults:
  data_hash: yaml_data
  datadir: data
hierarchy:
  - name: Hostname
    path: "hosts/%{facts.fqdn}.yaml"
  - name: hostgroup and environments
    path: "hostgroups/%{::hostgroup}/environments/%{facts.env}%{facts.env_num}.yaml"
  - name: hostgroup and tier
    path: "hostgroups/%{::hostgroup}/tiers/%{facts.tier}.yaml"
  - name: hostgroup
    path: "hostgroups/%{::hostgroup}.yaml"
  - name: tier
    path: "tiers/%{facts.tier}.yaml"
  - name: Common
    path: common.yaml

通用.yaml

    ---
es_strat::es_heap     : 16g
es_strat::es_version  : 2.3.2
es_strat::kopf_version: v2.1.2
es_strat::java_version: jdk1.7.0_91
es_strat::es_instances: 
  "%{::hostname}": 
    config: 
      bootstrap.mlockall: "true" 
      cluster.name: "%{::datacenter}%{::env}%{::env_num}stratsrch"
      discovery.zen.ping: 
        multicast: 
          enabled: "false"
        unicast: 
          hosts: "%{es_masters}"
      http: 
        compression: "true"
        enabled: "true"
        max_content_length: 500mb
        port: "9200"
      network.publish_host: "%{::ipaddress}"
      network.host: "%{::ipaddress}"
      node: 
        data: "true"
        master: "true"
        name: "%{::hostname}"
      path.logs: /indexes/logs
      transport.tcp.compress: "true"
      transport.tcp.port: "9300"
      indices.store.throttle.type: none
      script:
        indexed: "true"
        udpate: "true"
    datadir: /indexes/data

初始化文件

# Class: es_strat
#
# This module manages es_strat
#
# Parameters: none
#
# Actions:
#
# Requires: see Modulefile
#
# Sample Usage:
#
class es_strat (
  $es_heap      = hiera('es_strat::es_heap'),
  $es_instances = hiera('es_strat::es_instances'),
  $es_version   = hiera('es_strat::es_version'),
  $java_version = hiera('es_strat::java_version'),
  $es_hosts     = hiera('es_strat::es_hosts', undef),
  $kopf_version = hiera('es_strat::kopf_version', undef),
  $es_scripts   = hiera('es_strat::es_scripts', undef),
){
  # Create Elasticsearch user with reserved UID/GID.
  # TODO: Move this to virtual::users module
  ensure_resource('group', 'elasticsearch', {
    ensure     => 'present',
    forcelocal => true,
    gid        => 668981,
    before     => User['elasticsearch']
  })
  ensure_resource('user', 'elasticsearch', {
    ensure     => 'present',
    comment    => 'elasticsearch user',
    forcelocal => true,
    home       => '/opt/elasticsearch',
    shell      => '/bin/false',
    uid        => 3160070,
    gid        => 668981,
  })
  # Ensure elasticsearch logs are writeable. 
    file { [
    '/indexes/',
    '/indexes/logs',
  ]:
    ensure => directory,
    owner  => 'elasticsearch',
  }
  # Define master hosts to connect to. 
  if ! $es_hosts {
    $query_es_nodes = query_nodes("(class['es_strat'] and env=${::env} and env_num='${::env_num}')")
    $es_masters = parsejson(inline_template("[<%= @query_es_nodes.map{
      |host|
        \"\\\"\" + host + \":9300\\\"\"
      }.flatten.join(', ')
      %>]"
    ))
  }
  else {
    $es_masters = $es_hosts
  }
  # Install elasticsearch and setup instances. 
  class  { '::elasticsearch':
    version       => $es_version,
    init_defaults => {
      'ES_HEAP_SIZE' => $es_heap,
      'JAVA_HOME'    => "/opt/java/${java_version}/"
    },
    # Look these up again so es_masters will be included.
    instances     => hiera('es_strat::es_instances'),
  }
  # Install plugin if defined. 
  if $kopf_version {
    elasticsearch::plugin { "lmenezes/elasticsearch-kopf/${kopf_version}":
      instances  => $::hostname,
      proxy_host => 'repos.gspt.net',
      proxy_port => 3128
    }
  }
  # Install scripts if defined. 
  if $es_scripts {
    create_resources(elasticsearch::script, $es_scripts)
  }
  # Setup Java in path so plugins work propperly. 
  # TODO Remove this once this bug is fixed. https://github.com/elastic/puppet-elasticsearch/issues/619
  file {'/etc/sysconfig/mcollective':
    content => "export JAVA_HOME=/opt/java/${java_version}/",
    notify  => Service['mcollective'],
  }
}

然后,在 Foreman 中,我为 Host 设置了以下内容:

es_heap=hiera("es_strat::es_heap")

es_instances=hiera("es_strat::es_instances")

es_version=hiera("es_strat::es_version")

java_version=hiera("es_strat::java_version")

但是,当我在主机上运行 puppet 时(具体来说:puppet agent -t --no-noop)我收到以下错误:

错误:无法从远程服务器检索目录:服务器上的错误 500:服务器错误:评估错误:评估资源语句时出错,函数 lookup() 在节点上未找到名称为“es_strat::es_instances”的值

我很为难,因为它似乎应该能够从 Hiera 获取值。非常感谢任何/所有帮助。

答案1

好的,所以这个类在从 Puppet 3 更新到 Puppet 4 后就坏了。我们使用 GitLab 来控制我们的模块/类。这样,我就能通过以下设置让它发挥 99% 的功能:

数据/common.yaml

---
es_strat::es_heap     : 16g
es_strat::es_version  : 2.3.2
es_strat::kopf_version: v2.1.2
es_strat::java_version: jdk1.7.0_91
es_strat::es_instances:
  "%{::hostname}":
    config:
      bootstrap:
        mlockall: true
      cluster:
        name: "%{::datacenter}%{::env}%{::env_num}stratsrch"
      discovery:
        zen:
          ping:
            multicast:
              enabled: false
            unicast:
              hosts: "%{es_masters}"
              #hosts: "[]"
      http:
        compression: true
        enabled: true
        max_content_length: 500mb
        port: 9200
      indices:
        store:
          throttle:
            type: none
      network:
        host: "%{::ipaddress}"
        publish_host: "%{::ipaddress}"
      node:
        data: true
        master: true
        name: "%{::hostname}"
      path:
        data: /indexes/data
        logs: /indexes/logs
        repo: /nfs/lvs/elasticsearch/snapshots/stratsrch
      script:
        indexed: true
        udpate: true
      transport:
        tcp:
          compress: true
          port: 9300


#    datadir: /indexes/data
#"es_strat::es_scripts":
#  test:
#    source: "puppet:///modules/es_strat/%{::tier}/test.groovy"

清单/init.pp

# Class: es_strat
#
# This module manages es_strat
#
# Parameters: none
#
# Actions:
#
# Requires: see Modulefile
#
# Sample Usage:
#
class es_strat (
  $es_heap      = $::es_strat::es_heap,
  $es_instances = undef,
  $es_version   = $::es_strat::es_version,
  $java_version = $::es_strat::java_version,
  $es_hosts     = undef,
  $kopf_version = $::es_strat::kopf_version,
  $es_scripts   = undef,
){

  # Create Elasticsearch user with reserved UID/GID.
  # TODO: Move this to virtual::users module
  ensure_resource('group', 'elasticsearch', {
    ensure     => 'present',
    forcelocal => true,
    gid        => 668981,
    before     => User['elasticsearch']
  })
  ensure_resource('user', 'elasticsearch', {
    ensure     => 'present',
    comment    => 'elasticsearch user',
    forcelocal => true,
    home       => '/opt/elasticsearch',
    shell      => '/bin/false',
    uid        => 3160070,
    gid        => 668981,
  })

  # Ensure elasticsearch logs are writeable.
    file { [
    '/indexes/',
    '/indexes/logs',
  ]:
    ensure => directory,
    owner  => 'elasticsearch',
  }

  # Define master hosts to connect to.
  if ! $es_hosts {
    $query_es_nodes = query_nodes("(class['es_strat'] and datacenter=${::datacenter} and env=${::env} and env_num=${::env_num})")

    $es_masters = parsejson(inline_template("[<%= @query_es_nodes.map{
      |host|
        \"\\\"\" + host + \":9300\\\"\"
      }.flatten.join(', ')
      %>]"
    ))
  }

  else {
    $es_masters = $es_hosts
  }

  # Install elasticsearch and setup instances.
  class  { '::elasticsearch':
    version       => $es_version,
    init_defaults => {
      'ES_HEAP_SIZE' => $es_heap,
      'JAVA_HOME'    => "/opt/java/${java_version}/"
    },
    # Look these up again so es_masters will be included.
    instances     => $::es_strat::es_instances,
  }

  # Install plugin if defined.
  if $kopf_version {
    elasticsearch::plugin { "lmenezes/elasticsearch-kopf/${kopf_version}":
      instances  => $::hostname,
      proxy_host => 'repos.gspt.net',
      proxy_port => 3128
    }
  }

  # Install scripts if defined.
  if $es_scripts {
    create_resources(elasticsearch::script, $es_scripts)
  }

  # Setup Java in path so plugins work propperly.
  # TODO Remove this once this bug is fixed. https://github.com/elastic/puppet-elasticsearch/issues/619
  file {'/etc/sysconfig/mcollective':
    content => "export JAVA_HOME=/opt/java/${java_version}/",
    notify  => Service['mcollective'],
  }
}

现在,唯一无法正常工作的部分是自动生成集群中其他主机的列表。

相关内容