Puppet's
regsubst
function provides an easy way to manipulate text, search and replace within strings, or extract patterns from strings. We often need to do this with data obtained from a fact, for example, or from external programs.
In this example, we'll see how to use regsubst
to extract the first three octets of an IPv4 address (the network part, assuming it's a class C address).
Follow these steps to build the example:
Add the following code to your manifest:
$class_c = regsubst($::ipaddress, '(.*)\..*', '\1.0') notify { "The network part of ${::ipaddress} is ${class_c}": }
Run Puppet:
ubuntu@cookbook:~/puppet$ papply Notice: The network part of 10.96.247.132 is 10.96.247.0 Notice: /Stage[main]/Admin::Test/Notify[The network part of 10.96.247.132 is 10.96.247.0]/message: defined 'message' as 'The etwork part of 10.96.247.132 is 10.96.247.0' Notice: Finished catalog run in 0.09 seconds