Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| add0a7f2c2 | |||
| e40d91eb16 | |||
| f6a3273e08 | |||
| 83edc85654 | |||
| 78695e9f1d | |||
| 71c67cb81a | |||
| f2c6da73df | |||
| fdd7ca8016 | |||
| 223b787675 | |||
| 4c52425591 | |||
| 03ced3d1b1 | |||
| 51fd19f45a |
@@ -5,9 +5,7 @@ This project provides an Ansible-based framework to automatically provision virt
|
||||
## 🚀 Features
|
||||
|
||||
- **Automated Host Setup**: Installs and configures `libvirt`, `qemu-kvm`, and `libguestfs-tools`.
|
||||
- **Immutable OS Support**: Handles the specific boot-time configuration requirements for:
|
||||
- **CoreOS/Flatcar**: Generates and injects Ignition JSON configurations.
|
||||
- **MicroOS**: Generates and injects Cloud-init user-data.
|
||||
- **Ignition Support**: Generates and injects Ignition configuration for all supported OSs via the `fw_cfg` QEMU feature.
|
||||
- **Custom User Provisioning**: Automatically creates a default user with a hashed password and injects your SSH public key.
|
||||
- **Modular Design**: Uses Ansible roles for host preparation, configuration generation, and VM provisioning.
|
||||
|
||||
@@ -22,7 +20,7 @@ ansible-kvm-vms/
|
||||
│ └── vms.yml # List of VMs to create with CPU, RAM, and Disk specs
|
||||
├── roles/
|
||||
│ ├── kvm_host_setup/ # Installs virtualization dependencies on the host
|
||||
│ ├── os_config/ # Generates Ignition/Cloud-init config files
|
||||
│ ├── os_config/ # Generates Ignition configuration files
|
||||
│ └── vm_provision/ # Downloads images and creates VMs via virt-install
|
||||
└── playbooks/
|
||||
└── create_vms.yml # Main orchestration playbook
|
||||
@@ -67,9 +65,8 @@ Edit `vars/vms.yml` to add or modify the VMs you wish to deploy. You can specify
|
||||
## 🔍 How it Works
|
||||
|
||||
Since immutable OSs do not use traditional installers, this setup uses a "seed" approach:
|
||||
1. **Config Generation**: The `os_config` role creates a JSON (Ignition) or YAML (Cloud-init) file based on your variables.
|
||||
2. **Image Customization**: The `vm_provision` role downloads the official `.qcow2` cloud image and uses `virt-customize` (from `libguestfs-tools`) to inject the configuration directly into the disk image before the VM is started.
|
||||
3. **Deployment**: `virt-install` is used to create the VM with UEFI boot and the customized disk.
|
||||
1. **Config Generation**: The `os_config` role creates a JSON Ignition configuration file based on your variables.
|
||||
2. **Deployment**: `virt-install` is used to create the VM with UEFI boot. The `--sysinfo` flag is used to provide the Ignition config via the `fw_cfg` device, which the immutable OSs (CoreOS, Flatcar, MicroOS) process at first boot.
|
||||
|
||||
## 🌐 Accessing your VMs
|
||||
|
||||
|
||||
@@ -27,4 +27,6 @@
|
||||
cpu: "{{ item.cpu }}"
|
||||
ram: "{{ item.ram }}"
|
||||
disk: "{{ item.disk }}"
|
||||
disk_bus: "{{ item.disk_bus }}"
|
||||
loop: "{{ vms }}"
|
||||
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
---
|
||||
- name: Generate Ignition config for CoreOS/Flatcar
|
||||
- name: Generate Ignition config
|
||||
template:
|
||||
src: ignition.json.j2
|
||||
dest: "/tmp/{{ vm_name }}_ignition.json"
|
||||
when: os_type == "coreos" or os_type == "flatcar"
|
||||
dest: "/tmp/{{ vm_name }}.ign"
|
||||
|
||||
- name: Generate Cloud-init config for MicroOS
|
||||
template:
|
||||
src: user-data.yaml.j2
|
||||
dest: "/tmp/{{ vm_name }}_user-data"
|
||||
when: os_type == "microos"
|
||||
|
||||
@@ -1,26 +1,15 @@
|
||||
{
|
||||
"ignition": {
|
||||
"version": "0.3.0"
|
||||
"version": "3.0.0"
|
||||
},
|
||||
"passwd": {
|
||||
"users": [
|
||||
{
|
||||
"name": "{{ vm_user }}",
|
||||
"password_hash": "{{ vm_password | password_hash('sha512') }}",
|
||||
"ssh_public_keys": [
|
||||
"{{ lookup('file', vm_ssh_public_key) }}"
|
||||
"sshAuthorizedKeys": [
|
||||
"{{ lookup('file', vm_ssh_public_key | replace('~', lookup('env', 'HOME'))) }}"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"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,11 @@
|
||||
#cloud-config
|
||||
users:
|
||||
- name: {{ vm_user }}
|
||||
passwd: {{ vm_password | password_hash('sha512') }}
|
||||
ssh_authorized_keys:
|
||||
- {{ lookup('file', vm_ssh_public_key | replace('~', lookup('env', 'HOME'))) }}
|
||||
sudo: ALL=(ALL) NOPASSWD:ALL
|
||||
write_files:
|
||||
- path: /etc/ssh/sshd_config.d/permit_root_login.conf
|
||||
content: |
|
||||
PermitRootLogin yes
|
||||
@@ -1,8 +1,7 @@
|
||||
#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
|
||||
ssh_authorized_keys:
|
||||
- {{ lookup('file', vm_ssh_public_key | replace('~', lookup('env', 'HOME'))) }}
|
||||
lock_passwd: false
|
||||
|
||||
@@ -41,29 +41,32 @@
|
||||
args:
|
||||
creates: "{{ vm_images_dir }}/{{ vm_name }}.qcow2"
|
||||
|
||||
- name: Debug Provisioning Vars
|
||||
debug:
|
||||
msg: "Provisioning {{ vm_name }} with disk_bus={{ disk_bus | default('virtio') }} and os_variant={{ os_variant }}"
|
||||
|
||||
- name: Remove existing VM definition
|
||||
shell: |
|
||||
virsh destroy {{ vm_name }} || true
|
||||
virsh undefine {{ vm_name }} --nvram || true
|
||||
rm -f /etc/libvirt/qemu/{{ vm_name }}.xml
|
||||
become: yes
|
||||
|
||||
- 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 \
|
||||
--machine q35 \
|
||||
--disk path={{ vm_images_dir }}/{{ vm_name }}.qcow2,bus={{ disk_bus | default('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 %}
|
||||
--sysinfo type=fwcfg,entry0.name="opt/com.coreos/config",entry0.file="/tmp/{{ vm_name }}.ign"
|
||||
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
|
||||
|
||||
@@ -3,6 +3,7 @@ vms:
|
||||
- name: coreos-vm
|
||||
os_type: coreos
|
||||
os_variant: "fedora-coreos-stable"
|
||||
disk_bus: "virtio"
|
||||
cpu: 2
|
||||
ram: 2048
|
||||
disk: "20G"
|
||||
@@ -10,13 +11,15 @@ vms:
|
||||
- name: flatcar-vm
|
||||
os_type: flatcar
|
||||
os_variant: "fedora-coreos-stable"
|
||||
disk_bus: "virtio"
|
||||
cpu: 2
|
||||
ram: 2048
|
||||
disk: "20G"
|
||||
|
||||
- name: microos-vm
|
||||
os_type: microos
|
||||
os_variant: "opensusemicroos"
|
||||
os_variant: "generic"
|
||||
disk_bus: "virtio"
|
||||
cpu: 2
|
||||
ram: 2048
|
||||
disk: "20G"
|
||||
|
||||
Reference in New Issue
Block a user