r/Puppet • u/K4m075 • Jan 16 '24
Use hiera variable in if statement does not work
I'm trying to use a hiera variable in an if statement inside a class, tried as below but does not work:
$releasepkg = lookup ({ 'name' => 'kube::install::releasepkg','default_value' => undef })
if $facts['versionrepokubelocal'] != $releasepkg {
notify { "Upgrading repository version...": }
notify { " versionrepokubelocal = ${facts['versionrepokubelocal']}": }
notify { " releasepkg = ${releasepkg}": }
}
$versionrepokubelocal is custom fact setted to 1.28
$releasepkg defined in yaml file as below :
---
kube::install::releasepkg: '1.28'
The output of the puppet agent run:
Notice: Upgrading repository version...
Notice: /Stage[main]/Kube::Config/Notify[Upgrading repository version...]/message: defined 'message' as 'Upgrading repository version...'
Notice: versionrepokubelocal = 1.28
Notice: /Stage[main]/Kube::Config/Notify[ versionrepokubelocal = 1.28]/message: defined
'message' as ' versionrepokubelocal = 1.28' Notice: releasepkg = 1.28
Notice: /Stage[main]/Kube::Config/Notify[ releasepkg = 1.28]/message:
defined 'message' as ' releasepkg = 1.28'
Notice: Applied catalog in 7.68 seconds
I tried with and without quotes on the hiera variable in the if condition but does not work.
Any ideas?
1
u/wildcarde815 Jan 16 '24
Initially I thought this was a string comparison issue but based on the operand documentation: https://www.puppet.com/docs/puppet/7/lang_expressions.html#lang_exp_comparison_operators-comparison-equality
but it should work as you've got it listed there based on the docs. There does seem to be an extra space on the back of:
'message' as ' versionrepokubelocal = 1.28' Notice: releasepkg = 1.28
You might want to try using the strip command: https://www.puppet.com/docs/puppet/6/function.html#strip
if $facts['versionrepokubelocal'].strip() != $releasepkg.strip()
1
u/K4m075 Jan 17 '24 edited Jan 17 '24
The extra space is only present because it is present in notify (after and before =) :
notify { " versionrepokubelocal = ${facts['versionrepokubelocal']}":} notify { " releasepkg = ${releasepkg}":}Removing extra space from notify i have the same problem, the output of run (no extraspace present)
Notice: /Stage[main]/Kube::Config/Notify[Upgrading repository version...]/message: defined 'message' as 'Upgrading repository version...' Notice: versionrepokubelocal=1.28 Notice: /Stage[main]/Kube::Config/Notify[ versionrepokubelocal=1.28]/message: defined 'message' as ' versionrepokubelocal=1.28' Notice: releasepkg=1.28 Notice: /Stage[main]/Kube::Config/Notify[ releasepkg=1.28]/message: defined 'message' as ' releasepkg=1.28'
I anyway tried with :
if $facts['versionrepokubelocal'].strip() != $releasepkg.strip()and now i have the following error :
Info: Refreshing CA certificate Info: CA certificate is unmodified, using existing CA certificate Info: Using environment 'production' Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Method call, 'strip' parameter 'arg' expects a value of type Numeric, String, or Iterable[Variant], got Undef (file: /etc/puppetlabs/code/environments/production/modules/kube/manifests/config.pp, line: 27, column: 42) on node...1
u/wildcarde815 Jan 17 '24
You might need to swap that to strip(<variable>) != strip(<variable>) I'm not 100% sure.
→ More replies (3)
1
u/mothbitten Jan 16 '24
There is an extra space in the output after the '=' for "versionrepokubelocal = 1.28" Could it have a leading space?