hmaier
hmaier

Reputation: 83

Changing Array[String] in Puppet

I have a quite basic question: there is the array $nameserver, from which I want to cut the last entry and insert a new entry at index 0.

class baseline::resolv (
  Optional[Array[String]] $nameserver = undef,
  String $search                      = 'domain.net
  tag 'baseline_resolv'

  # Include baseline::network für Netzwerk Zonen Informationen
  include baseline::network
  tag 'baseline_resolv'

  # Include baseline::network für Netzwerk Zonen Informationen
  include baseline::network',
){

  if $facts['os']['name'] == 'Ubuntu' {
    $nameserver = ['127.0.0.1'] + $nameserver[0, -1]
  }

}

When compiling I get the error that a variable cannot be re-assigned. Due to the declarative nature of puppet, this kinda makes sense to me. Is there a way so solved this problem and still use the variable $nameserver?

EDIT: I see that you cannot reassign variables in puppet. Is there any convention, how such a needed helper variable would be called?

Upvotes: 0

Views: 144

Answers (1)

John Bollinger
John Bollinger

Reputation: 180058

When compiling I get the error that a variable cannot be re-assigned.

Yes:

Puppet allows a given variable to be assigned a value only one time within a given scope. This is a little different from most programming languages.

(Assigning Variables, Puppet Language Reference)

I've linked the current docs, but this has always been a characteristic of Puppet. The doc goes on a bit about scope, but as I understand what you're looking for, I don't think that provides an out for you.

Is there any convention, how such a needed helper variable would be called?

No.

But, inasmuch as you tagged the question "hiera", I'm inclined to think that you're probably going about the whole thing a suboptimal way. If the value in question is coming from Hiera data, then the right solution is to ensure that Hiera provides the data you want it to do in the first place, in all cases, instead of embedding special case tweak code in your manifests. That moots the question of naming a secondary variable. The Hiera subsystem is designed and intended to support flexibly assigning the right data to each node.

Upvotes: 0

Related Questions