About | FAQ | Help
Ask Your Question
0

Reassign variable. Compare variable with string

asked 2018-03-13 01:13:01 -0500

Anton gravatar image

updated 2018-03-13 10:05:04 -0500

Hello! I try Reassign variable

class zabbix_host (
  $server = $zabbix_host::params::server,
) inherits zabbix_host::params {
  $server = $manufacturer == 'OpenStack Foundation' { 'true' => 'proxy2', default => 'proxy1' }
}

I see topic https://ask.puppet.com/question/16820...

https://ask.puppet.com/question/13242... But i dont understand something

How Compare variable with string and Reassign it? I try used check variable

    class zabbix_host (
    ) inherits zabbix_host::params {
      $server = $manufacturer == 'OpenStack Foundation' { 'true' => 'proxy2', default => $zabbix_host::params::server } <== at line 15
    }

And get result Error: Could not parse for environment production: Syntax error at '{'; expected '}' at line 15

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-03-13 08:49:18 -0500

DarylW gravatar image

updated 2018-03-13 08:49:31 -0500

Variables are immutable in puppet, you cannot change the value once it has been assigned.

I have usually seen examples where people use separate variables, and pick the value out of them later..

something like the following

class zabbix_host (
  $server = $zabbix_host::params::server,
) inherits zabbix_host::params {
  $_server = $manufacturer == 'OpenStack Foundation' { 'true' => 'proxy2', default => 'proxy1' }
  # make use of the 'internal' $_server value in your code later, instead of the $server parameter
}
edit flag offensive delete link more

Comments

i tried this code. added to question

Anton gravatar imageAnton ( 2018-03-13 10:04:29 -0500 )edit

sorry! I didn't verify that you code was valid, I just changed your code to use a different name (_server instead of server). I'm not on a machine where I have direct access to puppet where I can run code samples through, follow the suggestion from natemccurdy above for proper syntax

DarylW gravatar imageDarylW ( 2018-03-14 09:00:01 -0500 )edit
0

answered 2018-03-14 02:46:45 -0500

natemccurdy gravatar image

This line is not valid Puppet code:

$server = $manufacturer == 'OpenStack Foundation' { 'true' => 'proxy2', default => 'proxy1' }

I'm not exactly sure what you're trying to do, but if it's just comparing a variable to a string to conditionally set the variable, you could use an if statement:

if $manufacturer == 'OpenStack Foundation' {
  $server = 'proxy2'
} else {
  $server = 'proxy1'
}

Personally, I like using a selector for that type of thing though as it's easier to read in my opinion:

$server = $manufacturer ? {
  'OpenStack Foundation' => 'proxy2',
  default                => 'proxy1',
}

And as has already been stated, you can't reassign a variable in Puppet. So if you end up using one of the examples I showed you, you will need to remove the $server parameter.

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: 2018-03-13 01:13:01 -0500

Seen: 67 times

Last updated: Mar 14