FAQ
Is it possible to access @resource variables inside a type?

I would like to make some decisions on parameters based on other parameters
that may have already been set.

For example,
---
newparam(:param1) do
Puppet.debug "Found drivesperarray parameter"
desc "parameter 1"
validate do |value|
if resource[:otherparam] then
#dosomething
else
resource[:param1] = 0
end
end
Puppet.debug "Parameter 1 is: #{@resource[:param1]}"
end
---

But I keep getting messages like "undefined method `[]' for nil:NilClass"

Anyone have experience with this? I've tried searching around for example
without much luck...

Thanks!

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/GvvMAT2llvMJ.
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.

Search Discussions

  • ZJE at Jul 31, 2012 at 9:20 pm

    On Tuesday, July 31, 2012 4:05:28 PM UTC-5, ZJE wrote:
    Is it possible to access @resource variables inside a type?

    I would like to make some decisions on parameters based on other
    parameters that may have already been set.

    For example,
    ---
    newparam(:param1) do
    Puppet.debug "Found drivesperarray parameter"
    desc "parameter 1"
    validate do |value|
    if resource[:otherparam] then
    #dosomething
    else
    resource[:param1] = 0
    end
    end
    Puppet.debug "Parameter 1 is: #{@resource[:param1]}"
    end
    ---

    But I keep getting messages like "undefined method `[]' for nil:NilClass"

    Anyone have experience with this? I've tried searching around for example
    without much luck...

    Thanks!

    Actually, it turns out that setting "resource[:param1] = 0" invokes the
    validate loop and the whole thing blows up eventually throws "stack level
    too deep" (glad that it didn't let it keep going)

    --
    You received this message because you are subscribed to the Google Groups "Puppet Users" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/bPZFhA3wtNMJ.
    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.
  • Nick Lewis at Jul 31, 2012 at 11:54 pm

    On Tuesday, July 31, 2012 2:05:28 PM UTC-7, ZJE wrote:
    Is it possible to access @resource variables inside a type?

    I would like to make some decisions on parameters based on other
    parameters that may have already been set.

    For example,
    ---
    newparam(:param1) do
    Puppet.debug "Found drivesperarray parameter"
    desc "parameter 1"
    validate do |value|
    if resource[:otherparam] then
    #dosomething
    else
    resource[:param1] = 0
    end
    end
    Puppet.debug "Parameter 1 is: #{@resource[:param1]}"
    end
    ---

    But I keep getting messages like "undefined method `[]' for nil:NilClass"

    Anyone have experience with this? I've tried searching around for example
    without much luck...
    It sounds like what you actually want is a munge block, which is used to
    change the value of the parameter.

    munge do |value|
    if resource[:otherparam] then
    #dosomething
    else

    end
    end

    validate should be used only to raise an exception if the value is invalid.
    Puppet will call validate and then munge. Also, parameters are set in the
    order they're defined in the type/<type>.rb file, and validated/munged
    before moving on to the next parameter. So a parameter can only depend on
    the values of parameters that come *before* it.

    Thanks!
    --
    You received this message because you are subscribed to the Google Groups "Puppet Users" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/vNg7G08OxowJ.
    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.
  • ZJE at Aug 1, 2012 at 12:46 pm

    On Tuesday, July 31, 2012 6:54:14 PM UTC-5, Nick Lewis wrote:
    On Tuesday, July 31, 2012 2:05:28 PM UTC-7, ZJE wrote:

    Is it possible to access @resource variables inside a type?

    I would like to make some decisions on parameters based on other
    parameters that may have already been set.

    For example,
    ---
    newparam(:param1) do
    Puppet.debug "Found drivesperarray parameter"
    desc "parameter 1"
    validate do |value|
    if resource[:otherparam] then
    #dosomething
    else
    resource[:param1] = 0
    end
    end
    Puppet.debug "Parameter 1 is: #{@resource[:param1]}"
    end
    ---

    But I keep getting messages like "undefined method `[]' for nil:NilClass"

    Anyone have experience with this? I've tried searching around for example
    without much luck...
    It sounds like what you actually want is a munge block, which is used to
    change the value of the parameter.

    munge do |value|
    if resource[:otherparam] then
    #dosomething
    else

    end
    end

    validate should be used only to raise an exception if the value is
    invalid. Puppet will call validate and then munge. Also, parameters are set
    in the order they're defined in the type/<type>.rb file, and
    validated/munged before moving on to the next parameter. So a parameter can
    only depend on the values of parameters that come *before* it.

    Thanks!
    Hi Nick,

    You're exactly right - even though I had read about munge, I completely
    forgot it and must've missed it a million times in the documentation.

    Thanks so much!

    --
    You received this message because you are subscribed to the Google Groups "Puppet Users" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/9qbvu_qT-jQJ.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppuppet-users @
categoriespuppet
postedJul 31, '12 at 9:05p
activeAug 1, '12 at 12:46p
posts4
users2
websitepuppetlabs.com

2 users in discussion

ZJE: 3 posts Nick Lewis: 1 post

People

Translate

site design / logo © 2022 Grokbase