The goal is to get an easy way to fire up a debian vm to which we can connect via ssh / ansible to configure further. An easy way to do this is the debian cloud images and cloud-init configuration that creates an user configures the authorized_keys file.

Get the right Debian cloudimage, for me only the generic types worked. The nocloud does not run cloud-init, and the genericcloud probaly does not have a cdrom driver, anyway it did not run my cloudinit. So the one i used was Debian 11 generic variant

You also need a copy of genisoimage, that is not availble for opensuse. So grab the source code from the next friendly Debian mirror. To build genisoimage I also needed to install libcap-devel with sudo zypper install libcap-devel

I wanted to have an virsh pool, so create one:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  dir=~/test-debian2
  rm -rf $dir
  mkdir -p $dir
  cd $dir
  POOL_NAME="debian-test-pool"
  DIR_PATH=$(pwd)
  #
  # Destory the virs pool $POOL_NAME, and create a fresh one
  #
  virsh pool-destroy $POOL_NAME
  virsh pool-undefine debian-test-pool
  POOL_XML="${POOL_NAME}_pool.xml"
  cat > $POOL_XML <<EOF
   <pool type='dir'>
     <name>$POOL_NAME</name>
     <target>
       <path>$DIR_PATH</path>
     </target>
   </pool>
  EOF
  virsh pool-define $POOL_XML
  virsh pool-start $POOL_NAME
  virsh pool-autostart $POOL_NAME
  rm $POOL_XML
  echo "Storage pool '$POOL_NAME' created and started using directory $DIR_PATH"

Download the image and create a qcow2 derivate from it:

1
2
  wget  https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-generic-amd64.qcow2 
  qemu-img create -b debian-11-generic-amd64.qcow2   -f qcow2 -F qcow2  debian-testing.img  8G

Create the user-data and a meta-data cloud init config files and bake an iso file out of it. yamllint and cloud-init schema --config-file user-data are helpfull to test the syntax of the config files.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    #
    # create the cloud init config
    #
    vm_name=debian-whatever

    cat << EOF > meta-data
    instance-id: $vm_name
    local-hostname: $vm_name
    EOF

    cat << EOF >user-data
    #cloud-config
    packages:
      - openssh
      - bash
    runcmd:
      - systemctl enable sshd
      - systemctl start sshd
    users:
      - name: debian
        groups: sudo
        shell: /bin/bash
        sudo: ["ALL=(ALL) NOPASSWD:ALL"]
        ssh_authorized_keys:
          - ssh-rsa $(cat ~/.ssh/id_rsa.pub)
    EOF

  /usr/local/bin/genisoimage -output cidata.iso -V cidata -r -J user-data meta-data

Thats, all, just fire up the vm, attach the create image and the created iso:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# create the vm
  virt-install --name=$vm_name \
                 --ram=2048 \
                 --vcpus=2 \
                 --import \
                 --disk path=debian-testing.img,format=qcow2 \
                 --disk path=cidata.iso,device=cdrom \
                 --os-variant=debian11 \
                 --network bridge=virbr0,model=virtio \
                 --graphics vnc,listen=0.0.0.0 \
                 --noautoconsole

virsh net-dhcp-leases network shows you the ip to connect ansible or ssh to.