1

Hello everybody,

When I do puppet apply, I get the error:

Warning: Scope(Mod::Cl[title]): Can't load '/tmp/file.yaml' File does not exist!
Error: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Operator '[]' is not applicable to an Undef Value.

I specify that loadyaml is a function of stdlib module

However, i want to execute file resource before define resource

Someone can help me ?

class mod::princ (

    file { '/tmp/file.yaml':
        ensure  => file,
        owner   => 'root',
        group   => 'root',
        mode    => '0644',
        source  => "puppet:///modules/othermod/file.yaml",
        before => Mod::Cl["title"],
    }->
    Mod::Cl{ "title" :
        tmp_file => "/tmp/file.yaml",
    }

}

define mod::cl (
    String $tmp_file,
){

    $tmp = loadyaml("$tmp_file")

    $tmp[var].each |Integer $i, Hash[String,String] $var|
    {
      $mark=$var['Mark']
    }

}
souf
  • 11
  • 3
  • Are you using a Puppet master? – Craig Watson May 21 '17 at 17:36
  • Thanks for the answer, with an apply or a Puppet master, I get the same error – souf May 21 '17 at 18:00
  • Puppet first compiles the manifest and then applies it, but `loadyaml` (and other functions) run at compile time, while resources like `file` run at apply time. Read the file directly from the module (`othermod/file.yaml`), else explain what you're trying to achieve so we can help better - `mod::cl` doesn't appear to do anything. – Dominic Cleal May 22 '17 at 08:31
  • Hello Dominic, thanks for the answer. In fact, I read the file yaml and rewrite another one from the define mod::cl. I already tried to read the file directly from the module (othermod/file.yaml), but it does not work. – souf May 22 '17 at 13:15

1 Answers1

0

As Dominic mentioned in his comment, the loadyaml function is run at compile-time.

If you are running Puppet via a Puppet Master/Server and puppet agent, this happens on the server, before the manifest is sent to the client, not on the client itself.

In addition, the loadyaml function is run at compile, not execution - so the file needs to exist before the Puppet run takes place.

I'd query why you're taking this approach with your Puppet code - you don't really explain what the code is for, or what your intended end-state looks like.

You can get the same results using Hiera.

hieradata/common.yaml

---
mod::princ::data:
  entry:
    foo: bar
  yrtne:
    oof: rab

modules/mod/manifests/princ.pp

class mod::princ (
  Hash $data
) {

  $data.each |Integer $i, Hash $elem, $var | {
    $mark=$var['Mark']
  }

}
Craig Watson
  • 9,575
  • 3
  • 32
  • 47