Initial commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
|
||||
---
|
||||
|
||||
- name: Install KVM and virtualization tools
|
||||
package:
|
||||
name:
|
||||
- qemu-kvm
|
||||
- libvirt-daemon-system
|
||||
- libvirt-clients
|
||||
- bridge-utils
|
||||
- virtinst
|
||||
- virt-manager
|
||||
- libguestfs-tools
|
||||
- xz-utils
|
||||
state: present
|
||||
when: ansible_facts['os_family'] == "Debian"
|
||||
|
||||
- name: Install KVM and virtualization tools (RedHat/Fedora)
|
||||
package:
|
||||
name:
|
||||
- qemu-kvm
|
||||
- libvirt
|
||||
- virt-install
|
||||
- virt-manager
|
||||
- libguestfs-tools
|
||||
- xz
|
||||
state: present
|
||||
when: ansible_facts['os_family'] == "RedHat"
|
||||
|
||||
- name: Install KVM and virtualization tools (Arch/CachyOS)
|
||||
package:
|
||||
name:
|
||||
- qemu-full
|
||||
- libvirt
|
||||
- virt-manager
|
||||
- iproute2
|
||||
- libguestfs
|
||||
- guestfs-tools
|
||||
- xz
|
||||
state: present
|
||||
when: ansible_facts['os_family'] == "Archlinux"
|
||||
|
||||
- name: Ensure libvirtd is started and enabled
|
||||
service:
|
||||
name: libvirtd
|
||||
state: started
|
||||
enabled: yes
|
||||
|
||||
- name: Ensure KVM default network is active and autostarts
|
||||
shell: |
|
||||
virsh net-start default || true
|
||||
virsh net-autostart default || true
|
||||
become: yes
|
||||
|
||||
- name: Add current user to libvirt group
|
||||
user:
|
||||
name: "{{ ansible_facts['user_id'] }}"
|
||||
groups: libvirt
|
||||
append: yes
|
||||
become: yes
|
||||
ignore_errors: yes # Some distros use different group names (e.g., kvm)
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
- name: Generate Ignition config for CoreOS/Flatcar
|
||||
template:
|
||||
src: ignition.json.j2
|
||||
dest: "/tmp/{{ vm_name }}_ignition.json"
|
||||
when: os_type == "coreos" or os_type == "flatcar"
|
||||
|
||||
- name: Generate Cloud-init config for MicroOS
|
||||
template:
|
||||
src: user-data.yaml.j2
|
||||
dest: "/tmp/{{ vm_name }}_user-data"
|
||||
when: os_type == "microos"
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"ignition": {
|
||||
"version": "0.3.0"
|
||||
},
|
||||
"passwd": {
|
||||
"users": [
|
||||
{
|
||||
"name": "{{ vm_user }}",
|
||||
"password_hash": "{{ vm_password | password_hash('sha512') }}",
|
||||
"ssh_public_keys": [
|
||||
"{{ lookup('file', vm_ssh_public_key) }}"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"storage": {
|
||||
"files": [
|
||||
{
|
||||
"path": "/etc/ssh/sshd_config.d/permit_root_login.conf",
|
||||
"contents": {
|
||||
"source": "data:text/plain;charset=utf-8,PermitRootLogin yes"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#cloud-config
|
||||
users:
|
||||
- name: {{ vm_user }}
|
||||
passwd: {{ vm_password | password_hash('sha512') }}
|
||||
ssh_authorized_keys:
|
||||
- {{ lookup('file', vm_ssh_public_key) }}
|
||||
sudo: ALL=(ALL) NOPASSWD:ALL
|
||||
lock_passwd: false
|
||||
@@ -0,0 +1,69 @@
|
||||
---
|
||||
|
||||
- name: Define image URLs
|
||||
set_fact:
|
||||
os_images:
|
||||
coreos: "https://builds.coreos.fedoraproject.org/prod/streams/stable/builds/44.20260510.3.1/x86_64/fedora-coreos-44.20260510.3.1-qemu.x86_64.qcow2.xz"
|
||||
flatcar: "https://stable.release.flatcar-linux.net/amd64-usr/current/flatcar_production_qemu_uefi_image.img"
|
||||
microos: "https://ftp.halifax.rwth-aachen.de/opensuse/tumbleweed/appliances/openSUSE-MicroOS.x86_64-kvm-and-xen.qcow2"
|
||||
|
||||
- name: Verify internet connectivity
|
||||
uri:
|
||||
url: http://google.com
|
||||
return_content: no
|
||||
timeout: 10
|
||||
|
||||
- name: Download OS image
|
||||
get_url:
|
||||
url: "{{ os_images[os_type] }}"
|
||||
dest: "{{ vm_images_dir }}/{{ vm_name }}.download"
|
||||
mode: '0644'
|
||||
become: yes
|
||||
|
||||
- name: Handle compressed or raw images
|
||||
shell: |
|
||||
DOWNLOAD_FILE="{{ vm_images_dir }}/{{ vm_name }}.download"
|
||||
FINAL_FILE="{{ vm_images_dir }}/{{ vm_name }}.qcow2"
|
||||
|
||||
# 1. Handle XZ compression
|
||||
if [[ "{{ os_images[os_type] }}" == *.xz ]]; then
|
||||
echo "Decompressing XZ image..."
|
||||
unxz -c "$DOWNLOAD_FILE" > "$FINAL_FILE"
|
||||
elif [[ "{{ os_images[os_type] }}" == *.img ]]; then
|
||||
echo "Converting RAW image to QCOW2..."
|
||||
qemu-img convert -f raw -O qcow2 "$DOWNLOAD_FILE" "$FINAL_FILE"
|
||||
else
|
||||
echo "Moving QCOW2 image to final destination..."
|
||||
mv "$DOWNLOAD_FILE" "$FINAL_FILE"
|
||||
fi
|
||||
rm -f "$DOWNLOAD_FILE"
|
||||
become: yes
|
||||
args:
|
||||
creates: "{{ vm_images_dir }}/{{ vm_name }}.qcow2"
|
||||
|
||||
- name: Provision VM using virt-install
|
||||
shell: |
|
||||
virt-install \
|
||||
--name {{ vm_name }} \
|
||||
--vcpus {{ cpu | default(default_cpu) }} \
|
||||
--memory {{ ram | default(default_ram) }} \
|
||||
--disk path={{ vm_images_dir }}/{{ vm_name }}.qcow2,bus=virtio \
|
||||
--import \
|
||||
--os-variant {{ os_variant }} \
|
||||
--network network=default \
|
||||
--graphics none \
|
||||
--noautoconsole \
|
||||
--boot uefi \
|
||||
{% if os_type == 'coreos' or os_type == 'flatcar' %}
|
||||
--cloud-init user-data=/tmp/{{ vm_name }}_ignition.json
|
||||
{% elif os_type == 'microos' %}
|
||||
--cloud-init user-data=/tmp/{{ vm_name }}_user-data
|
||||
{% endif %}
|
||||
args:
|
||||
creates: "/etc/libvirt/qemu/{{ vm_name }}.xml"
|
||||
|
||||
- name: Attach configuration to VM
|
||||
debug:
|
||||
msg: "Configuration is now handled by virt-install --cloud-init flag."
|
||||
when: false # This task is now obsolete
|
||||
become: yes
|
||||
Reference in New Issue
Block a user