Feature #3181
Create resource if it doesn't already exist (with array support)
| Status: | Needs More Information | Start date: | 02/16/2010 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | - | % Done: | 0% |
|
| Category: | functions | |||
| Target version: | - | |||
| Affected Puppet version: | 0.25.4 | Branch: | ||
| Keywords: | ||||
| Votes: | 2 |
Description
Hi all,
I’m currently looking for a way to say something like “create this resource if it doesn’t already exist”. The current way to do this, is simply checking with
if ! defined(Resource['$resname']) { resource{ '$resname':; } }
which works with strings, but doesn’t support arrays. And I’m running into several cases in which it would save me a lot of work if I could make this work if $resname is an array. I think more people can do something useful with this kind of functionality.
Although you probably have a better idea what would fit, a syntax that would work for this is something like
?resource { '$resname':; }
Would be especially useful if it could also support virtual/exported resources, like so, for example:
?@@resource { '$resname':; }
Thanks in advance for considering this!
History
Updated by James Turnbull over 2 years ago
- Category set to functions
- Status changed from Unreviewed to Needs Decision
- Assignee set to Luke Kanies
Updated by Luke Kanies about 2 years ago
- Status changed from Needs Decision to Needs More Information
- Assignee deleted (
Luke Kanies)
I like the basic idea, but the design needs to be fleshed out a bit more.
Could you provide a bit more info on how you use this?
Updated by Tim Stoop about 2 years ago
Currently, I’m working on our new Nagios module, so my example is Nagios orientated, but I’m sure you can see the benefit for other resources too. Mainly I’d like to use it for “magic resource creation”. Take the example below, hostgroups aren’t very interesting and we never use any other option than giving them a name, so we can more easily search for them in the Nagios interface. Actually, I don’t even know if there are other options at all… I use the syntax I proposed in the ticket, just for clarity’s sake.
define monitor_host ($hostgroups = "", ... ) {
?@@nagios_hostgroup { $hostgroups:
target => "/etc/nagios3/conf.d/hostgroups.cfg",
tag => "monitor-local",
}
?@@file { "/etc/nagios3/conf.d/hostgroups.cfg":
owner => "nagios",
}
@@nagios_host { $name:
hostgroups => $hostgroups,
...
}
}
This can be done using ‘if ! defined’ when you only enter one hostgroup, but it’s impossible if you want to do this with multiple hostgroups.
The idea is that there’s a lot of automation you can do with puppet that requires the creation of ‘stock’ resources, which can be created if you only know their name. Allowing for this request, would make it easier to handle arrays for this.
Another way to solve it, is by creating loop controls for arrays in the puppet DSL. That might actually solve even more problems than just the resource creation for resources that might already be created. That way you could do something like:
if $hostgroups {
for group in $hostgroups {
if ! defined(Nagios_hostgroup[$group]) {
@@nagios_hostgroup { $group:; }
}
etc.
}
}
PS. I believe nagios_host’s hostgroups setting currently doesn’t allow arrays, but that’s easily fixed with an inline_template or if #2990 gets accepted.