简体   繁体   中英

Puppet create variable names using hiera

I want Puppet to create a different variable name depending on the hiera file associated with the environment. I want to do this because I want Puppet to use the ip address associated with a specific network interface. Ideally, the network interface will be in the hiera file. That way you could concatenate the ip_address variable name with the network interface defined in the hiera file, which would look something like.

::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.

I would probably just use a custom fact that always returns the IP address I care about. 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:

In hiera.yaml:

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

In common.yaml:

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

In production.yaml:

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

Finally, be aware that you can look up values from within Hiera, see here . Possibly that could be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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