title: puppet, set template variable based on hostname value or unique random value
body: <p>I want to deploy a config file on several linux machines and need to make sure that a certain variable in that config file has a unique value for each machine. Until now I've been shipping the same config file the following way:</p>
  
  <pre><code>file {
      "/etc/nxserver/node.conf":
      mode =&gt; 644,
      ensure =&gt; file,
      source =&gt; "puppet:///modules/nxserver/nxserver-node.conf";
  }
  </code></pre>
  
  <p>I assume that switching to templates is as easy as replacing the <code>source</code> with <code>content</code>:</p>
  
  <pre><code>file {
      "/etc/nxserver/node.conf":
      mode =&gt; 644,
      group =&gt; root,
      content =&gt; template("nxserver/nxserver-node.erb"),
  
  }
  </code></pre>
  
  <p>In the <code>nxserver-node.erb</code> file I could set the variable of interest the following way:</p>
  
  <pre><code># This file is managed by puppet
  ... random config stuff ...
  somevariable = &lt;%= hostname %&gt;
  </code></pre>
  
  <p>My questions:</p>
  
  <ul>
  <li>Is there a way to process &amp; extract parts of <code>&lt;%= hostname %&gt;</code> inside the erb file? E.g. my hostnames could be "desktop-01234", and I would want to use the "01234" bit only. How would I achieve this?</li>
  <li>Alternatively, could I specify a range of valid values that get uniquely but randomly assigned to the variable inside the erb file? E.g. <code>somevariable</code> could be a variable representing some port number, in the range of 1000 - 4000. I need to make sure that on each puppet host this variable has a different value. How would I achieve this?</li>
  </ul>
  
  <p><strong>EDIT:</strong> I can use<br>
  <code>fqdn_rand(MAX, [SEED])</code> to generate a random number that's unique for each hostname, but according to the docs it will return a <code>random whole number greater than or equal to 0 and less than MAX</code>.</p>
  
  <pre><code>&lt;% Puppet::Parser::Functions.function('fqdn_rand') -%&gt;
  &lt;% value = scope.function_fqdn_rand(['10000']) -%&gt;
  somevariable=&lt;%= value.to_s %&gt;
  </code></pre>
  
  <p>I've tried using <code>+</code> as well as <code>add</code> to add an offset but <code>+</code> seems to be reserved for strings and the <code>add</code> function is unknown.</p>