Using variables to Call Puppet resources
Sometimes you may wish to use variables for calling a specific Puppet resources instead of using if-else conditions. Luckily Puppet allows us to use variables in Puppet resource names like classes, modules etc. …
In my case I had several Puppet classes like aem_curator::install_aem62
, aem_curator::install_aem63
and aem_curator::install_aem64
. To call the right Puppet class for the installation I used the following If-else conditions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if $aem_profile == 'aem62' {
aem_curator::install_aem62 { "${aem_id}: Install AEM profile ${aem_profile}":
aem_artifacts_base => $aem_artifacts_base,
aem_base => $aem_base,
}
} elsif $aem_profile == 'aem63' {
aem_curator::install_aem63 { "${aem_id}: Install AEM profile ${aem_profile}":
aem_artifacts_base => $aem_artifacts_base,
aem_base => $aem_base,
}
} elsif $aem_profile == 'aem_64' {
aem_curator::install_aem64 { "${aem_id}: Install AEM profile ${aem_profile}":
aem_artifacts_base => $aem_artifacts_base,
aem_base => $aem_base,
}
As you can see this file can be getting to big to keep track of it very easily.
So instead of maintaining such a complex Puppet manifest I want to reduce it to one Puppet resource call by calling a class using the variable aem_profile
in the class name. To do so we can call a Puppet resource within Resource[]
.
1
2
3
4
Resource["aem_curator::install_${aem_profile}"] { "${aem_id}: Install AEM profile ${aem_profile}":
aem_artifacts_base => $aem_artifacts_base,
aem_base => $aem_base,
}
As you can see, the class call now depends on the variable ${aem_profile}
. This variable defines which Puppet class we want to call.
In my case I reduced a manifest of 280 lines down to 40 lines Example.
Cheers