I am looking for the API equivalent of doing the below.
lxc launch ubuntu1804 test --config=user.network-config="$(cat network-config.yaml)"
I am using pylxd client and while creating a container I can pass a config dictionary.
How do I pass a yaml as a value to the user.network-config in config object?
You have to create the container first, and then apply the cloud-init config to the container’s “config” dict. I’m using lxd 3.13 and pylxd 2.2.6 as of this writing.
Here’s the way I do it. “netconf” is a dict in the format as this example {“eth0”: “192.168.55.19”}
for ifname, nwstr in netconf.items():
if not '/' in nwstr:
nwstr = "%s/%d" % (nwstr, 24)
ic = ipaddress.ip_interface(nwstr)
gw = ipaddress.ip_address(ic.network.network_address + 1).compressed
dns.append(ipaddress.ip_address(ic.network.network_address + 2).compressed)
networks.append({'type': 'physical', 'name': ifname, 'subnets':
[{
'type': 'static',
'address': ic.compressed,
'gateway': gw,
}],
})
nwconfig = yaml.dump({'version': 1, 'config': networks})
cont = client.containers.create(config, wait=True)
# have to create() before setting cloud-init config for some unfathomable reason
cont.config['user.network-config'] = nwconfig
cont.save(wait=True)