On 06/06/16 15:52, Robert Poulson wrote:
Dear Henrik,
dear Luke,

thank you to both of you!

I use 4.x but like the second solution too. Went with the first one tho.

Since then I realised that I'm actually using hashes of hashes... so
hashes more like in a dictionary way. So the following:

$input_base => {
  input1 => { 'port' => '2001', 'component' => 'component1' },
}

with this:

$input_merged.each |$input, $options| {
  file { "/path/to/project_${input}.conf":
  ... }

would yield filenames like "/path/to/project_{ 'port' => '2001',
'component' => 'component1' }.conf" since that is the value of that
key:value pair!

And iterating through this key:value pair (in a nested .each) is not
easy either, because for the .each function, the key is once "port" and
value is "2001", then the key will be "component" and the value will be
"component1". So I'd have two configfiles for every inputN.


You could have accessed $input[port] and $input[component] in your path
(title), and where you needed those values). You changed data structure is probably better (and ok) since port is probably unique anyway so you don't need the extra indirection of naming the inputs.

- henrik

I simplified the the input format to:

$input_base => {
  '2001' => 'component1',
  '2002 => 'component2',
}

and now I can simply iterate them over with a single .each.

I'm clearly missing the developer mindset (yet... I hope) and have to
learn some basics as well for every task I solve.

Thank you,
Rob

On Mon, Jun 6, 2016 at 10:21 AM, Luke Bigum <[email protected]
<mailto:[email protected]>> wrote:

    In Puppet 3.x you have to use the Define Wrapper "trick". It's not
    pretty, but without lambda functions it's all that you've got. If
    it's 4.x, see Henrik's post before.




    $input_base => {
      input1 => { 'port' => '2001', 'component' => 'component1' },
      input2 => { 'port' => '2002', 'component' => 'component2' }
    }
    $input_node => {
      input3 => { 'port' => '2003', 'component' => 'component3' },
      [possibly input4... inputN]
    }
    $input_merged = merge($input_base, $input_node)
    file { 'input_merged_file':
      content => template("template_that_uses_input_merged")
    }

    #Uses $port and $component params in individual File resources
    define input_node_wrapper ($port, $component) {
      file { "a_file_for_input_node_${name}":
        path    => 'somewhere',
        content => 'some_other_template',
      }
    }
    create_resources('input_node_wrapper', $input_merged)



    --
    Luke Bigum
    Senior Systems Engineer

    Information Systems

    ----- Original Message -----
    From: "Robert Poulson" <[email protected] <mailto:[email protected]>>
    To: "puppet-users" <[email protected]
    <mailto:[email protected]>>
    Sent: Sunday, 5 June, 2016 18:56:48
    Subject: [Puppet Users] merge hashes and create_resources

    Dear List,

    I've been using Puppet for over a year now and I'm quite enjoying
    it. I've
    learned some stuff but there is of course always room for
    improvement. Now
    I have a task which needs a nicer solution than I'm currently
    capable of.

    I have a hash of items with to key/value pairs, which is *the same for
    every node*:

    $input_base => {
      input1 => { 'port' => '2001', 'component' => 'component1' },
      input2 => { 'port' => '2002', 'component' => 'component2' }
    }

    Then I have a second hash, which is different for every node.

    $input_node => {
      input3 => { 'port' => '2003', 'component' => 'component3' },
      [possibly input4... inputN]
    }

    These all will be used in a single template. So I can simply do:

    $input_merged = merge($input_base, $input_node)

    The corresponding port/component entries will then be added in a
    configuration file with an $input_merged.each_pair - so far so good.



    Now the actual task: I need an extra configfile, generated from a
    template,
    for all the inputN elements of the $input_node - but only for them,
    not for
    the $input_base elements - like this:

    * /path/to/project_input3.conf
    * [/path/to/project_input4.conf...project_inputN.conf]

    This would be possible with create_resources:

    create_resources(file, $input_merged)

    but in order to do this, $input_merged should have the values of a file
    resource - at least "path" and "source => template()". This is not
    the case.

    I could define $input_node initially as a file resource hash - but
    in this
    case I can't merge it anymore with $input_base.

    Currently I have no other idea than manually map the input_node elements
    into a third hash, and use that with create_resources, but there
    should be
    a nicer solution. Do you have an idea? :-)


    Best,
    Rp


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

--
You received this message because you are subscribed to the Google Groups "Puppet 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/3f945621-0c53-0959-d4ed-c0743da439db%40puppet.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to