Reputation: 445
I have a script where I retrieve the IP address from a machine and then perform some logical operations to decide what value to give to a variable. Then, I use this value to make some decisions about the environment the machine is working in.
I am trying to translate this to Puppet, but I am not sure how to proceed. I know I can retrieve the IP address using facts, but should I do this in an manifest or a template? Also, where should I perform the logical operations. Please keep in in mind that I will be doing string manipulation.
To summarize, these are the steps I need.
Get the IP address from the machine.
Perform logical operations.
Pass a variable with the result to a Puppet manifest.
The main point is: how do I retrieve the fact as a variable and where should I put the code to work with that variable?
Thanks in advance.
Upvotes: 0
Views: 560
Reputation: 180093
You cannot exactly "retrieve" the IP using facts, but the catalog compiler can and does receive it as a fact. The former suggests that the catalog compiler has an active role in the process, which it does not.
You have two general alternatives:
You can compute the value on the node by writing a custom fact. Its work can be simplified by relying on Facter to provide the IP to it. The result will be presented to the catalog compiler as its own, separate fact, and nothing special is required on that side to use it anywhere.
The down sides of computing the derived value as a fact include that the code for doing so is copied to all nodes, where it can potentially be examined, and if a node is compromised then in principle the value of this fact can be spoofed. Also, the custom fact would probably need to be implemented in Ruby, in case that's an issue for you.
It's not particularly harder to perform the computation in the catalog builder. You could do it as a custom function, which would be comparable in complexity to a custom fact, but I'd suggest doing it in a class, and assigning the result to a class variable of that class. How, specifically, you should implement the value computation depends on the details of that computation. For example,
class site::derived_data {
$ip_pieces = split($ipaddress, '[.]')
# This is the derived value we are computing:
$department = $ip_pieces[2] ? {
0 => 'network operations',
# ...
default => 'other'
}
}
You could then use it elsewhere like so:
class mymodule::myclass(
# parameters ...
) {
include site::derived_data
notify { $site::derived_data::department : }
}
Upvotes: 2