Skip to content

Commit defbd26

Browse files
committed
fix: Add internet (NAT) NIC
1 parent 8265c25 commit defbd26

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

roles/create-vm/defaults/main.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ vm_disk_format: qcow2
1010

1111
vm_network_hostnet_name: stackable-adds-test-hostnet
1212
vm_network_hostnet_subnet: 192.168.197.0/24
13-
vm_network_internet_name: default
13+
vm_network_internet_name: stackable-adds-test-internet
14+
vm_network_internet_subnet: 192.168.196.0/24
1415

1516
install_iso_windows: "{{ lookup('first_found', 'target') }}/Windows Server 2022 EVAL.iso"
1617

roles/create-vm/tasks/main.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
path: "{{ install_iso_windows }}"
99
get_checksum: false
1010
register: install_iso_windows_stat
11+
1112
- name: Complain about missing Windows ISO
1213
ansible.builtin.fail:
1314
msg: "Windows ISO could not be found, please download it from https://www.microsoft.com/en-us/evalcenter/download-windows-server-2022 and put it at {{ install_iso_windows }} (or modify install_iso_windows to point at the correct path)"
@@ -18,6 +19,7 @@
1819
dest: "{{ install_iso_virtio_win }}"
1920
url: "{{ install_iso_virtio_win_url }}"
2021
checksum: "{{ install_iso_virtio_win_checksum }}"
22+
2123
- name: Download SPICE guest tools
2224
ansible.builtin.get_url:
2325
dest: "{{ install_exe_spice_guest_tools }}"
@@ -30,11 +32,26 @@
3032
command: define
3133
xml: "{{ lookup('template', 'windows-vm-network.xml.j2') }}"
3234
uri: "{{ libvirt_uri }}"
35+
3336
- name: Start VM Network
3437
community.libvirt.virt_net:
3538
name: "{{ vm_network_hostnet_name }}"
3639
state: active
3740
uri: "{{ libvirt_uri }}"
41+
42+
- name: Create VM Network (Internet)
43+
community.libvirt.virt_net:
44+
name: "{{ vm_network_internet_name }}"
45+
command: define
46+
xml: "{{ lookup('template', 'windows-vm-network-internet.xml.j2') }}"
47+
uri: "{{ libvirt_uri }}"
48+
49+
- name: Start VM Network (Internet)
50+
community.libvirt.virt_net:
51+
name: "{{ vm_network_internet_name }}"
52+
state: active
53+
uri: "{{ libvirt_uri }}"
54+
3855
- name: Create VM
3956
community.libvirt.virt:
4057
command: define
@@ -43,6 +60,7 @@
4360
- ADD_UUID
4461
- ADD_MAC_ADDRESSES
4562
uri: "{{ libvirt_uri }}"
63+
4664
- name: Check if VM Volume already exists
4765
# Ansible has no idempotent way to define libvirt volumes,
4866
# so instead try to check whether it exists.
@@ -52,29 +70,34 @@
5270
# Errors should cause the create step to fail anyway
5371
failed_when: False
5472
changed_when: result_check_vm_disk.rc != 0
73+
5574
- name: Create VM Volume
5675
when: result_check_vm_disk is changed
5776
ansible.builtin.command:
5877
cmd: virsh --connect "{{ libvirt_uri }}" vol-create --pool "{{ vm_disk_pool }}" --file /dev/stdin
5978
stdin: "{{ lookup('template', 'windows-vm-volume.xml.j2') }}"
79+
6080
- name: Start VM
6181
community.libvirt.virt:
6282
name: "{{ vm_name }}"
6383
state: running
6484
uri: "{{ libvirt_uri }}"
85+
6586
- name: Get VM Metadata
6687
community.libvirt.virt:
6788
name: "{{ vm_name }}"
6889
command: get_xml
6990
uri: "{{ libvirt_uri }}"
7091
register: created_vm
92+
7193
# VM can have multiple NICs, extract the one we care about so we can match it against the IP address of the guest later
7294
- name: Extract VM's MAC address
7395
community.general.xml:
7496
xmlstring: "{{ created_vm.get_xml }}"
7597
xpath: /domain/devices/interface[alias/@name="ua-net-hostnet"]/mac
7698
content: attribute
7799
register: vm_network_mac_xml
100+
78101
- set_fact:
79102
vm_network_mac: "{{ vm_network_mac_xml.matches[0].mac.address }}"
80103
delegate_to: sble-addc
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<network connections="1">
2+
<name>{{ vm_network_internet_name }}</name>
3+
<forward mode="nat">
4+
<nat>
5+
<port start='1024' end='65535'/>
6+
</nat>
7+
</forward>
8+
<bridge name='virbr2' stp='on' delay='0'/>
9+
<ip
10+
address="{{ vm_network_internet_subnet | ansible.utils.ipaddr('next_usable') }}"
11+
netmask="{{ vm_network_internet_subnet | ansible.utils.ipaddr('netmask') }}">
12+
<dhcp>
13+
<range
14+
start="{{ vm_network_internet_subnet | ansible.utils.next_nth_usable(2) }}"
15+
end="{{ vm_network_internet_subnet | ansible.utils.ipaddr('last_usable') }}"/>
16+
</dhcp>
17+
</ip>
18+
</network>

0 commit comments

Comments
 (0)