Installing VirtualBox Guest Additions on a Debian Bullseye VM with an Ansible playbook

VirtualBox is a general-purpose full virtualizaton for x86 hardware, targeted at server, desktop and embedded use. The VirtualBox Guest Additions are extra pieces of software that enable better performance and functionality in virtual machines.

We install Guest Additions inside the virtual machine to activate this functionality. In VirtualBox, we can install Guest Additions by selecting Devices > Insert Guest Additions CD Image on the toolbar at the top of our guest OS window. This mounts a virtual disc to the VM. When we do this, our guest OS will respond as if we’ve just inserted a physical disc.

Here’s my example playbook that we can use to install Virtualbox Guest Additions on Debian Bullseye after installing the OS.

This solution is a quick and portable playbook. If we want to follow the industry standards, then it can be rewritten into an Ansible role.

---
- name: Install VirtualBox Guest Additions
  hosts: all
  become: yes

  tasks:
    - name: Update package repository
      apt:
        update_cache: yes

    - name: Install required packages
      apt:
        name:
          - build-essential
          - dkms
          - linux-headers-amd64

    - name: Mount Guest Additions ISO
      mount:
        path: /mnt/cdrom
        src: /usr/share/virtualbox/VBoxGuestAdditions.iso
        fstype: iso9660
        state: mounted

    - name: Install Guest Additions
      shell: /mnt/cdrom/VBoxLinuxAdditions.run

    - name: Unmount Guest Additions ISO
      mount:
        path: /mnt/cdrom
        state: unmounted

This playbook will update the package repository of Debian Bullseye, install the required packages, mount the Guest Additions ISO, install the Guest Additions software and then unmount the ISO.

Leave a comment