puppetlookuphiera

Lookup function does not work within the Profile for Puppet


I am getting the following error in the event viewer on my node during a puppet run. I suspect the issue is with incorrect lookup function in my profile.

Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Resource Statement, Scconfig::Coserveradmin[SomeSettings]:
parameter 'parameterA' expects a String value, got Undef
parameter 'parameterB' expects a String value, got Undef
parameter 'parameterC' expects a String value, got Undef
parameter 'parameterD' expects a String value, got Undef

coserveradmin is a define resource with all string parameters. I would like to do lookup of values from a Json file

{
    "SASettings" : {
        "Watchdog" : {
            "ParameterA"            : "somevalue",
            "ParameterB"            : "somevalue"
        },
        "Serversettings" : {
            "ParameterC"            : "somevalue",
            "ParameterD"            : "somevalue",

        },
        "GeneralSettings" : {
            "ParameterE"            : "somevalue",
            "ParameterF"            : "somevalue",

        },
        "customsettings_prod" : {
            "ParameterG"            :"somevalue",
            "ParameterH"            : "%{facts.hostname}.example-cloud.com"
        },
        "customsettings_dev" : {
            "ParameterI"            :"",
            "ParameterK"            : "%{facts.hostname}.example.net"
        }
    }
}

In my hiera.yaml file I have defined the name and path to the json file.

- name: "Desired Some Settings"
    path: "default/serveradmin.json" 

In profile I have the following code .

    class profile::scconfig_someprofile_a {
                .
                .
                .
                $hname= $::facts['hostname']
                $mac= "${facts['macaddress'].delete(':')}"
                $adminpropeties = lookup('SASettings')

               if $hname=~someregex {
                           scconfig::coserveradmin{ 'SomeSettings':
                           property1 => $adminpropeties['customsettings_prod.ParameterG'],
                           property2 => $adminproperties['Watchdog.ParameterA'],
                           property3 => $adminproperties['Watchdog.ParameterB'],
                           property4 => $adminproperties['Serversettings.ParameterC'],
                           .
                           .
                           .
                           .
                           and so on
                           .
                           macaddress => $mac,

               }
               elsif $hname=~someregex {
                           scconfig::coserveradmin{ 'SomeSettings':
                           property1 => $adminpropeties['customsettings_dev.ParameterI'],
                           property2 => $adminproperties['Watchdog.ParameterA'],
                           property3 => $adminproperties['Watchdog.ParameterB'],
                           property4 => $adminproperties['Serversettings.ParameterC'],
                           .
                           .
                           .
                           .
                           and so on
                           .
                           macaddress => $mac,

               }

Also adding the code for the "define" resource as requested.

define scconfig::coserveradmin(
    String $Property1,
    String $Property2,
    String $Property3,
    String $Property4,
    .
    .
    .
    String $macaddress,

  ) {

  $dscmoduleversion = lookup('requires.modules.codsc.version')
  if $dscmoduleversion != '' {
      $module = {
          'name'    => 'codsc',
          'version' => $dscmoduleversion,
      }
  }else{
      $module = 'codsc'
  }

  $configname1='someconfig1'
  $configname2='someconfig2'
  $configname3='someconfig3'

  dsc { 'someconfig1':
    require       => lookup('requires.cloudopssoftware'),
    resource_name => 'Someresourcename',
    module        => $module,
    properties    => {
      configname         => $configname1,
      Prop1   => $Property1,
      Prop2   => $Property2,
      Prop3   =>$Property3,
    },
  }
  dsc { 'someconfig2':
   require       => lookup('requires.cloudopssoftware'),
   resource_name => 'someresourcename2',
   module        => $module,
   properties    => {
      configname    => $configname2,
            Prop1   => $Property4,
            Prop2   => $Property5,
            Prop3   =>$Property6,
    },
  }

 dsc { 'someconfig3':
   require       => lookup('requires.cloudopssoftware'),
   resource_name => 'someresourcename3',
   module        => $module,
   properties    => {
      configname    => $configname3,
            Prop1   => $Property6,
            Prop2   => $Property7,
            Prop3   =>$Property8,
            .
            .
            .
            Propn   => $macaddress
         },
       }

Please note that the last property which is the macaddress is evaluated within the profile class therefore I don't see any error for it.

Any ideas what could be the issue.


Solution

  • I suspect the issue is with incorrect lookup function in my profile.

    That does not appear to be the case. If your lookup() call were not successfully looking up and returning a hash then you would get a different error when you tried to extract values.

    I guess it's possible that you're retrieving the wrong hash -- which would be a matter of your hiera configuration and / or data, not the lookup() call itself -- but whether it's the right hash or the wrong one, the syntax you are trying to use to extract the data from it is not matched to the hash structure presented in the question. For example, this expression

    $adminpropeties['customsettings_prod.ParameterG']
    

    attempts to retrieve the value whose key is 'customsettings_prod.ParameterG', but the data presented contain no such key.

    What you seem to want is

     $adminpropeties['customsettings_prod']['ParameterG']
    

    That extracts the value having key 'customsettings_prod', and, that value being a hash itself, extracts its value associated with key 'ParameterG'.

    Alternatively, you may find the dig() function convenient for extracting data from nested data structures such as yours:

    dig($adminpropeties, 'customsettings_prod', 'ParameterG')