如何在 puppet 中实现虚拟 hiera 数据,或者,如何从每台机器上的用户选择中抽象出 hiera 用户定义?

How to realize virtual hiera data in puppet, or, how to abstract hiera user definitions from user selection on each machine?

我正在尝试在 hiera 中定义虚拟用户和组数据,然后通过在每个主机的基础上定义用户列表和组列表,仅在需要的节点上实现它。注意:我使用的是来自 puppet forge 的 accounts 模块,它使用了 hiera 中的 accounts:: 名称 space。例如,如果我创建这个:

# snippet from hiera.yaml
hierarchy:
  - name: "Users and Groups"
    glob: "UsersAndGroups/*.yaml"

# snippet from data/UsersAndGroups/webadmins.yaml
accounts::user_list:
  alice:
    uid: '999'
    gid: '999'
    comment: 'Alice User'
    shell: '/bin/bash'
  bob:
    uid: '998'
    gid: '998'
    comment: 'Bob User'
    shell: '/bin/bash'

它可以工作,但问题是,alicebob 会在所有机器上创建。我只希望它们在某些上创建。相反,我想在 hiera 中将此数据定义为 virtual,然后使用不同的数据结构来指定应在每台机器上 realized 哪些资源,例如:

# snippet from data/UsersAndGroups/webadmins.yaml
accounts::user_list:
  @alice:
    uid: '999'
    gid: '999'
    comment: 'Alice User'
    shell: '/bin/bash'
  @bob:
    uid: '998'
    gid: '998'
    comment: 'Bob User'
    shell: '/bin/bash'

然后创建另一个数据结构(和相应的人偶class,未显示),如下所示:

# snippet from data/users_by_host/hosts.yaml
realize_accounts::by_host:
  host1.example.dom:
    - alice
    - bob
  host2.example.dom:
    - bob
  host3.example.dom:
    - alice

我已经尝试了所有我能想到的变体来完成这项工作,但到目前为止都失败了。第一个问题是虚拟资源声明。它不喜欢我使用 @ 符号。我收到错误消息:found character '@' that cannot start any token.

谁能建议如何从应该在哪些计算机上创建哪些用户的选择中抽象出用户定义?

I am trying to define virtual user & group data in hiera, and then realize it only on the nodes where it's needed, by defining user lists and group lists on a per-host basis.

好的,这不无道理。

Note: I am using the accounts module from puppet forge, and it utilizes the accounts:: name space in hiera.

你可以使用puppetlabs/accounts模块,但如果你想坚持你的计划,你不能使用它的accounts class来管理用户或组,因为它不提供以虚拟方式宣布这些。这是声明的 Puppet 代码的函数,而不是(直接)class 使用的任何数据的函数。

您仍然可以使用 Accounts::User 定义的类型、模块的各种自定义数据类型及其自定义函数。这些可能提供了相对于 Puppet 的核心 User 资源类型的足够增值,使该模块物有所值。无论如何,您可能正在查看核心 Group 资源类型。您需要虚拟地声明 Accounts::User(或 User)和 Group 资源,然后根据您的每个主机列表实现需要的资源。


但还有其他选择。您在评论中澄清说,您的 objective 是集中用户和组属性的主列表,您可以从中 select 基于每台机器的子集。您可以在 Hiera's alias() interpolation function.

的帮助下直接在 Hiera 中完成所有这些操作

您可以使用两级(至少)层次结构来执行此操作。用户和组详细信息将进入所有主机共享的层次结构级别,如果您使用它们,也许 accounts::user_defaultsaccounts::group_defaults 也会进入那里。 accounts::user_listaccounts::group_list 将进入每个主机或其他更具体的层次结构级别,从中央列表插入元素。示例:

common.yaml

user:
  alice:
    uid: '999'
    gid: '999'
    comment: 'Alice User'
    shell: '/bin/bash'
  bob:
    uid: '998'
    gid: '998'
    comment: 'Bob User'
    shell: '/bin/bash'

host1.yaml

accounts::user_list:
  alice: "%{alias('user.alice')}"
  bob: "%{alias('user.bob')}"

host2.yaml

accounts::user_list:
  bob: "%{alias('user.bob')}"

host3.yaml

accounts::user_list:
  alice: "%{alias('user.alice')}"