Ask Your Question
0

puppet-agent 1.8 - facter 3.5: howto check for an undefined fact ?

asked 2017-12-17 18:43:44 -0600

cm01 gravatar image

Hi Guys

Want to be able to check if a disk is attached (fwiw it's normally on real hw, not VM in this case)
I always get

 Evaluation Error: Operator '[]' is not applicable to an Undef Value

When using eg

if ( defined( $facts['disks']['sdb']['model'] ) )

or similar.

does anyone now how to go about this?

Thx
Chris

puppetserver version: 2.7.0 aka puppet 4.8.0

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
1

answered 2017-12-18 04:34:43 -0600

stivesso gravatar image

updated 2017-12-18 04:42:08 -0600

Hi cm01,

As you can see in the output below, the error is most probably due to the third element in the facts (model). it might be better for you to run the condition on $facts['disks']['sdb'] instead of $facts['disks']['sdb']['model'] (especially as you seem to try to check that sdb is attached).

[stivesso@mytestlab ~]$ facter disks
{
  xvda => {
    size => "8.00 GiB",
    size_bytes => 8589934592
  }
}

[stivesso@mytestlab ~]$ puppet apply -e 'if $facts['disks'] != undef {notice "not undef"} else {notice "undef"}'
Notice: Scope(Class[main]): not undef
Notice: Compiled catalog for mytestlab.athome.internal in environment production in 0.02 seconds
Notice: Applied catalog in 0.01 seconds

[stivesso@mytestlab ~]$ puppet apply -e 'if $facts['disks']['sdb'] != undef {notice "not undef"} else {notice "undef"}'
Notice: Scope(Class[main]): undef
Notice: Compiled catalog for mytestlab.athome.internal in environment production in 0.02 seconds
Notice: Applied catalog in 0.01 seconds

[stivesso@mytestlab ~]$ puppet apply -e 'if $facts['disks']['sdb']['model'] != undef {notice "not undef"} else {notice "undef"}'
Error: Evaluation Error: Operator '[]' is not applicable to an Undef Value.  at line 1:4 on node mytestlab.athome.internal
edit flag offensive delete link more
0

answered 2017-12-19 15:10:58 -0600

cm01 gravatar image

Daryl,

Tried that but got

" Evaluation Error: Error while evaluating a Function Call, 'defined' parameter 'vals' expects a value of type String or Type, got Undef"

Apparently define() only copes with a proper var :(

Look like I'll have to go with

if ( $facts.dig('disks','sda','model') != undef ) 
{
    notify{"sda defined": }  
}
else
{
       notify{"sda NOT defined": }  
 }

which is ugly imho ...

I am genuinely surprised that defined() doesn't handle 'undef' value - after all that's what we are testing for ....

Chris

edit flag offensive delete link more

Comments

Interesting! I didn’t try executing it, but I had assumed it would work as well! I probably would use dig to pull a value out into an intention revealing variable and evaluate if that was defined

DarylW gravatar imageDarylW ( 2017-12-19 16:24:52 -0600 )edit
0

answered 2017-12-19 08:49:23 -0600

DarylW gravatar image

updated 2017-12-19 08:50:40 -0600

I believe the functionality you are looking for is called 'dig'... see https://puppet.com/docs/puppet/5.3/fu...

dig Returns a value for a sequence of given keys/indexes into a structure, such as an array or hash. This function is used to “dig into” a complex data structure by using a sequence of keys / indexes to access a value from which the next key/index is accessed recursively.

The first encountered undef value or key stops the “dig” and undef is returned.

An error is raised if an attempt is made to “dig” into something other than an undef (which immediately returns undef), an Array or a Hash.

Example

$data = {a => { b => [{x => 10, y => 20}, {x => 100, y => 200}]}}
notice $data.dig('a', 'b', 1, 'x')

would put the value 100

in your case, you could check

if ( defined( $facts.dig('disks','sdb','model') ) )
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-12-17 18:43:44 -0600

Seen: 267 times

Last updated: Dec 19 '17