Search code examples
rubypuppetpuppet-enterprisehiera

auto-create require parameters by data in hiera


Is there a way to create the require parameter by hiera? Maybe it's possible to lookup but I am new to puppet and don't know all possibilities.

I am using the oneview-puppet module to create resources from a puppet apply.

The resources were created by hiera defined as one config file (YAML). There I am combining several resources from the module above. These resources have complicated dependencies. An overview can be found here (page 29).

So for each resource I have to require the dependencies although it could "found" in my config file. Actual it only works when the resources created by it's sequence in the site/manifest/init.pp.

I've tried to add the require paremeter in hiera, but there it will be interpreted as a string.

site/oneviewconf/manifest/init.pp example:

class oneviewconf (
  Hash $oneview_ethernet_networks = {},
  Hash $oneview_logical_interconnect_groups = {}
)
{
  $oneview_ethernet_networks.each | $k,$v | {
    oneview_ethernet_network { $k:                      # -> oneview-puppet resource
      * => $v,
    }
  }
  $oneview_logical_interconnect_groups.each | $k,$v | {
    oneview_logical_interconnect_group { $k:             # -> oneview-puppet resource
      require => Oneview_ethernet_network['VLAN0001']
      * => $v,
    }
  }
}

Hiera example:

---
oneviewconf::oneview_ethernet_networks:
  VLAN0001:
    ensure: present
    data:
      name: 'VLAN0001'
      vlanId: 0001
oneviewconf::oneview_logical_interconnect_groups:
  LIG_A:
    ensure: present
    data:
      name: 'LIG_A'
      networkUris: ['VLAN0001']

Solution

  • Is there a way to create the require parameter by hiera?

    Yes.

    I've tried to add the require parameter in hiera, but there it will be interpreted as a string.

    Not if you format it correctly. If you look at a compiled Puppet catalog you can see how resource references are encoded in a JSON catalog and this also tells you how they would need to be encoded in a Hiera YAML file.

    Take a manifest like this:

    class test {
      notify { 'notify1':
        message => 'I am notify 1',
        require => Notify['notify2'],
      }
      notify { 'notify2':
        'message' => 'I am notify 2',
      }
    }
    

    Now compile that catalog and look inside it. You will see:

        {
          "type": "Notify",
          "title": "notify1",
    ...
          "parameters": {
            "message": "I am notify 1",
            "require": "Notify[notify2]"
          }
        },
    

    In case that's not obvious, whereas the manifests require the resource title to be quoted like Notify['notify2'] those quotes around the resource title are deleted in the catalog and it becomes Notify[notify2].

    Thus I can add a parameter to Hiera the same way, and refactor the whole thing like this.

    Hiera:

    ---
    notify_resources:
      notify1:
        message: I am notify 1
        require: Notify[notify2]
      notify2:
        message: I am notify 2
    

    Manifests:

    class test {
      $notify_resources = lookup('notify_resources')
      $notify_resources.each |$k,$v| {
        notify { $k: * => $v }
      }
    }
    

    Should you do this though? I tend to agree with John Bollinger's comments that resource references in Hiera might be a clue that you have too much data/code coupling.