Ask Your Question

Reassign variable. Compare variable with string

asked 2018-03-13 01:13:01 -0600

Anton gravatar image

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

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 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

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

DarylW gravatar image

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

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


i tried this code. added to question

Anton gravatar imageAnton ( 2018-03-13 10:04:29 -0600 )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 -0600 )edit

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

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


Asked: 2018-03-13 01:13:01 -0600

Seen: 129 times

Last updated: Mar 14