Casting Fixnum to String (Hiera/YAML)

1,333 views
Skip to first unread message

Lars Francke

unread,
Feb 26, 2012, 12:48:11 PM2/26/12
to puppet...@googlegroups.com
Hi,

I'm trying to write a SSH module to learn Hiera. The module is
including a Firewall config from the Puppetlabs Firewall module.

I've made the port configurable using a variable "ssh_server_port".

$port = hiera('ssh_server_port')
firewall { "100 SSHD":
proto => 'tcp',
action => 'accept',
dport => $port,
}

If I do $ssh_server_port = 22 in a Puppet file everything works as expected.

If I'm using a YAML file with this:
ssh_server_port: 2222
I get the following error message: Parameter dport failed: Munging
failed for value 2222 in class dport: can't convert Fixnum into String
It makes sense for the Firewall module to require a String here as it
accepts ranges too.

With
ssh_server_port: '2222'
in the YAML file everything works as expected.

It would be nice for a plain Integer (Fixnum) to work as well when
it's coming from a YAML file.

I guess the question boils down to: Is there a way to cast from Fixnum
to String?

It seems like an inconsistency to me that in Puppet it works quoted
and unquoted but not in YAML so I'd like to provide the same syntax
for both versions. For now i've added a validate_string($port) call.

Thanks,
Lars

Krzysztof Wilczynski

unread,
Mar 1, 2012, 1:52:51 PM3/1/12
to Puppet Users
Hi Lars,

[...]
> It would be nice for a plain Integer (Fixnum) to work as well when
> it's coming from a YAML file.

When you introduce a numeric value through the manifest, then Puppet
will do the right thing during parsing it, but when you introduce
Integer or Float from Hiera, then you will get them "as-is" after
parsing YAML, meaning as intended types in Ruby, so to speak. And
because Ruby is dynamic and uses duck-typing, it simply just work
internally to the point when parser is trying to combine both types.

> I guess the question boils down to: Is there a way to cast from Fixnum
> to String?

Yes, look below :)

> It seems like an inconsistency to me that in Puppet it works quoted
> and unquoted but not in YAML so I'd like to provide the same syntax
> for both versions. For now i've added a validate_string($port) call.

Puppet in most cases treats non-negative Internet and Float as string
(unless this behavior differs nowadays).

Take a look on this:

https://github.com/kwilczynski/puppet-functions/blob/master/lib/puppet/parser/functions/num2str.rb

And this:

https://github.com/kwilczynski/puppet-functions/blob/master/lib/puppet/parser/functions/type.rb

I hope it helps a little :)

KW

Lars Francke

unread,
Mar 2, 2012, 5:59:04 AM3/2/12
to puppet...@googlegroups.com
Hi Krzysztof,

>> I guess the question boils down to: Is there a way to cast from Fixnum
>> to String?
>
> Yes, look below :)

Excellent!

Yes, thank you very much!

This looks like a good addition for puppet-stdlib, no?

Thanks for taking the time to answer.

Cheers,
Lars

Reply all
Reply to author
Forward
0 new messages