puppet

Array / list in puppet plan cannot be converted to Numeric


We have old puppet master running version 2021.7

I have a list / array in a puppet plan, and I want to join this with another list.

For some reason, this is giving me the following error when running the puppet plan:

"msg" : "Evaluation Error: The value '[{\"key\":\"MY_KEY1\",\"value\":\"my_value\",\"protected\":true},{\"key\":\"MY_KEY2\",\"value\":\"my_value\",\"protected\":true},{\"key\":\"MY_KEY3\",\"value\":\"my_value\",\"protected\":false}]' cannot be converted to Numeric.

The code in question:

$env_types = ['ci', 'qa01']
$cicd_variables = $project_details['cicd_variables'].to_json

$env_types.each |$env_type| {
  # env_types variables
  $env_type_uc = $env_type.upcase

  $env_variables = [
    {
      key       => "${env_type_uc}_ID",
      value     => "123",
      protected => true,
    },
    {
      key       => "${env_type_uc}_VAR2",
      value     => "test",
      protected => true,
    },
    {
      key       => "${env_type_uc}_ENV_VARS",
      value     => "test",
      protected => false,
    }
  ]

  # Merge original + new env_variables
  $all_variables = $cicd_variables + $env_variables
  $cicd_variables_final = $all_variables.to_json
}

I've been breaking my head over this for a couple of days. Been searching on the Puppet wiki, but imo, it's poorly documented.

I've been asking chatgpt and other AI tools the same question. But I cannot seem to find the correct answer.

If anybody can help me with this, many thanks!


Solution

  • The + operator can add two Numerics, concatenate two Arrays, or merge two Hashes. In this case your two operands are of conflicting types String and Array, and so Puppet throws the error that that it cannot convert the first operand to a Numeric because it first attempts the arithmetic operation with the + operator. A more sensible error would probably have been that it could not convert the first operand to an Array, but note that the documentation confirms this behavior:

    If the left operand isn't an array, Puppet interprets + as arithmetic addition.

    If you retain the first operand as a List instead of converting it to a String then it should function as expected:

    $all_variables = $project_details['cicd_variables'] + $env_variables