Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-3124

Mustache templates should allow iteration over CFEngine classic arrays from datastate

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: To Do
    • Priority: Medium
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: cf-agent
    • Labels:
      None
    • Found in version (details):
      3.12.2

      Description

      Example policy:

      bundle agent main {
      
        methods:
          any::
            "json_data";
            "array_data";
            "manage_files";
      
      }
      
      bundle agent json_data {
      
        vars:
          any::
            "phone_prices"
              data => '
                {
                  "Pixel" : "$800",
                  "Galaxy": "$700",
                  "iPhone": "$999",
                }';
      
      }
      
      bundle agent array_data {
      
        vars:
          any::
      
            "phone_prices[Pixel]"
              string => "$800";
      
            "phone_prices[Galaxy]"
              string => "$700";
      
            "phone_prices[iPhone]"
              string => "$999";
      
      }
      
      bundle agent manage_files {
      
        files:
          any::
      
            "/tmp/from_json_with_explicit_data"
              create => "true",
              template_method => "inline_mustache",
              template_data => @(json_data.phone_prices),
              edit_template_string => "{{#-top-}}
      {{@}} is {{.}}
      {{/-top-}}";
      
            "/tmp/from_json_with_datastate"
              create => "true",
              template_method => "inline_mustache",
              edit_template_string => "{{#vars.json_data.phone_prices}}
      {{@}} is {{.}}
      {{/vars.json_data.phone_prices}}";
      
            "/tmp/from_array_with_explicit_data"
              create => "true",
              template_method => "inline_mustache",
              template_data => @(array_data.phone_prices),
              edit_template_string => "{{#-top-}}
      {{@}} is {{.}}
      {{/-top-}}";
      
            "/tmp/from_array_with_datastate"
              create => "true",
              template_method => "inline_mustache",
              edit_template_string => "{{#vars.array_data.phone_prices}}
      {{@}} is {{.}}
      {{/vars.array_data.phone_prices}}";
      
      }
      

      Results:

      [root@cfenginehub extra-examples]# cat /tmp/from_json_with_explicit_data
      Pixel is $800
      Galaxy is $700
      iPhone is $999
      [root@cfenginehub extra-examples]# cat /tmp/from_json_with_datastate
      Galaxy is $700
      Pixel is $800
      iPhone is $999
      [root@cfenginehub extra-examples]# cat /tmp/from_array_with_explicit_data
      iPhone is $999
      Pixel is $800
      Galaxy is $700
      [root@cfenginehub extra-examples]# cat /tmp/from_array_with_datastate
      [root@cfenginehub extra-examples]#
      

      The last of the four files promises should work as well as the others.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              mweilgart Mike Weilgart
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:

                Summary Panel