How to Override Values From the Config File?

I currently have a config file that looks like this:

devices:
  eth0:
    nictype: macvlan
    parent: enp3s0
    type: nic
config:
  cloud-init.network-config: |
    version: 1
    config:
      - type: physical
        name: eth0
        subnets:
          - type: static
            ipv4: true
            ipv6: true
            address: 192.168.1.201
            netmask: 255.255.255.0
            gateway: 192.168.1.1
            control: auto
      - type: nameserver
        address: 8.8.8.8
  cloud-init.user-data: |
    #cloud-config
    users:
      - name: ansible
        ssh-authorized-keys:
          - pub_key
        shell: /bin/bash
        sudo: ALL=(ALL) NOPASSWD:ALL

and Iā€™m spinning up an instance like this: lxc launch ubuntu:jammy my-test --profile default < ./config.yml
If I wanted to specify a different ip address though, how would I specify in the launch command without going in and editing the config file?

You can change each config easily using lxc config set command, for example:

lxc config set instance-name boot.autostart false

And then you can check changes using this command:

lxc config show instance-name

For the IP address, if you want to assign static IP, you can use these commands:

lxc network attach lxdbr0 instance-name eth0 eth0
lxc config device set instance-name eth0 ipv4.address 10.99.10.42

Your config file is using a macvlan NIC, which is connected to an external network parent enp3s0.
As such LXD cannot provide static DHCP allocations, and thus cannot control the IP the instance is given.

Instead you have passed cloud-init config to the instance, so it will self-configure its own static IP settings when cloud-init runs inside it at first start.

However if you want to change that IP without editing the config file then you will need to use some sort of templating system and pass that config file through a pre-processor step before passing it to lxc launch command.

This could be as simple as using something like awk to find and replace a placeholder in the file.

1 Like