Mustache template question

19 views
Skip to first unread message

Bas van der Vlies

unread,
Nov 6, 2020, 10:28:12 AM11/6/20
to help-cfengine
I am want some dynamic templating without duplicationg to much code. It want to iterate over key and use the fields defined for that key, eg:
```
bidirectional_rules_interfaces: {
"cua_admin_interface": {
service: cfengine,
source: admin-lan-grp
},
<more interfaces>
},
```

No i want to loop over the interfaces and uss the keys that are defined in mustache:
```
{{#bidirectional_rules_interfaces}}
allow from {{@.source}} to any port {{{@.service}}} in {{{@}}}
allow from {{cua_admin_interface.source}} to any port {{{cua_admin_interface.service}}} in {{{@}}}
{{/bidirectional_rules_interfaces}}

the second line works because if specify the interface name, but the first does not expand. Is there a feature that allows dynamic
keyword expansion? I would be very useful for a lot of templates.

regards


--
Bas van der Vlies
| Operations, Support & Development | SURFsara | Science Park 140 | 1098 XG Amsterdam
| T +31 (0) 20 800 1300 | bas.van...@surf.nl | www.surf.nl |




Nick Anderson

unread,
Nov 6, 2020, 11:27:43 AM11/6/20
to Bas van der Vlies, help-cfengine

Bas van der Vlies writes:

{{#bidirectional_rules_interfaces}} allow from {{@.source}} to any port {{{@.service}}} in {{{@}}} allow from {{cua_admin_interface.source}} to any port {{{cua_admin_interface.service}}} in {{{@}}} {{/bidirectional_rules_interfaces}} the second line works because if specify the interface name, but the first does not expand. Is there a feature that allows dynamic keyword expansion? I would be very useful for a lot of templates.

Hi Bas,

Does this work for you?

bundle agent main
{
  vars:
    "d" data => '{ "bidirectional_rules_interfaces": {
                  "cua_admin_interface":{                                                                   
            "service": "cfengine",
            "source":  "admin-lan-grp"
    },
    "cua_admin_interface2":{                                                                   
            "service": "cfengine2",
            "source":  "admin-lan-grp2"
    }}}';
  reports:
    "$(with)" with => string_mustache( "Iterating over dict of dicts:$(const.n){{#bidirectional_rules_interfaces}} Interface = {{@}}, source = {{.source}}$(const.n){{/bidirectional_rules_interfaces}}", d );

}
R: Iterating over dict of dicts:
 Interface = cua_admin_interface, source = admin-lan-grp
 Interface = cua_admin_interface2, source = admin-lan-grp2

Bas van der Vlies

unread,
Nov 6, 2020, 12:21:25 PM11/6/20
to Nick Anderson, help-cfengine


Nick,

Thanks a lot. I did not knew about the {{{.<key>}}} constuct. I life saver ;-)

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

Nick Anderson

unread,
Nov 6, 2020, 12:37:27 PM11/6/20
to help-cfengine

Yay :thumbsup:

Bas van der Vlies

unread,
Nov 6, 2020, 3:22:17 PM11/6/20
to Nick Anderson, help-cfengine
Nick maybe you have also the answer for thiis one:
````
{
bidirectional_rules_interfaces: {
cua_admin_interface: {
"rules": [
{
service: cfengine,
source: ldap-admin-lan
}
]
},
“lisa_admin_interface: {
}
}

No I want stiil want to iteracte on ‘bidirectional_rules_interfaces’ but is there a
possibilty to iterate for rukes also

{{#bidirectional_rules_interfaces}}
{{#.rules}}
{{service}}
{{/.rules}}
{{/bidirectional_rules_interfaces}}

wihtout the `.` and wirh ‘@.1` but no succes. Is it possible?


--
Bas van der Vlies
| Operations, Support & Development | SURFsara | Science Park 140 | 1098 XG Amsterdam
| T +31 (0) 20 800 1300 | bas.van...@surf.nl | www.surf.nl |




> --
> You received this message because you are subscribed to the Google Groups "help-cfengine" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to help-cfengin...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/d6c3d042-3b07-423b-9dda-f21f430f7abcn%40googlegroups.com.

Bas van der Vlies

unread,
Nov 6, 2020, 4:07:15 PM11/6/20
to Nick Anderson, help-cfengine

This two’s are working:
````
{{#rules}}
1 allow from {{source}} to any port {{{service}}} in {{{@}}}
{{/rules}}

{{#.rules}}
2 allow from {{source}} to any port {{{service}}} in {{{@}}}
{{/.rules}}
```
> To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/45B46510-C9FE-45AC-9F29-E6D3E229F5F0%40surf.nl.

Bas van der Vlies

unread,
Nov 8, 2020, 1:48:46 PM11/8/20
to Nick Anderson, help-cfengine
I have another strange issue. I have to write the mustache template like this to make it work:
```
{{#vars.sara_data.firewater.bidirectional_rules_interfaces}}
# {{{@}}} needed
allow from { {{#allow}}{{{.}}} {{/allow}} } to any port { {{#service}}{{{.}}} {{/services}} } {{{@}}}
{{/vars.sara_data.firewater.bidirectional_rules_interfaces}}

I had this:
```
{{#vars.sara_data.firewater.bidirectional_rules_interfaces}}
allow from { {{#allow}}{{{.}}} {{/allow}} } to any port { {{#service}}{{{.}}} {{/services}} } {{{@}}}
{{/vars.sara_data.firewater.bidirectional_rules_interfaces}}

But then I get an error:
```
info: Updated '/etc/node_status/templates/firewater/surf.rules.mustache' from source '/root/bas/cf_masterfiles/template_data/firewater/surf.rules.mustache' on 'localhost'
warning: RenderVariable: {{@}} Mustache tag must be used in a context where there's a valid key or iteration position
error: Error rendering mustache template '/etc/node_status/templates/firewater/surf.rules.mustache’
```

Regards

--
Bas van der Vlies
| Operations, Support & Development | SURFsara | Science Park 140 | 1098 XG Amsterdam
| T +31 (0) 20 800 1300 | bas.van...@surf.nl | www.surf.nl |




> To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/41546020-C1C8-49BC-BA28-7AFCC01CF2D3%40surf.nl.

Nick Anderson

unread,
Nov 12, 2020, 12:58:10 PM11/12/20
to help-cfengine

So, you added a comment line and it worked?
Smells fishy.

Can you open a ticket and attach a standalone reproducer?

Bas van der Vlies

unread,
Nov 12, 2020, 3:40:00 PM11/12/20
to Nick Anderson, help-cfengine

On 12 Nov 2020, at 18:58, 'Nick Anderson' via 


So, you added a comment line and it worked?
Smells fishy.

Yes that was what I also thought 

Reply all
Reply to author
Forward
0 new messages