String Manipulation ?

1,344 views
Skip to first unread message

Richard Palmer

unread,
Mar 4, 2008, 9:46:20 AM3/4/08
to Puppet Users
Hello,

I'm trying to find a way to extract a number from a string variable
(within a ERB template). The only way I can think how to do it would
be a string manipulation function or a regular expression but I cannot
find any reference to anything like this in the Wiki. Is this possible
in puppet? (preferably using 0.20.1 (Debian Etch)).

thanks,

Richard.

Thomas Bellman

unread,
Mar 4, 2008, 10:12:29 AM3/4/08
to puppet...@googlegroups.com
Richard Palmer wrote:

You can put arbitrary Ruby code in your ERB templates, so anything you
can do with Ruby, you can do in ERB. For example, if you have a
Puppet variable named 'foo' with the value "4711", you can get "4712"
in your file by writing

<%= foo.to_i() + 1 %>

in your template.

Can you be more specific about what you mean by extracting a number
from a string? What kind of string value do you have, and what number
do you want to extract from it?


/Thomas Bellman

Richard Palmer

unread,
Mar 4, 2008, 11:14:02 AM3/4/08
to Puppet Users
Hello,

On Mar 4, 3:12 pm, Thomas Bellman <bell...@nsc.liu.se> wrote:
> Richard Palmer wrote:
> > I'm trying to find a way to extract a number from a string variable
> > (within a ERB template). The only way I can think how to do it would
> > be a string manipulation function or a regular expression but I cannot
> > find any reference to anything like this in the Wiki. Is this possible
> > in puppet? (preferably using 0.20.1 (Debian Etch)).
>
> You can put arbitrary Ruby code in your ERB templates, so anything you
> can do with Ruby, you can do in ERB. For example, if you have a
> Puppet variable named 'foo' with the value "4711", you can get "4712"
> in your file by writing

Aha. Thanks Thomas, that'll do it. I'm going to have to start reading
up on Ruby.

> Can you be more specific about what you mean by extracting a number
> from a string? What kind of string value do you have, and what number
> do you want to extract from it?

It's for a load balancing cluster, I want to create different
variables in the erb file(s)
using the cluster identifier, which is part of the hostname So from
the hostname
'tc-dev-3' I just need the 3.

From a very quick look at the ruby String reference, scan() or
split() look like they will
do the job.

thanks again,

Richard.

jrojas

unread,
Mar 4, 2008, 6:10:55 PM3/4/08
to Puppet Users
Easy....


<%= hostname.gsub(/\D/,"") %>

(that will give you 3 if the hostname is tc-dev-3)

jrojas

unread,
Mar 4, 2008, 6:14:59 PM3/4/08
to Puppet Users
Btw, thats ERB + \D = non-numeric, so it just substitutes all non-
numerics with nothing (removes them) \d is all numeric.
-jason

Blake Barnett

unread,
Mar 4, 2008, 2:32:01 PM3/4/08
to puppet...@googlegroups.com

On Mar 4, 2008, at 8:14 AM, Richard Palmer wrote:
>
> It's for a load balancing cluster, I want to create different
> variables in the erb file(s)
> using the cluster identifier, which is part of the hostname So from
> the hostname
> 'tc-dev-3' I just need the 3.
>
> From a very quick look at the ruby String reference, scan() or
> split() look like they will
> do the job.

Sounds like a perfect custom fact.

http://reductivelabs.com/trac/puppet/wiki/AddingFacts

Then you can just reference "cluster_id" (or whatever) in your
templates and anywhere else that can use facter.

-Blake

Peter Meier

unread,
Mar 4, 2008, 5:36:01 PM3/4/08
to puppet...@googlegroups.com
Hi

> It's for a load balancing cluster, I want to create different
> variables in the erb file(s)
> using the cluster identifier, which is part of the hostname So from
> the hostname
> 'tc-dev-3' I just need the 3.
>
> From a very quick look at the ruby String reference, scan() or
> split() look like they will
> do the job.

sometimes I'm thinking in situations like these if not rather a facter
variable from a custom fact might be the better approach? with that you
could get off the ugly ruby code (ugly in sense of the structure of your
code) in a view more to a point where the better place might be for it.
so afterwards you can just use the facter variable inside the templates.

greets pete

Reply all
Reply to author
Forward
0 new messages