1
votes

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']
1
If the reason the second resource is requiring the first is because of the values in the networkUris key, then you could do: require => Oneview_ethernet_network["${v['data']['networkUris']}"]. Is that the logic you are attempting to code for here? - Matt Schuchard
Yes thank you. But in some cases I have to require another resource like require => Oneview_fc_network['SAN_A']. I was thinking about a more generic method to search in my data if a resource from networkUris exists. - rico_nem
If "a more generic method" means embedding a resource reference in your hiera data then I urge you to think differently. Such a thing would constitute a much tighter coupling between data and Puppet code than I would advise. - John Bollinger
Thanks for your advise. We are differentiate them too but in my case our current config file has no additional logic for create, delete or update a single resource if this resource required to others. Everything I want is some safety that a puppet apply don't crash if a resource has wrong dependencies. Maybe a dependency checker of the hiera data is a solution before a puppet apply. -> As said before I am a puppet rookie. - rico_nem

1 Answers

0
votes

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.