referencing boolean within puppet manifest

924 views
Skip to first unread message

Mike Reed

unread,
Apr 12, 2016, 2:53:00 PM4/12/16
to Puppet Users
Hello all,

I've recently experienced some inconsistencies around referencing Boolean values within a puppet manifest.  We've written some custom Boolean facts and we look to the true/false values within our manifests to help make decisions on what should or shouldn't be done.

The issue I have is the different behavior I see when referencing the fact value.  For instance, in some cases I have to reference the Boolean value with quotes (ie.. 'true') in order to get things working right.  In other cases, removing the quotes returns the results I would expect.

I understand that this is most likely due to what the fact is returning but I was wondering if there's a 'best practice' for approaching something like this.  I've also played around with stringify_facts and achieved varied results (in terms of referencing the Boolean values) and that may have only confused me.  

We're currently running: Puppet v:3.8.6/Hiera v:1.3.4

I realize this question touches on a few different things but does anybody have a brief explanation for how I might consistently reference these Boolean values throughout our puppet infrastructure?  

Here's an example of a custom fact that we use:

require 'facter'

Facter.add(:nvidia_installed) do
  setcode do
    tools_test = Facter::Util::Resolution.exec("/usr/bin/nvidia-smi")
    if tools_test
      nvidia_installed = true
    else
      nvidia_installed = false
    end
  end
end

In order to reference this particular value within my manifest, I have to reference the value without quotes:   elsif ($::class == 'render_workstation') and ($::nvidia_installed == false) {

As always, thank you in advance for the help and support.

Cheers,

Mike


Garrett Honeycutt

unread,
Apr 12, 2016, 9:31:47 PM4/12/16
to puppet...@googlegroups.com
Hi Mike,

Booleans such as true vs. stringified booleans such as 'true' have long
been a sore spot within the Puppet ecosystem.

You want to set stringify_facts = false in your puppet.conf. This will
allow you to have other data types as facts such as booleans, arrays,
and hashes.

Recommend using this design pattern to deal with stringified booleans in
Puppet v3.

if is_string($my_param) == true {
$my_param_bool = str2bool($my_param)
} else {
$my_param_bool = $my_param
}
validate_bool($my_param_bool)

So given the param, $my_param, this will ensure it is an actual boolean.
In your code you reference $my_param_bool instead of $my_param.


Best regards,
-g

--
Garrett Honeycutt
@learnpuppet
Puppet Training with LearnPuppet.com
Mobile: +1.206.414.8658

Mike Reed

unread,
Apr 13, 2016, 6:53:50 PM4/13/16
to Puppet Users
Thank you for the explanation Garrett.  I've seen similar things like that in my web searches but failed to put it all together.

I'll roll with this until we upgrade to 4.0

Cheers,

Mike

Henrik Lindberg

unread,
Apr 14, 2016, 7:31:25 AM4/14/16
to puppet...@googlegroups.com
On 13/04/16 03:31, Garrett Honeycutt wrote:
> On 4/12/16 2:53 PM, Mike Reed wrote:
>> Hello all,
>>
>> I've recently experienced some inconsistencies around referencing
>> Boolean values within a puppet manifest. We've written some custom
>> Boolean facts and we look to the true/false values within our manifests
>> to help make decisions on what should or shouldn't be done.
>>
>
> Hi Mike,
>
> Booleans such as true vs. stringified booleans such as 'true' have long
> been a sore spot within the Puppet ecosystem.
>
> You want to set stringify_facts = false in your puppet.conf. This will
> allow you to have other data types as facts such as booleans, arrays,
> and hashes.
>
> Recommend using this design pattern to deal with stringified booleans in
> Puppet v3.
>
> if is_string($my_param) == true {
> $my_param_bool = str2bool($my_param)
> } else {
> $my_param_bool = $my_param
> }
> validate_bool($my_param_bool)
>
> So given the param, $my_param, this will ensure it is an actual boolean.
> In your code you reference $my_param_bool instead of $my_param.
>
>

From 4.5.0 (coming soon) the above example can be written like this:

$my_param_bool = Boolean($my_param)

Although in 4.x booleans are most likely already of Boolean type.

Best
- henrik

--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/
Reply all
Reply to author
Forward
0 new messages