1

Aim

The aim is to reuse hiera arrays to avoid code duplication.

Attempts

The following array has been defined in a hiera.yaml file:

hiera_array: 
  - host1
  - host2
  - host3

In an attempt to reuse it, the hiera_array is set to be used by another hiera array:

reuse::hiera_array:
  - "%{hiera('hiera_array')}"

Expected outcome

There should be no discrepancy between the files if hiera_array or reuse::hiera_array is called:

filename.erb

<%= scope.function_hiera(['reuse::hiera_array']).join("\n") %>

/path/to/filename

host1
host2
host3

Current outcome

However the filename.erb template that calls the array returns:

/path/to/filename

-host1
-host2
-host3
+["host1, host2, host3"]

Removing the join from the templates indicates that the looked up hiera array looks different:

["[\"host1\", \"host2\", \"host3\"]"] vs. ["host1", "host2", "host3"]

Discussion

It looks like that the hiera_array can be reused as ["host1, host2, host3"] is returned and looks like an array. However the hiera or the erb function <%= scope.function_hiera(['reuse::hiera_array']).join("\n") %> interprets it as a string.

Question

How to reuse Hiera arrays in Puppet?

030
  • 10,842
  • 12
  • 78
  • 123
  • I'm a little baffled by the fact that you pass an Array argument to Hiera in `scope.function_hiera(['reuse::hiera_array'])` and still get a result. Why do you construct an Array parameter? – Felix Frank Jul 31 '15 at 10:13
  • @FelixFrank the `.join("\n")` requires that it is an array. Could you explain how you would solve the issue? I do think that I do not understand the question. – 030 Jul 31 '15 at 12:41
  • The join expects the **result** of the Hiera call to be an array. The **parameter** should (must?) always be a string. I.e. `scope.function_hiera('reuse::hiera_array')`. – Felix Frank Jul 31 '15 at 14:44
  • @FelixFrank I have updated the question. Looking up the hiera array results in a different formatted array. – 030 Aug 04 '15 at 09:03
  • I'm not talking about *removing the join*. I'm talking about removing the brackets. `scope.function_hiera('reuse::hiera_array').join("\n")`. The `[` and `]` are not useful in this context as far as I can tell. The behavior you are observing also makes me wonder wether you should omit the `"` quotes in your YAML **and** why you perform the lookup in yet another array. Try `reuse::hiera_array: %{hiera('hiera_array')}`. – Felix Frank Aug 04 '15 at 12:46
  • @FelixFrank Thank you for the advice. I have tried it and it results in `Error: Could not retrieve catalog from remote server: Error 400 on SERVER: (): found character that cannot start any token while scanning for the next token at line X column Y`. It seems that double quotes are required to lookup the hiera variable. – 030 Aug 04 '15 at 13:26

0 Answers0