How to upgrade LXD VMs powered by Ubuntu/Debian or CentOS Linux

I run over 10+ LXD VMs powered by CentOS Linux 7 and Debian/Ubuntu Linux. How do I upgrade all VMs automatically or on demand using Ansible tool?

There are two ways to upgrade your LXD (Linux containers) VMs. Both methods depend upon the use of the /usr/bin/lxc command.

Method 1 – Update using a shell script

The syntax is pretty simple:
/usr/bin/lxc exec vmNameHere -- command
/usr/bin/lxc exec vmNameHere -- /usr/bin/apt-get -y upgrade
/usr/bin/lxc exec vmNameHere -- /usr/bin/apt-get -y upgrade
/usr/bin/lxc exec vmNameHere -- /usr/bin/yum -y update

Next you can create a shell script as follows:

#!/bin/bash
# Purpose: Run given command inside the LXD vm 
# Note: Must be run from host. Only tested on Ubuntu 16.04 LTS
# Author: SXI ADMIN { https://sxi.io } under GPL v2.x+
# -----------------------------------------------------------------
cmd="[email protected]"
for i in vm1 vm2 vm3 vm10
do 
  echo "[*** $i ***]"
  /usr/bin/lxc exec $i -- $cmd
done

#!/bin/bash
# Purpose: Run given command inside the LXD vm
# Note: Must be run from host. Only tested on Ubuntu 16.04 LTS
# Author: SXI ADMIN { https://sxi.io } under GPL v2.x+
# —————————————————————–
cmd="[email protected]"
for i in vm1 vm2 vm3 vm10
do
echo "[*** $i ***]"
/usr/bin/lxc exec $i — $cmd
done

Now all you have to do is run command for a Debian or Ubuntu Linux based vm i.e. pass the apt-get command/apt command to update VM:
$ ~/bin/script-name /usr/bin/apt-get update
$ ~/bin/script-name /usr/bin/apt-get -y upgrade

If you are using a CentOS 7 based VM pass the yum command:
$ ~/bin/script-name /usr/bin/apt-get /usr/bin/yum -y update

Method 2 – Update LXD VMs using Ansible devops/sysadmin automation tool

You can create a lxd-update.yml ansible playbook file as follows:

          #
          # Update host running on Debian/Ubuntu
          #
          - name: Updating host using apt
            apt:
                    update_cache: yes
                    upgrade: dist
          #
          # Now update VMs powered by Debian/Ubuntu Linux
          #
          - name: Run lxc to update debian/ubuntu vms repo
            command: /usr/bin/lxc exec {{ item }} -- /usr/bin/apt-get -y update 
            with_items:
                    - ubuntuvm1
                    - debianvm2
                    - ubuntuvm3
 
          - name: Run lxc to upgrade debian/ubuntu vms pacakges
            command: /usr/bin/lxc exec {{ item }} -- /usr/bin/apt-get -y upgrade 
            with_items:
                    - ubuntuvm1
                    - debianvm2
                    - ubuntuvm3
          #
          # Now update VMs powered by CentOS 7 Linux
          #
          - name: Run lxc to upgrade centos vms
            command: /usr/bin/lxc exec {{ item }} -- /usr/bin/yum -y update
            with_items:
                    - centosvm4
                    - centosvm5
                    - centosvm6

#
# Update host running on Debian/Ubuntu
#
– name: Updating host using apt
apt:
update_cache: yes
upgrade: dist
#
# Now update VMs powered by Debian/Ubuntu Linux
#
– name: Run lxc to update debian/ubuntu vms repo
command: /usr/bin/lxc exec {{ item }} — /usr/bin/apt-get -y update
with_items:
– ubuntuvm1
– debianvm2
– ubuntuvm3 – name: Run lxc to upgrade debian/ubuntu vms pacakges
command: /usr/bin/lxc exec {{ item }} — /usr/bin/apt-get -y upgrade
with_items:
– ubuntuvm1
– debianvm2
– ubuntuvm3
#
# Now update VMs powered by CentOS 7 Linux
#
– name: Run lxc to upgrade centos vms
command: /usr/bin/lxc exec {{ item }} — /usr/bin/yum -y update
with_items:
– centosvm4
– centosvm5
– centosvm6

You can run it as follows using the ansible-playbook command:
$ ansible-playbook -i my-hosts lxd-update.yml
The last method is easy to use and easy to extend too. Hence, I recommend that you use the Ansible playbooks to update your LXD powered VMs.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.