Ask Your Question
0

puppet v4 case statement variable set but not matched

asked 2016-09-27 00:44:07 -0500

cm01 gravatar image

updated 2016-10-09 21:58:11 -0500

Consider

class php::classes::install {

notify { "operatingsystemmajrelease = $operatingsystemmajrelease": }

        case $operatingsystemmajrelease {
                5: {
                        $php_packages = [
                                'php53', 'php53-cli', 'php53-common', 'php53-ldap',
                                'php53-pgsql', 'php53-pdo', 'php53-mysql',
                        ]
                        }
                6: {
notify { "in case 6": }
                        $php_packages = [
                                'php', 'php-cli', 'php-common', 'php-ldap',
                                'php-pgsql', 'php-pdo', 'php-mysql',
                        ]
                }
        }
notify { "php_packages = $php_packages": }
}

--noop o/p

Notice: /Stage[main]/Php::Classes::Install/Notify[operatingsystemmajrelease = 6]/message: current_value absent, should be operatingsystemmajrelease = 6 (noop)
Notice: /Stage[main]/Php::Classes::Install/Notify[php_packages = ]/message: current_value absent, should be php_packages =  (noop)
Notice: Class[Php::Classes::Install]: Would have triggered 'refresh' from 2 events

Notice that although operatingsystemmajrelease is set to "6", the case statement will not match it....!

Why not ???

Chris
NB: this works under puppet v3

@Eric: what version are you running? For me

facter -p 
operatingsystemmajrelease => 6

and putting either single or double quotes (or /5/, /6/ for regex match) around the 5 &6 in the pp file gives

Error: Could not retrieve catalog from remote server: Could not intern from text/pson: Invalid tag "php_packages = "
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

@Dylan - here you go

cat install.pp
class php::classes::install {

notify { "operatingsystemmajrelease = $operatingsystemmajrelease": }

    case $operatingsystemmajrelease {
        "5": {
notify { "in case 5": }
            $php_packages = [
                'php53', 'php53-cli', 'php53-common', 'php53-ldap',
                'php53-pgsql', 'php53-pdo', 'php53-mysql',
            ]
            }
        "6": {
notify { "in case 6": }
notify { "operatingsystemmajrelease6 = $operatingsystemmajrelease": }

            $php_packages = [
                'php', 'php-cli', 'php-common', 'php-ldap',
                'php-pgsql', 'php-pdo', 'php-mysql',
            ]
        }
        default: {
notify { "in default case": }

#               fail("Failed to match operatingsystemmajrelease $operatingsystemmajrelease")
        }
    }
notify { "php_packages = $php_packages": }

}

@Dylan 2: Server: puppetserver version: 2.4.0 puppet agent -V 4.5.0

Client puppet agent -V 3.3.1

Still getting the '... invalid tag ..' msg if using above with-quotes method.. Without quotes it drops into the default case ...

It gets worse; apparently it's 'consistent'

class tuned {

        if $operatingsystemmajrelease == 6 {
                include 'tuned::classes::install'
                include 'tuned::classes::configuration'
                include 'tuned::classes::service'
        }

}

That simple class does nothing as run.
If I comment out start/end 'if' statements, (ie just call the 'include's ), then it will run them..
aarrghh - Help...


Ok, so I had a week off and came back with a more relaxed mind and did some careful testing

It turns out there were 2 issues which got mixed in together.

My original/actual problem (testing against an integer literal) is solved by using either single or double quotes around the integer literal.

Where I got distracted was trying to debug by 'notify{}'ing an array, which it appears can't be done. This throws an 'invalid tag...' error as shown above.
You can however notify{} individual elements .

I did try "array sectioning" https://docs.puppet.com/puppet/latest...dataarray.html#array-sectioning but couldn't make it work for eg [0,-1]; I'm guessing its because the client end is too old. Only wanted it for debugging anyway, so not a problem as such.

Anyway, thanks for all the suggestions etc - think I'm going to call this one 'Done' for now

Cheers
Chris

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
0

answered 2016-09-27 06:33:48 -0500

puser gravatar image

Shot in the dark but... when I look at my fact for the same thing I get

"operatingsystemmajrelease": "11",

That looks like a string to me.. Try

case $operatingsystemmajrelease {
            "5": {
                    $php_packages = [
                            'php53', 'php53-cli', 'php53-common', 'php53-ldap',
                            'php53-pgsql', 'php53-pdo', 'php53-mysql',
                    ]
                    }
            "6": {
notify { "in case 6": }
                    $php_packages = [
                            'php', 'php-cli', 'php-common', 'php-ldap',
                            'php-pgsql', 'php-pdo', 'php-mysql',
                    ]
            }
    }
edit flag offensive delete link more
0

answered 2016-09-28 02:49:11 -0500

I'm a bit concerned about the error that you are getting when you try to "put either single or double quotes (or /5/, /6/ for regex match) around the 5 &6 in the pp file" It almost seems like it's bringing up some kind of bug in the compiler... Just to be sure, can I see the exact code that you used when quoting the values in the case statement, as normally quoting them would work.

The following works for me:

case $operatingsystemmajrelease {
  "7": { notify { "case 7": }}
  "6": { notify { "case 6": }}
  "5": { notify { "case 5": }}
}
edit flag offensive delete link more

Comments

@cm01 Your code is working perfectly for me, if you run your code are you still getting the error you described earlier? Also what versions are the agent this this code is running on and the master that is compiling it?

dylanratcliffe gravatar imagedylanratcliffe ( 2016-09-28 20:42:54 -0500 )edit
0

answered 2016-10-06 07:11:10 -0500

geoff_williams gravatar image

@cm01 how are you testing this? I just tried your final sample (adjusted to match my OS) and after quoting the value being matched it works fine:

test.pp

class tuned {
    if $operatingsystemmajrelease == '7' {
            notify { "in if block":}
            #include 'tuned::classes::install'
            #include 'tuned::classes::configuration'
            #include 'tuned::classes::service'
    }

}
include tuned

Run test with puppet apply

root@agent:~# puppet apply test.pp 
Notice: Compiled catalog for agent.localdomain in environment production in 0.05 seconds
Notice: in if block
Notice: /Stage[main]/Tuned/Notify[in if block]/message: defined 'message' as 'in if block'
Notice: Applied catalog in 0.09 seconds

I tested your above fixed code for php::classes::install and it works fine. In the example after for tuned, Your problem here is that you just forgot to quote the value again. The best way to isolate the cause of your problems would be to condense them into a simple testcase to demonstrate the problem as you have done and then run it using puppet apply, as above. This will help you isolate the code your interested in from the rest of your codebase.

Things to check:

  • testcase in apply mode
  • make your testcase really simple - eg just notify resources in a case statement
  • watch for fixing one error and moving on to the next - eg where does the invalid tag message you mentioned come from? I don't see any your code...
  • you can check syntax errors with puppet parser validate FILENAMEif your worried that something like a changing a quote has broken something
  • If everything works in apply mode but breaks in agent mode, then in your case I'd suspect its a version miss-match issue and I'd proceed to see if upgrading the agent fixes things (or provision a new node and test there)

HTH Geoff

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: 2016-09-27 00:44:07 -0500

Seen: 162 times

Last updated: Oct 09 '16