Aufbau einer Puppet-Variable basierend auf Bedingungen

Aufbau einer Puppet-Variable basierend auf Bedingungen

Ich gewöhne mich also an die unveränderlichen Variablen in Puppet und bin nun auf der Suche nach Ratschlägen oder einer Klarstellung, ob ich bereits auf dem richtigen Weg bin.

Ich habe ein Verzeichnis, das ich synchronisieren möchte, aber möglicherweise auch mehrere Unterverzeichnisse davon. Ich möchte also ein Array mit einem oder mehreren Pfaden erstellen, das an einen rekursivenDateiRessource. Idealerweise hätte ich so etwas

$paths = ['fist/path'] # assume this is provided via class parameter
# this should work, since it's only overriding what was provided from higher scope
if($condition) {
  $paths += 'second/path'
}
# this won't fly, since $paths has already been set once locally
if($another_condition) {
  $paths += 'third/path'
}

aber das kann ich nicht, richtig, da die Variablen unveränderlich sind. Was mir bisher als „bester“ Ansatz eingefallen ist, ist so etwas wie das hier

$paths = ['fist/path']
if($condition) {
  $condition_1_path = ['second/path']
} else {
  $condition_1_path = []
}
if($another_condition) {
  $condition_2_path = ['third/path']
} else {
  $condition_2_path = []
}

$paths = concat($paths, $condition_1_path, $condition_2_path)

Ich bin mir nicht sicher, obVerkettungwird einen Eintrag aus dem Ergebnis auslassen, wenn es ein leeres Array für eines seiner Argumente bereitgestellt hat, aber ich warte darauf, das zu testen, sobald ichFinden Sie heraus, wie Sie die Standardbibliothek laden.

Wie dem auch sei, wenn ich mir diesen Code ansehe, finde ich ihn einfach nur abscheulich. Gibt es eine sauberere Möglichkeit, so etwas zu tun?

Antwort1

Es ist hässlich, aber ich habe es so gemacht.

if($condition) {
  $condition_1_path = 'first/path'
} else {
  $condition_1_path = ''
}
if($another_condition) {
  $condition_2_path = 'second/path'
} else {
  $condition_2_path = ''
}

# split into array based on whitespace
$mylistofpaths = split("$condition_1_path $condition_2_path", '\s+')

verwandte Informationen