Puppet .... ruby regex

59 views
Skip to first unread message

Alex Samad

unread,
Aug 4, 2016, 2:17:55 AM8/4/16
to Puppet Users
Hi

I want to take ${trusted['certname']} and  extract the hostname short form from it.

so if i had thisbox.abc.com.au. I would like to get just thisbox

I have tried this

$pp_shortcertname= "${trusted['certname']}[/bitbucket/]"

doesn't work , tried this

$pp_shortcertname= "${trusted['certname']}"[/bitbucket/]

error cannot convert to numeric error.

and

$pp_shortcertname= ${trusted['certname']}[/bitbucket/]

doesn't work 

???  bit lost ...

Alex Samad

unread,
Aug 4, 2016, 2:26:37 AM8/4/16
to Puppet Users
so got to this

$pp_shortcertname= "${trusted['certname']}".match(/([^.]+)\./)[1]

not sure I fully understand why this works and [] doesn't.

Not 100% sure I understand what the [1] does !

A

Craig Dunn

unread,
Aug 4, 2016, 3:59:15 AM8/4/16
to puppet...@googlegroups.com
On Thu, Aug 4, 2016 at 8:17 AM, Alex Samad <al...@samad.com.au> wrote:
Hi

I want to take ${trusted['certname']} and  extract the hostname short form from it.

so if i had thisbox.abc.com.au. I would like to get just thisbox


$hostpart=split($trusted['certname'], '[.]')[0]

... would be a cleaner approach.

Craig

Robert Poulson

unread,
Aug 4, 2016, 10:42:29 AM8/4/16
to puppet...@googlegroups.com
$pp_shortcertname= "${trusted['certname']}".match(/([^.]+)\./)[1]

not sure I fully understand why this works and [] doesn't.

Not 100% sure I understand what the [1] does !

The parentheses are for capturing the matching text, and \1 for re-using it as a backreference; [1] outside of the pattern.

Rp

R.I.Pienaar

unread,
Aug 4, 2016, 10:50:54 AM8/4/16
to puppet-users
this is not a ruby question, the code in question is PUPPET DSL.

The DSL match function looks for a regex and returns what it finds and any
capture groups.

You access the 2nd item in the array as array[1]

if certname if hostname.domain.name

$matches = $trusted["certname"].match(/([^.]+)\./)

Here $matches will have ["hostname.", "hostname"]

and $matches[1] will fetch "hostname"

https://docs.puppet.com/puppet/latest/reference/function.html#match

Alex Samad

unread,
Aug 4, 2016, 8:31:07 PM8/4/16
to Puppet Users
So Puppet DSL is ruby like then....

Think thats where I was going wrong. I went to the ruby page

Craig & Robert thanks.

Alex
Reply all
Reply to author
Forward
0 new messages