Fetching the domain name from a string variable

142 views
Skip to first unread message

Jan

unread,
Apr 28, 2011, 4:54:36 AM4/28/11
to puppet...@googlegroups.com
Hi guys,

is there a simple way to fetch the domain name (e.g. test-domain.tld) from
a string variable (e.g. host-internal.test-domain.tld)?

Jan

Mohit Chawla

unread,
Apr 28, 2011, 4:58:31 AM4/28/11
to puppet...@googlegroups.com
On Thu, Apr 28, 2011 at 2:24 PM, Jan <j...@agetty.de> wrote:
is there a simple way to fetch the domain name (e.g. test-domain.tld) from
a string variable (e.g. host-internal.test-domain.tld)?

Directly use the domain fact from facter or:
http://docs.puppetlabs.com/references/latest/function.html#split

Jan

unread,
Apr 28, 2011, 7:44:04 AM4/28/11
to puppet...@googlegroups.com, Mohit Chawla

I should have been more clear about what I'm trying to achieve. I'm not
talking about the domain name of a node but from a string contained
variable. Lets say we've got the following variable located somewhere in
our manifest ...

$vhost = "host-internal.test-domain.tld"

... and there might be a need to compare it with a string in order to
execute some peace of code:

if $vhostDomain == "test-domain.tld" {
# create file if $variable contains domain name
file { '/some/file': ensure => present }
}

Further more the $vhost variable is passed to a definition to create the
respective vhost. For the conditional to work the variable $vhostDomain
shall contain only the domain part of $vhost.

What about using regsubst here? I just played around with it a few times
but didn't

I think that the split function won't work because it splits the string
into three separate parts by using '.' as the delimiter and to join them
back together (domain part + tld) doesn't seem to be much practical.

Actually I solved my problem by using the "regsubst" function:

-------------------------->------------------------------
:~$ cat test.pp;
$vhost = "host-internal.test-domain.tld"
$vhostSplit = regsubst($vhost,'[a-z\-]*\.(.*)','\1')
alert $vhostSplit

:~$ puppet apply test.pp;
alert: Scope(Class[main]): test-domain.tld
--------------------------<------------------------------

Works like a charm, just what I was searching for ;)

Jan

Mohit Chawla

unread,
Apr 28, 2011, 8:23:02 AM4/28/11
to Puppet Users


On Thu, Apr 28, 2011 at 5:52 PM, Mohit Chawla <mohit.cha...@gmail.com> wrote:
On Thu, Apr 28, 2011 at 5:14 PM, Jan <j...@agetty.de> wrote:
Works like a charm, just what I was searching for ;)

Heh, sorry for not understanding the problem correctly. Thanks for posting the solution. :)


Reply all
Reply to author
Forward
0 new messages