Hello list, I have some services that may be duplicated in some machines. They are much like an Apache vhost. In order to remove details from my manifests, I moved the service names to ENC because they are machine-dependent. Here are my datasources and manifests:
ENC:
parameters:
myservices:
host1:
myperserviceconfig
host2:
myperserviceconfig
Hiera:
myservice_host:
myproblematicconfig:
config1:
data
config2:
data
myotherconfig:
...
Manifests:
class profile::my::myservice {
# ENC: $myservices
$hiera_myservice_host = hiera("myservice_host")
# ENC has per-host/service and hiera has per-environment configurations
create_resources(::myservice::host, $myservices, $hiera_myservice_host)
}
define myservice::host (myconfigs...) {
...
if $myproblematicconfig {
create_resources(::myservice::myconfig, $myproblematicconfig)
}
}
Each myproblematicconfig key is a new resource
and I can have zero to N keys. This configuration is working nice if I have only one service, but if I have two or more services, Puppet Master correctly says:
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Duplicate declaration: myservice::myconfig[config1] is already declared
Tried prefix (from puppetlabs/stdlib) in order to change the resource name but it only supports arrays. Changing create_resources to $var.each should help but it requires parser=future which I cannot use right now. Do you see another approach I could try?
So, I'm interpreting the hiera data as providing configuration details that, if present, apply to every 'myservice', at least as defaults. Furthermore, from the data and manifests I judge that 'myproblematicconfig' is a distinguished identifier, handled in a manner specific to it.
A different structure for your data might work better
, but you can also work with the data structure you already have. In particular, create_resources() can often be replaced by an array-titled resource declaration with the array drawn from the keys() of your resource hash. Combinatorial resource declarations are a bit trickier, but they can be done, even without the future parser.
You may be able to make that cleaner, simpler, or otherwise better by applying some of your knowledge of the problem space to the structure of the defined types.
Em sexta-feira, 11 de julho de 2014 11h11min17s UTC-3, jcbollinger escreveu:So, I'm interpreting the hiera data as providing configuration details that, if present, apply to every 'myservice', at least as defaults. Furthermore, from the data and manifests I judge that 'myproblematicconfig' is a distinguished identifier, handled in a manner specific to it.
Almost that. I have quite some configurations like that but almost all of them are applied on a single ERB template. The "problematicconfig", on the other hand, will manage different files. So AFAICS the iteration need to be at the Puppet manifest side. create_resources() is the way I have found to iterate a hash without parser=future.A different structure for your data might work better
Share your thoughts! No problem with some refactor.
, but you can also work with the data structure you already have. In particular, create_resources() can often be replaced by an array-titled resource declaration with the array drawn from the keys() of your resource hash. Combinatorial resource declarations are a bit trickier, but they can be done, even without the future parser.
Got the idea, nice approach with keys+regsubst, thank you very much.You may be able to make that cleaner, simpler, or otherwise better by applying some of your knowledge of the problem space to the structure of the defined types.
Digging a bit more into the problem I found another approach: define a local variable with the $title just before the hiera_hash call, and use such local variable in the hash declaration. Something like this:
Manifest:
class profile::my::myservice {
$mytitle_hiera = $title
# ENC: $myservices
$hiera_myservice_host = hiera("myservice_host")
# ENC has per-host/service and hiera has per-environment configurations
create_resources(::myservice::host, $myservices, $hiera_myservice_host)
}
Hiera:
myservice_host:
myproblematicconfig:
%{mytitle_hiera}_config1:
data
%{mytitle_hiera}_config2:
data
myotherconfig:
...
It worked like a charm in my configuration. On the other hand Puppet docs[1] says this is not a good practice. I follow the doc in the sense that the lookup will be dependent of a manifest configuration.
Best practices, straightforward code and fit the needs are really challenging.