template_data and 'global' classes

38 views
Skip to first unread message

Tina Friedrich

unread,
Oct 1, 2018, 12:36:08 PM10/1/18
to help-cfengine
Hello,

does / should using 'template_data' restrict access to 'global' classes? I
seemed to remember it shouldn't (and it should work), but can't find in the
docs at the moment. I found a template expansion going wrong because it didn't
pick up a class that's definitely defined (the template itself has a
{{#classes.X}} section, as well as data expansion stuff). (When I took the
'template_data' out of the template expansion, it all worked, so I'm pretty
positive my class gets defined - it's defined in a common bundle).

Tina

Nick Anderson

unread,
Oct 1, 2018, 12:54:13 PM10/1/18
to help-cfengine
Hi Tina,

Yes, in mustache templates if you provide template data you are only allowed to use the provided data with templates.

If you do not provide explicit data then datastate() is used.


Tina Friedrich

unread,
Oct 1, 2018, 12:58:10 PM10/1/18
to help-c...@googlegroups.com
Thanks Nick. I obviously didn't quite parse that as classes will also not be
available'. Would *any* classes be available when passing template_data? (Just
wondering; I think I might be referencing things like redhat_7 class in the
odd template, and it seems to me that I need to very carefully look at that
again. Or just not ever user 'template_data' :) )

Tina

Nick Anderson

unread,
Oct 1, 2018, 1:36:33 PM10/1/18
to help-cfengine


On Monday, October 1, 2018 at 11:58:10 AM UTC-5, Tina Friedrich wrote:
Thanks Nick. I obviously didn't quite parse that as classes will also not be
available'. Would *any* classes be available when passing template_data? (Just
wondering; I think I might be referencing things like redhat_7 class in the
odd template, and it seems to me that I need to very carefully look at that
again. Or just not ever user 'template_data' :) )


No, only classes that you have explicitly made part of the data provided.

datastate() produces a json structure of the vars and classes that cfengine knows about in the format:

{
  "classes": {
    "classname": true,
    "redhat_7": true
  }
  "vars": {
    "bundlename": {
      "variablename": "value"
    },
    "sys": {
      "os": "linux"
  }
}

This would let you use classes.redhat_7 and vars.sys.os.




Ted Zlatanov

unread,
Oct 7, 2018, 7:48:19 PM10/7/18
to help-c...@googlegroups.com
On Mon, 1 Oct 2018 10:36:33 -0700 (PDT) Nick Anderson <nick.a...@northern.tech> wrote:

NA> No, only classes that you have explicitly made part of the data provided.

NA> datastate() produces a json structure of the vars and classes that cfengine
NA> knows about in the format:
...
NA> This would let you use classes.redhat_7 and vars.sys.os.

As another idea, you could capture datastate() in container D and then
use mergedata() to mix D.classes into your desired template_data (making
a new container or on the fly).

Ted

Nick Anderson

unread,
Oct 8, 2018, 9:50:03 AM10/8/18
to Ted Zlatanov, help-c...@googlegroups.com

Ted Zlatanov <t...@lifelogs.com> writes:

As another idea, you could capture datastate() in container D and then

use mergedata() to mix D.classes into your desired templatedata (making


a new container or on the fly).

Yes, thanks for saying what I apparently omitted. Here is an example
that shows merging explicit data with classes.

bundle agent main
{
   vars:

     "m" data => '{ "MyData": "explicit" }';

     # Get current datastate, pick classes, reparent classes, merge explicit
     # data on to classes

     "s" data => datastate(), if => not( isvariable ( s ) );
     "c" data => mergedata( '{ }', "s[classes]" );
     "c1" data => mergedata( '{"classes": c}' );
     "r" data => mergedata( c1, m );

   reports:
     "$(with)"
       with => string_mustache("My data is {{{MyData}}} and I am running CFEngine {{#classes.enterprise_edition}}Enterprise{{/classes.enterprise_edition}}{{^classes.enterprise_edition}}Community{{/classes.enterprise_edition}}", r );

     "The full data container:$(const.n)$(with)"
       with => string_mustache("{{%-top-}}", r );
}
R: My data is explicit and I am running CFEngine Enterprise
R: The full data container:
{
  "MyData": "explicit",
  "classes": {
    "127_0_0_1": true,
    "172_17_0_1": true,
    "192_168_0_1": true,
    "192_168_100_1": true,
    "192_168_10_1": true,
    "192_168_121_1": true,
    "192_168_122_1": true,
    "192_168_42_189": true,
    "4_cpus": true,
    "64_bit": true,
    "Day8": true,
    "GMT_Afternoon": true,
    "GMT_Day8": true,
    "GMT_Hr13": true,
    "GMT_Hr13_Q4": true,
    "GMT_Lcycle_2": true,
    "GMT_Min45_50": true,
    "GMT_Min49": true,
    "GMT_Monday": true,
    "GMT_October": true,
    "GMT_Q4": true,
    "GMT_Yr2018": true,
    "Hr08": true,
    "Hr08_Q4": true,
    "Hr8": true,
    "Lcycle_2": true,
    "Min45_50": true,
    "Min49": true,
    "Monday": true,
    "Morning": true,
    "October": true,
    "PK_SHA_43c979e264924d0b4a2d3b568d71ab8c768ef63487670f2c51cd85e8cec63834": true,
    "Q4": true,
    "Yr2018": true,
    "agent": true,
    "any": true,
    "cfengine": true,
    "cfengine_3": true,
    "cfengine_3_12": true,
    "cfengine_3_12_0": true,
    "compiled_on_linux_gnu": true,
    "debian": true,
    "debian_buster": true,
    "enterprise": true,
    "enterprise_3": true,
    "enterprise_3_12": true,
    "enterprise_3_12_0": true,
    "enterprise_edition": true,
    "fe80__5ee0_c5ff_fe9f_f38f": true,
    "fe80__fc54_ff_fe61_1523": true,
    "fe80__fc54_ff_fe7a_cef0": true,
    "fe80__fc54_ff_fef0_9262": true,
    "fe80__fc54_ff_fef8_3bda": true,
    "feature": true,
    "feature_curl": true,
    "feature_def": true,
    "feature_def_json": true,
    "feature_def_json_preparse": true,
    "feature_xml": true,
    "feature_yaml": true,
    "ipv4_127": true,
    "ipv4_127_0": true,
    "ipv4_127_0_0": true,
    "ipv4_127_0_0_1": true,
    "ipv4_172": true,
    "ipv4_172_17": true,
    "ipv4_172_17_0": true,
    "ipv4_172_17_0_1": true,
    "ipv4_192": true,
    "ipv4_192_168": true,
    "ipv4_192_168_0": true,
    "ipv4_192_168_0_1": true,
    "ipv4_192_168_10": true,
    "ipv4_192_168_100": true,
    "ipv4_192_168_100_1": true,
    "ipv4_192_168_10_1": true,
    "ipv4_192_168_121": true,
    "ipv4_192_168_121_1": true,
    "ipv4_192_168_122": true,
    "ipv4_192_168_122_1": true,
    "ipv4_192_168_42": true,
    "ipv4_192_168_42_189": true,
    "ipv4_gw_192_168_42_1": true,
    "ipv6_fe80__5ee0_c5ff_fe9f_f38f": true,
    "ipv6_fe80__fc54_ff_fe61_1523": true,
    "ipv6_fe80__fc54_ff_fe7a_cef0": true,
    "ipv6_fe80__fc54_ff_fef0_9262": true,
    "ipv6_fe80__fc54_ff_fef8_3bda": true,
    "linux": true,
    "linux_4_15_0_33_lowlatency": true,
    "linux_x86_64": true,
    "linux_x86_64_4_15_0_33_lowlatency": true,
    "linux_x86_64_4_15_0_33_lowlatency__36_Ubuntu_SMP_PREEMPT_Wed_Aug_15_17_20_28_UTC_2018": true,
    "mac_02_42_d2_da_e3_92": true,
    "mac_52_54_00_3c_1e_b7": true,
    "mac_52_54_00_6b_62_06": true,
    "mac_52_54_00_7d_55_73": true,
    "mac_52_54_00_9b_e8_bd": true,
    "mac_52_54_00_ff_1d_f1": true,
    "mac_5c_e0_c5_9f_f3_8f": true,
    "net_iface_docker0": true,
    "net_iface_lo": true,
    "net_iface_virbr0": true,
    "net_iface_virbr1": true,
    "net_iface_virbr3": true,
    "net_iface_virbr4": true,
    "net_iface_virbr5": true,
    "net_iface_wlan0": true,
    "nickanderson_thinkpad_w550s": true,
    "nova": true,
    "nova_3": true,
    "nova_3_12": true,
    "nova_3_12_0": true,
    "nova_edition": true,
    "systemd": true,
    "ubuntu": true,
    "ubuntu_18": true,
    "ubuntu_18_04": true,
    "ubuntu_18_4": true,
    "x86_64": true
  }
}


Nick Anderson| Doer of Things | (+1) 785-550-1767 | https://northern.tech

Reply all
Reply to author
Forward
0 new messages