[lxc-users] lxd container templates user.foo=bar and dot usage in keys passed to pongo2?

RayFerguson subscriber at share-foo.com
Mon Jul 25 17:27:11 UTC 2016

Am I doing something wrong, or is the user section of config template
variables a real pain to use due to the use of dots in keys?

In theory, you can do just about anything you want by adding custom config
keys under the user prefix, then using those values in templates.

The trouble comes when trying to use them.  Dot is supposed to be a break
point in the pongo template language representing another nested key/value
hash.  It's not supposed to be a valid char on the key side and there seems
to be no way to escape the dot and forcing pongo2 to treat the dot as a
part of a key in the template language.

So when lxd provides "user.foo"="bar" to the template expansion, you can't
just use them as {{user.foo}} and have them expanded as expected, because
because {{use.foo}} references the non-existent {user: { foo: bar } } not {
user.foo: bar }.

For example set

  user.foo: bar

in a template or via lxc luanch -c user.foo=bar and the only way I found to
use it was this really obtuse looking for loop.

{% for k,v in config %}{%if k in "user.foo" %}{{v}}{% endif %}{% endfor %}

And it's not just user space keys that use dot as part of a key provided to
the template.  If you want the automagic-mac, I can only get it with

{% for k,v in config %}{%if k in "volatile.eth0.hwaddr" %}{{v}}{% endif
%}{% endfor %}

not {{volatile.eth0.hwaddr}} as the examples suggest.

but if you want the container name, you follow the straight forward syntax

{{container.name}} is correct.

This all seems way to strange to be the right way to do this, but alternate
constructions that should make it a map to satisfy pongo2 are rejected as
invalid config by lxc/lxd.

So the big question is, am I missing a way easier construct, and is this
intentional or something that should be changed?

       foo: bar


config: { foo: bar }


==== metadata.yaml:

    "templates": {
        "/etc/sysconfig/network-scripts/ifcfg-eth0": {
            "template": "ifcfg-eth0.tpl",
            "when": [

==== templates/ifcfg-eth0.tpl:
HWADDR={% for k,v in config %}{%if k in "volatile.eth0.hwaddr" %}{{v}}{%
endif %}{% endfor %}
IPADDR={% for k,v in config %}{%if k in "user.ip" %}{{v}}{% endif %}{%
endfor %}
NETMASK={% for k,v in config %}{%if k in "user.netmask" %}{{v}}{% endif
%}{% endfor %}
GATEWAY={% for k,v in config %}{%if k in "user.gateway" %}{{v}}{% endif
%}{% endfor %}

==== lxc profile show local
name: local
description: Adds local network options that don't change from host to host.
    name: eth0
    nictype: bridged
    parent: lxdbr0
    type: nic
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linuxcontainers.org/pipermail/lxc-users/attachments/20160725/7146252a/attachment.html>

More information about the lxc-users mailing list