James Cammarata wrote:

Hi, we're trying to re-use some variables that are currently used for
templating a configuration file.  We found out these same hosts need to be
in our /etc/hosts file as well, so we'd like to use the built-in host type
to do this, but since our list of hosts is stored as an array, I'm having
some issues.

Synchronicity!  I did exactly that just two days ago.

$my_servers = ["server1.domain|1.1.1.1","server2.domain|1.1.1.2"]
[...]
define multihosts($line) {
  $hostname = split($line, "[|]")[0]
  $ip = split($line, "[|]")[1]
  ...
  host { "$name_$hostname": name => $hostname, ... }
}

multihosts { foo: line => $my_servers[0] }

But puppet does not seem to like indexing variables inside classes, and I
can't find anywhere in the language reference where to do this currently.

Indeed, the Puppet DSL doesn't support indexing yet.

Also, despite the fact that the split() function is in the function
reference, I am getting the following error:

err: Could not retrieve catalog: Unknown function split at
/var/lib/puppet/modules/... on node mynode

As has already been mentioned, split() didn't come until 0.25.
And even if you had split(), since you can't index, you can't
do it anyway.

Luckily, there is the regsubst() function, which can be put to
perhaps surprising use.  Here's what I did:

define hostentry($ipcolumn=2)
{
    $pattern = '^\s*(\S+)\s+(\S+)\s+(\S+)\s*$'
    $hostname = regsubst($name, $pattern, '\1')
    $ipno = regsubst($name, $pattern, "\\$ipcolumn")

    host {
        $hostname:
            ip => $ipno;
    }
}

In my case, the fields of each entry in the array are separated
by whitespace, not by the pipe character, and I have several
columns, and I can choose which column to pick for the IP address
using the ipcolumn parameter.  For your format, you would use
'^([^|]*)[|]([^|]*)' for $pattern (untested).  You can then do:

    hostentry {
        $my_servers: ;
    }

and off you go.


        /Bellman

--
You received this message because you are subscribed to the Google Groups "Puppet 
Users" group.
To post to this group, send email to puppet-us...@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en.

Reply via email to