简体   繁体   English

木偶使用hiera创建变量名

[英]Puppet create variable names using hiera

I want Puppet to create a different variable name depending on the hiera file associated with the environment. 我希望Puppet根据与环境关联的hiera文件创建一个不同的变量名。 I want to do this because I want Puppet to use the ip address associated with a specific network interface. 我想要这样做是因为我希望Puppet使用与特定网络接口关联的IP地址。 Ideally, the network interface will be in the hiera file. 理想情况下,网络接口将位于hiera文件中。 That way you could concatenate the ip_address variable name with the network interface defined in the hiera file, which would look something like. 这样,您可以将ip_address变量名称与hiera文件中定义的网络接口连接起来,看起来像这样。

::ipaddress_{$network_interface_from_hiera_file}

Is this possible? 这可能吗?

Right now I have an the following, but I think there is a better implementation. 现在,我有以下内容,但我认为有更好的实现方法。 If the network interfaces change I would have to add another case. 如果网络接口更改,我将不得不添加另一种情况。

if $environment == 'production' {
  $client_address = $::ipaddress_enp130s0f0
} else {
  $client_address = $::ipaddress_eth2
} 

It sounds like you're after an eval in Puppet, like you have in shell and Perl other languages, and as far as I know, there isn't one. 听起来您正在使用Puppet进行eval ,就像您在Shell和Perl其他语言中一样,据我所知,还没有。

I would probably just use a custom fact that always returns the IP address I care about. 我可能会使用一个总是返回我关心的IP地址的自定义事实。 Of course, then you need to solve the problem of how to get the custom facts out to your fleet. 当然,那么您需要解决如何将自定义事实提供给您的机队的问题。

Another solution might be to use Hiera's hierarchical lookup: 另一个解决方案可能是使用Hiera的分层查找:

In hiera.yaml: 在hiera.yaml中:

:hierarchy:
  - %{::node_environment}
  - common

In common.yaml: 在common.yaml中:

---
myclass::client_address: "%{::ipaddress_eth2}"

In production.yaml: 在production.yaml中:

---
myclass::client_address: "%{::ipaddress_enp130s0f0}"

Finally, be aware that you can look up values from within Hiera, see here . 最后,请注意,您可以在Hiera中查找值,请参见此处 Possibly that could be helpful. 可能会有所帮助。

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

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