Puppet:在没有包含的情况下声明资源

Puppet: declare resource without containment

Puppet 将包含在另一个已定义类型(或类)内声明的所有已定义类型。据我了解,这意味着任何声明的来源都将取决于容器。这将导致依赖循环:

define user {
}
define bar {
  user { $name: }
  ->
  Bar[$name]
}

bar { 'foo': }
错误:无法应用完整目录:找到 1 个依赖周期:
(用户[foo] => 酒吧[foo] => 用户[foo])

有什么办法可以避免这种情况吗?我有一个特定的实例,我希望在声明 Bar[$name] 时也声明 User[$name],但 Bar[$name] 依赖于 User[$name],而不是相反。基本上与 require 相同的行为,但用于定义的类型依赖性。

有什么方法可以做到这一点,或者是唯一的解决方案,让清单声明 Bar[$name] 也声明 User[$name](然后添加对 [=19= 的主体的依赖) ] 或在声明清单中?


一个更现实的例子:

define servize {}
define appserver {
  user { $name: }
  ->
  servize { $name: }
}

appserver { 'app': }

# the deploy application needs a directory owned by itself on startup
file { '/tmp/foobar':
  ensure => directory,
  owner  => 'app', # auto-require
}
->
Appserver['app']

首先,请不要重新定义内置类型。所有内置类型列表:https://docs.puppetlabs.com/references/latest/type.html.

如果您只有一个特定实例,其中 $Bar[$name] 依赖于 User[$name] 您可以从栏定义中删除用户并创建 ordered_bar

  define ordered_bar {
    user { $name: }
    ->
    bar {$name : }
  }

你只需要创建一个 ordered_bar 的实例。
另请阅读有关 puppet 中资源排序的文档:https://docs.puppetlabs.com/learning/ordering.html

您可以将有问题的资源创建为虚拟资源。这样做的可行性取决于你的数据层的结构。

define servize {}

user { 'app': }

define appserver {
  User<| title == $name |>
  ->
  servize { $name: }
}

appserver { 'app': }

# the deploy application needs a directory owned by itself on startup
file { '/tmp/foobar':
  ensure => directory,
  owner  => 'app', # still auto-require, but no ill effect
}
->
Appserver['app']