简体   繁体   English

木偶:使自定义功能依赖于资源

[英]Puppet: Making a custom function depend on a resource

I have a Puppet custom function that returns information about a user defined in OpenStack's Keystone identity service. 我有一个Puppet定制函数,该函数返回有关OpenStack的Keystone身份服务中定义的用户的信息。 Usage is something along the lines of: 用法大致如下:

$tenant_id = lookup_tenant_by_name($username, $password, "mytenant")

The problem is that the credentials used in this query ( $username ) are supposed to be created by another resource during the Puppet run (a Keystone_user resource from puppet-keystone ). 问题在于,该查询中使用的凭据( $username )应该由Puppet运行期间的另一个资源(来自puppet-keystoneKeystone_user资源)创建。 As far as I can tell, the call to the lookup_tenant_by_name function is being evaluated before any resource ordering happens, because no amount of dependencies in the calling code is able to force the credentials to be created prior to this function being executed. 据我所知,对lookup_tenant_by_name函数的调用是在任何资源排序发生之前进行评估的,因为调用代码中没有大量的依赖关系能够强制在执行此函数之前创建凭据。

In general, it is possible to write custom functions -- or place them appropriately in a manifest -- such that they will not be executed by Puppet until after some specified resource has been instantiated? 通常,可以编写自定义函数-或将其适当地放置在清单中-这样,在实例化某些指定资源之后,Puppet才能执行它们?

Short answer : You cannot make your manifest's behavior depend on resources declared inside of it. 简短答案 :您不能使清单的行为取决于清单中声明的​​资源。

Long answer: Parser functions are called during the compilation phase (on the master if you use one, or the agent if you use puppet apply ). 长答案:解析器函数是在编译阶段调用的(如果使用一个,则在主机上调用,如果使用puppet apply ,则在代理上调用)。 In neither case can it ever run before any resource is synced, because that will happen after the compiler has done all its work (including invocation of your functions). 在任何情况下,它都不会在同步任何资源之前运行,因为这将在编译器完成所有工作(包括调用函数)之后发生。

To query information from the agent machine, you generally want to use custom facts . 要从代理计算机查询信息,通常需要使用自定义事实 Still, those will be populated before even the compiler run. 不过,即使在编译器运行之前,这些变量也会被填充。

Likely the best approach in this situation is to make the manifest tolerate the absence of the information, so that anything that depends on the value that your lookup_tenant_by_name function returns will only be evaluated if that value is available. 在这种情况下,最好的方法可能是使清单能够容忍信息的缺失,这样,取决于lookup_tenant_by_name函数返回的值的任何事情都只会在该值可用的情况下进行评估。 This will usually be during the second Puppet run. 这通常是在第二次木偶游戏中。

if $tenant_id == "" {
  notify { "cannot yet find tenant $username": }
}
else {
  # your code using the tenant ID
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM