0
votes

puppet version: 4.9.4 hiera version: 3.3.1

What i am trying to do is have httpd reload when a new version of package-x/y is installed, and it doesn't seem like the array from Hiera is being passed correctly.

for my httpd.pp file, I have:

class service::common::httpd (
  $service_state = undef, # undef = unmanaged
  $service_run_at_boot = undef,
  $packages = undef
  ) {
    service { 'httpd':
      ensure     => $service_state,
      enable     => $service_run_at_boot,
      subscribe  => $packages,
      restart    => "/usr/sbin/apachectl graceful"
    }
  }

and in the yaml file for hiera, I have:

service::common::httpd::packages: [Package['package-x'],Package['package-y']]

running puppet with this gives the error

Error: Evaluation Error: Error while evaluating a Function Call, Lookup of key 'allow_virtual_packages' failed: Unable to parse (/root/repos/puppet-config/data/nodes/<location of yaml file>): did not find expected ',' or ']' while parsing a flow sequence

also saying that its missing a comma between flow collection entries. I've tried many different combinations of spaces and commas as well..

I have also tried including the packages inside the class with an include statement.

What am i doing wrong?

2
Relevant YAML documentation can be found at yaml.org/spec/1.2/spec.html#id2802662 as well as other deocumentation for YAML syntax.Matt Schuchard
This is not merely a YAML documentation issue as Puppet requires the references in the YAML file to be formatted in a quite unintuitive way, per my answer below. Actually, it is a good question.Alex Harvey

2 Answers

2
votes

The yamllint utility is quite useful for analysing Puppet Hiera YAML files. When I tried it on your file I got:

▶ yamllint spec/fixtures/hiera/data/common.yaml 
spec/fixtures/hiera/data/common.yaml
  2:25      error    syntax error: expected ',' or ']', but got '['
  2:39      error    too few spaces after comma  (commas)

The syntax error there reveals that the file is simply invalid YAML.

But how to fix it?

Confusingly, a line in a Puppet manifest like:

  subscribe => [Package['package-x'], Package['package-y']]

When compiled into a JSON Puppet catalog becomes:

  "subscribe": ["Package[package-x]", "Package[package-y]"]

And you can place the same JSON string in the YAML file to make valid YAML like this:

service::common::httpd::packages: ["Package[package-x]", "Package[package-y]"]

You can also use single quotes in the YAML i.e.

service::common::httpd::packages: ['Package[package-x]', 'Package[package-y]']

More info on how to compile the Puppet catalog in my blog post here.

0
votes

quotes.

hiera doesn't know what Package is. just quote it, since its a string.

service::common::httpd::packages: [ "Package['package-x']", "Package['package-y']" ]

works perfectly.


or you can just change [ "Package['package-x']", "Package['package-y']" ] to [ 'package-x', 'package-y' ]

works flawlessly. see below.

host01.yaml

beats::packetbeat::packages: [ acl, htop ]

packetbeat.pp

class beats::packetbeat (
    $packages = undef
) {
    package {
        "packetbeat":
            ensure    => "$version",
            subscribe => Package[$packages],
        ;
    } 
}

Notice: /Stage[main]/Beats::Packetbeat/Package[acl]/ensure: current_value 'absent', should be '2.2.51-14.el7' (noop)

Notice: /Stage[main]/Beats::Packetbeat/Package[packetbeat]: Would have triggered 'refresh' from 1 event

Notice: /Stage[main]/Beats::Packetbeat/Service[packetbeat]: Would have triggered 'refresh' from 2 events

Notice: Class[Beats::Packetbeat]: Would have triggered 'refresh' from 3 events