[lxc-users] lxd container templates user.foo=bar and dot usage in keys passed to pongo2?
Stéphane Graber
stgraber at ubuntu.com
Mon Jul 25 20:56:40 UTC 2016
On Mon, Jul 25, 2016 at 12:27:11PM -0500, RayFerguson wrote:
> 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
>
> config:
> 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 %}
You can use: {{ config_get("user.foo", "default-if-not-set") }}
Feel free to file a bug at https://github.com/lxc/lxd to have this
documented as I don't think it's covered in our image doc.
>
> 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
> works:
>
> {{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?
>
> config:
> user:
> foo: bar
>
> or
>
> config: { foo: bar }
>
> Example:
>
> ==== metadata.yaml:
> ...
>
> "templates": {
> ...
> "/etc/sysconfig/network-scripts/ifcfg-eth0": {
> "template": "ifcfg-eth0.tpl",
> "when": [
> "start"
> ]
> },
> ...
>
> ==== templates/ifcfg-eth0.tpl:
> DEVICE=eth0
> BOOTPROTO=static
> DHCPCLASS=
> 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 %}
> ONBOOT=yes
>
> ==== lxc profile show local
> name: local
> config:
> user.gateway: 10.0.2.1
> user.netmask: 255.255.255.128
> user.ns: 10.0.2.1
> description: Adds local network options that don't change from host to host.
> devices:
> eth0:
> name: eth0
> nictype: bridged
> parent: lxdbr0
> type: nic
> _______________________________________________
> lxc-users mailing list
> lxc-users at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-users
--
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-users/attachments/20160725/3fb96b34/attachment.sig>
More information about the lxc-users
mailing list