稍后在人偶中将用户添加到组

Add user to group at a later point in puppet

我在一个模块中有一个用户资源被几个不同的节点使用。现在我想将此用户添加到一个组中,但只能在一个特定节点中。有好的解决办法吗?

模块看起来像这样:

class testmodule::basics {
  user { 'testuser':
    ensure     => present,
    home       => '/home/testuser',
    managehome => true,
  }
}

节点清单:

node 'testnode' {
  include testmodule::basics

  # here I would like to add the user to a group
  # something like this (obviously does not work because of duplicate resource)

  user { 'testuser':
    groups     => 'testgroup',
    membership => 'minimum',
  }
}

您有多种选择,分为几个大类。

类别 1 - 使用外部数据来传达用户应该拥有哪些辅助组。特定数据可能是一个标志,指示用户是否应该在次要组中,或者它可能是适当的次要组的实际数组。然后,您可以通过直接调用 lookup()hiera() 函数来获取它,具体取决于您使用的 Puppet 版本,或者为其创建 class 参数并使用自动数据绑定。

示例:

modules/testmodule/manifests/basics.pp:

class testmodule::basics($secondary_groups = []) {
  user { 'testuser':
    ensure     => present,
    home       => '/home/testuser',
    managehome => true,
    groups     => $secondary_groups
  }
}

data/nodes/special.my.com.yaml:

---
testmodule::basics::secondary_groups:
  - testgroup

类别2 - 设置一个class参数来接收区分数据,就像在其中一个类别中一样1 个选项,并通过外部节点 classifier (ENC) 输入数据,而不是外部数据。然而,设置和启用 ENC 比将数据馈送到单个 class 具有更广泛的意义,所以我不推荐这样做,除非您已经在使用或计划使用 ENC。

类别 3 - 在需要时执行资源参数覆盖。这几乎是对您的示例清单的 drop-in 更改,尽管将覆盖放在单独的 class 中比直接在节点块中执行它会更好。在来自 testmodule::basicsinherits 的 class 中,您可以使用资源参数覆盖语法,如下所示:

modules/testmodule/manifests/basics/special.pp:

class testmodule::basics::special inherits testmodule::basics {
  User['testuser'] {
    groups     => 'testgroup'
  }
}

但是,如果您想在节点块或不相关的 class 中执行此类覆盖,则需要通过收集器执行此操作:

node 'testnode' {
  include testmodule::basics

  User<title == 'testuser'> {
    groups     => 'testgroup'
  }
}

两种覆盖在使用范围之外存在一些细微差别,因此请阅读 the docs 了解更多信息。