On Oct 7, 5:44 am, Luis Bruno <m...@lbruno.org>
wrote:> I''m trying to put user definitions in a single string, and make
> something like the following. Should I go down this way, or should I
> use a template that takes the array and creates all users in one go?
>
> $users = [
> "223 sip.login allyourbase Sip Extension 223"
> ]
>
> $users.split().each do |userspec|
> spec = userspec.split()
> asterisk::user { spec[1]:
> full => spec[4..42],
> secret => spec[2],
> extension => spec[0],
> }
> end
>
> Please forgive my lack of Ruby-fu, I''m still making baby-steps.
Puppet is implemented in Ruby, and you can extend it with plug-ins
implemented in Ruby. It is very important, however, to remember that
the Puppet manifest language IS NOT Ruby (nor ERB), syntactic
similarities notwithstanding. You cannot use Ruby code in your
manifests. Furthermore, although the Puppet template() function
supports using ERB to compute arbitrary values, you cannot use
templates to declare Puppet resources (or if you somehow can, don''t --
that would be Evil).
If the only thing your mashup code is supposed to do is unpack the
strings and declare resources with the pieces, then I''m not sure what
your approach gains over writing a regular (and much clearer) resource
declaration for each user. You can use single-line format if you
wish, and it won''t consume much more space than your array of strings:
asterisk_user {"sip.login": extension => "223", secret
=>
"allyourbase", full => "Extension 223" }
asterisk_user {"sap.login": extension => "224", secret
=>
"arebelongtous", full => "Extension 224" }
...
The advantages of that approach over string parsing are manifold.
If you need to do a bit more work with the pieces than your example
showed, then you may be able to use a defined type. For example:
define ast_user($extension, $secret, $full) {
asterisk_user { $name:
full => $full,
secret => $secret,
extension => $extension,
}
}
ast_user {"sip.login": extension => "223", secret =>
"allyourbase",
full => "Extension 223" }
ast_user {"sap.login": extension => "224", secret =>
"arebelongtous",
full => "Extension 224" }
...
As that''s written, it provides no advantage over declaring the
resources directly, but if you needed to manipulate the provided
values before declaring the resource then you could put Puppet code
for that purpose into the definition.
If you have needs that absolutely require the capabilities of a
general-purpose programming language (i.e. Ruby), then you would put
that code into your custom type''s provider class (I''m assuming
that
asterisk::user is supposed to be a custom type) or maybe just use a
custom provider for an existing type, such as User. Considering that
you''re a Ruby newbie, I''d recommend avoiding custom types and
custom
providers if you can. Actually, I''d recommend that even if you were a
Ruby pro. Custom types and providers have their place and can be very
useful, but if Puppet can already do the job then let it.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To post to this group, send email to puppet-users@googlegroups.com
To unsubscribe from this group, send email to
puppet-users+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en
-~----------~----~----~----~------~----~------~--~---