Creating a Virtual Machine (VM) using Ansible Playbooks on an Ubuntu Server 24.04 to pass a USB Wi-Fi adapter and detect its connection during the installation process is a common task. However, despite the network interface being detected and connected, there might be instances where the VM still does not have an internet connection. This article will provide a detailed context, covering key concepts, subtitles, and a solution to the issue.
Table of Contents
- Preparing the Host Machine
- Creating the Ansible Playbook
- Running the Ansible Playbook
- Troubleshooting No Internet Connection in the VM
Preparing the Host Machine
Before creating the VM, ensure that the host machine has the following prerequisites:
- A USB Wi-Fi adapter
- Ansible installed and configured
- VirtualBox installed and configured
Creating the Ansible Playbook
Create a new directory for the playbook and initialize it:
mkdir -p vm_setup/ansible_playbooks/
cd vm_setup/ansible_playbooks/
ansible-galaxy init vm_setup.yml
Now, edit the vm_setup.yml file to include the necessary tasks:
---
- hosts: vm
become: yes
vars:
vm_name: my_vm
vm_memory: 2048
vm_cpus: 2
vm_storage: 40960
usb_device: 046d:c52b
tasks:
- name: Add USB Wi-Fi adapter to VM
virsh:
cmd: 'define $vm_name'
args:
connection: virtualbox
state: present
when: ansible_os_family == 'Debian'
- name: Add USB Wi-Fi adapter to VM configuration
virsh:
cmd: 'edit $vm_name'
args:
connection: virtualbox
state: present
when: ansible_os_family == 'Debian'
- name: Add USB device to VM configuration
blockinfile:
path: /etc/virsh/qemu-ifup.d/50-usb-device.cfg
block: |
<interface type='userdevice'>
<mac address='52:54:00:{{ vm_mac }}'/>
<source controller='usb' index='{{ item }}'/>
<model type='rndis'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</interface>
create: yes
vars:
vm_mac: `cat /sys/class/net/virbr0/address`
with_items:
- "{{ usb_device }}"
- name: Create and start the VM
virsh:
cmd: 'create $vm_name'
args:
connection: virtualbox
state: present
- name: Install OS on the VM
command: virt-install --connect qemu:///system --name $vm_name --ram $vm_memory --vcpus $vm_cpus --disk path=$vm_storage,format=qcow2 --os-type=linux --os-variant=ubuntu24.04 --graphics vnc --noautoconsole --network bridge=virbr0 --usb $usb_device
args:
executable: /usr/bin/env
become: yes
- name: Wait for the VM to finish booting
wait_for:
host: "{{ vm_name }}"
port: 5900
state: started
delay: 60
retries: 60
ignore_errors: yes
- name: Install necessary packages
apt:
name:
- wpa_supplicant
- dhcpcd5
- net-tools
- name: Configure Wi-Fi connection
blockinfile:
path: /etc/wpa_supplicant/wpa_supplicant.conf
create: yes
block: |
country=US
network={
ssid="Your_Wi-Fi_SSID"
psk="Your_Wi-Fi_PASSWORD"
}
- name: Restart networking services
service:
name: networking
state: restarted
Running the Ansible Playbook
To run the playbook, execute the following command:
ansible-playbook vm_setup.yml
Troubleshooting No Internet Connection in the VM
If the VM still does not have an internet connection after following the above steps, try the following troubleshooting steps:
- Check the VM's network interface:
virsh net-dhcp-leases virbr0
- Check the DHCP client logs:
dmesg | grep dhcp
- Manually configure the network interface:
sudo ip addr show
sudo ip link set eth0 up
sudo ip addr add <IP_ADDRESS>/<NETMASK> dev eth0
sudo ip route add default via <GATEWAY_IP>
Replace <IP_ADDRESS>, <NETMASK>, and <GATEWAY_IP> with the appropriate values.
References