【问题标题】:Hiera - variables from hashHiera - 来自哈希的变量
【发布时间】:2021-05-27 11:07:49
【问题描述】:

我在 Hiera 真的很糟糕,我只是似乎没有得到它。我不知道这是否可能,但我想为我的班级提供一个哈希,例如:

modules::unit::filemode::config
 - /usr/bin/ls, root, root, 0775

并让它映射到

file { "$filename":
   owner => $owner,
   group => $group,
   mode  => $mode
}

我尝试过使用哈希和$config.each,但我似乎无法弄清楚。

有什么建议吗? :) 谢谢

【问题讨论】:

    标签: puppet hiera


    【解决方案1】:

    我想为我的班级提供一个哈希值,例如:

    modules::unit::filemode::config
     - /usr/bin/ls, root, root, 0775
    

    这不是有效的 YAML,并且您似乎试图与键 modules::unit::filemode::config 关联的值看起来不像散列。哈希将值与键相关联,那么键在哪里?

    从预期的用途来看,我猜你想要更像这样的东西:

    # The value is one hash with keys name, owner, group, and mode
    modules::unit::filemode::config:
      path: '/usr/bin/ls'
      owner: 'root'
      group: 'root'
      mode: '0775'
    

    或者像这样:

    # The value is hash of hashes; the outer hash has file names for keys,
    # and the inner ones have keys owner, group, and mode
    modules::unit::filemode::config2:
      '/usr/bin/ls':
        owner: 'root'
        group: 'root'
        mode: '0775'
      '/maybe/another':
        owner: 'luser'
        group: 'root'
        mode: '0644'
    

    其中任何一个都可以为类参数$modules::unit::filemode::config 提供数据:

    class modules::unit::filemode(
      Hash $config
    ) {
      # ...
    }
    

    前一种数据风格为单个文件提供信息,通过键显式访问哈希成员是最有意义的:

    file { $config['path']:
      owner => $config['owner'],
      group => $config['group'],
      mode  => $config['mode'],
    }
    

    另外,因为我仔细选择了与 File 资源属性名称(包括 path)匹配的哈希键,您可以将其缩写为:

    file { $config['path']:
      * => $config,
    }
    

    在这种情况下迭代成员没有多大意义,因为它们几乎没有单独的意义,而且它们只是在句法上而非语义上具有可比性。

    后一种数据布局允许提供有关多个文件的信息,一个用于外部哈希的每个元素。这些外部成员在语义上是可比的,并且迭代它们是使用数据的一种可能方式,可能是这样的:

    $config2.each |$path, $props| {
      file { $path:
        * => $props,
      }
    }
    

    这声明了一个File 资源,对应于(外部)散列的每个元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 2017-03-29
      • 2013-03-21
      • 1970-01-01
      相关资源
      最近更新 更多