Skip to content
Advertisement

retrieve fact info based on variable in puppet

I have these facts:

"interfaces": "enp1s0f0,enp1s0f1,enp8s0,enp9s0,lo",
"ipaddress": "10.Z.Y.X",
"ipaddress_enp8s0": "10.Z.Y.X",
"ipaddress_lo": "127.0.0.1",
"main_ip": "10.Z.Y.X",

So with the main_ip fact I need to retrieve the interface that host it.

I have try the following to compare the ip:

  split($::interfaces, ',').each |  String $value | {
   notify { "ipaddress_${value}"  : }
  }

but I cannot retrieve the value of the fact with the variable i’m giving… Can you please advice me how to find the value of the fact with the variable information in puppet?

Or if you have any best idea on how to achieve the main goal it would be appreciated.

Advertisement

Answer

In any reasonably recent Puppet, you can access node facts through the $facts hash. This goes back at least to Puppet 4 (latest is Puppet 6). I don’t recall whether it was available in Puppet 3. In a version that supports it, you should be able to do almost what you described in comments:

notify { $facts["ipaddress_${value}"] : }

(Note punctuation differences.)

Going back at least as far as Puppet 3, you can also do it with the Puppet Swiss army knife: ERB templates. Here, the the scope object provided automatically to templates is key.

$myfact = inline_template('<%= scope.lookupvar("ipaddress_#{@value}") %>')
notify { $myfact : }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement