Created
September 24, 2021 19:37
-
-
Save nickanderson/6286afb8413d2b1308518cc4a6014712 to your computer and use it in GitHub Desktop.
Example illustrating how to use classes with explicit data when rendering mustache templates in CFEngine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bundle agent render_template_with_explicit_data_and_classes | |
{ | |
classes: | |
"My_Class" scope => "bundle", meta => { "findme" }; | |
vars: | |
# Some data we want to use for rendering a mustache template | |
"_m" data => '{ "MyData": "explicit" }'; | |
# Get current datastate | |
"_s" data => datastate(); | |
# pick out classes from the datastate | |
"_c" data => mergedata( '{ }', "_s[classes]" ); | |
# reparent classes to be a top level key | |
"_c1" data => mergedata( '{"classes": _c}' ); | |
# merge the explicit data on to classes leaving the data container that we | |
# will ultimately hand to the templating engine. | |
"_r" data => mergedata( _c1, _m ); | |
reports: | |
"Without merging in classes from datastate:$(const.n)$(with)" | |
with => string_mustache("My data is {{{MyData}}} and My_Class is {{#classes.My_Class}}defined{{/classes.My_Class}}{{^classes.My_Class}}not defined{{/classes.My_Class}}", _m ); | |
"With merging in classes from datastate:$(const.n)$(with)" | |
with => string_mustache("My data is {{{MyData}}} and My_Class is {{#classes.My_Class}}defined{{/classes.My_Class}}{{^classes.My_Class}}not defined{{/classes.My_Class}}", _r ); | |
"The contents of _r: $(with)" | |
with => storejson( _r ); | |
} | |
bundle agent __main__ | |
{ | |
methods: | |
"render_template_with_explicit_data_and_classes"; | |
} | |
# R: Without merging in classes from datastate: | |
# My data is explicit and My_Class is not defined | |
# R: With merging in classes from datastate: | |
# My data is explicit and My_Class is defined | |
# R: The contents of _r: { | |
# "MyData": "explicit", | |
# "classes": { | |
# "127_0_0_1": true, | |
# "16_cpus": true, | |
# "172_17_0_1": true, | |
# "172_27_224_146": true, | |
# "192_168_33_1": true, | |
# "192_168_42_232": true, | |
# "64_bit": true, | |
# "Afternoon": true, | |
# "Day24": true, | |
# "Friday": true, | |
# "GMT_Day24": true, | |
# "GMT_Evening": true, | |
# "GMT_Friday": true, | |
# "GMT_Hr19": true, | |
# "GMT_Hr19_Q3": true, | |
# "GMT_Lcycle_2": true, | |
# "GMT_Min35_40": true, | |
# "GMT_Min36": true, | |
# "GMT_Q3": true, | |
# "GMT_September": true, | |
# "GMT_Yr2021": true, | |
# "Hr14": true, | |
# "Hr14_Q3": true, | |
# "Lcycle_2": true, | |
# "Min35_40": true, | |
# "Min36": true, | |
# "My_Class": true, | |
# "PK_SHA_6565a8e647e61e4a7ff2c709e0fe772acce2e45aaa294b2bb713de0ba5a6d8c3": true, | |
# "Q3": true, | |
# "September": true, | |
# "Yr2021": true, | |
# "agent": true, | |
# "any": true, | |
# "cfengine": true, | |
# "cfengine_3": true, | |
# "cfengine_3_19": true, | |
# "cfengine_3_19_0a": true, | |
# "cfengine_3_19_0a_8c6b2b326": true, | |
# "compiled_on_linux_gnu": true, | |
# "debian": true, | |
# "debian_bullseye": true, | |
# "enterprise": true, | |
# "enterprise_3": true, | |
# "enterprise_3_19": true, | |
# "enterprise_3_19_0a": true, | |
# "enterprise_3_19_0a_5b80a189c": true, | |
# "enterprise_edition": true, | |
# "feature": true, | |
# "feature_curl": true, | |
# "feature_def": true, | |
# "feature_def_json": true, | |
# "feature_def_json_preparse": true, | |
# "feature_host": true, | |
# "feature_host_specific": true, | |
# "feature_host_specific_data": true, | |
# "feature_host_specific_data_load": true, | |
# "feature_tls": true, | |
# "feature_tls_1": true, | |
# "feature_tls_1_0": true, | |
# "feature_tls_1_1": true, | |
# "feature_tls_1_2": true, | |
# "feature_tls_1_3": true, | |
# "feature_xml": true, | |
# "feature_yaml": true, | |
# "have_aptitude": true, | |
# "inform_mode": 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_172_27": true, | |
# "ipv4_172_27_224": true, | |
# "ipv4_172_27_224_146": true, | |
# "ipv4_192": true, | |
# "ipv4_192_168": true, | |
# "ipv4_192_168_33": true, | |
# "ipv4_192_168_33_1": true, | |
# "ipv4_192_168_42": true, | |
# "ipv4_192_168_42_232": true, | |
# "ipv4_gw_172_27_224_129": true, | |
# "ipv4_gw_192_168_42_1": true, | |
# "linux": true, | |
# "linux_5_14_1_051401_generic": true, | |
# "linux_x86_64": true, | |
# "linux_x86_64_5_14_1_051401_generic": true, | |
# "linux_x86_64_5_14_1_051401_generic__202109030936_SMP_Fri_Sep_3_09_45_45_UTC_2021": true, | |
# "mac_00_00_00_00_00_00": true, | |
# "mac_02_42_3f_46_7c_ce": true, | |
# "mac_0a_00_27_00_00_00": true, | |
# "mac_90_2e_16_33_11_53": true, | |
# "net_iface_docker0": true, | |
# "net_iface_eno1": true, | |
# "net_iface_lo": true, | |
# "net_iface_tun0": true, | |
# "net_iface_vboxnet0": true, | |
# "nickanderson_legion_7_16achg6": true, | |
# "nova": true, | |
# "nova_3": true, | |
# "nova_3_19": true, | |
# "nova_3_19_0a": true, | |
# "nova_3_19_0a_5b80a189c": true, | |
# "nova_edition": true, | |
# "systemd": true, | |
# "ubuntu": true, | |
# "ubuntu_21": true, | |
# "ubuntu_21_04": true, | |
# "x86_64": true | |
# } | |
# } | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment