From 48e556a946342a8c6a3afc0d74eeb11931669596 Mon Sep 17 00:00:00 2001 From: Peymaneh Date: Thu, 26 Nov 2026 12:50:21 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20Add=20virtualization=20role?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See roles/virt/README.md --- group_vars/all/main.yml | 3 + group_vars/hypervisor/main.yml | 2 + group_vars/hypervisor/secrets.yml | 9 ++ group_vars/roy/main.yml | 3 + inventory | 5 + playbooks/virtualization.yml | 9 ++ roles/monitor/tasks/_rewrite_prom_config.yml | 17 +++ roles/monitor/templates/prometheus.yml.j2 | 4 + roles/prometheus_libvirt_exporter/README.md | 3 + .../defaults/main.yml | 4 + .../handlers/main.yml | 10 ++ .../meta/argument_specs.yml | 9 ++ .../tasks/main.yml | 93 ++++++++++++++ .../templates/prometheus.service.j2 | 29 +++++ roles/virt/README.md | 118 +++++++++++++++++ roles/virt/defaults/main.yml | 36 ++++++ roles/virt/handlers/main.yml | 6 + roles/virt/meta/argument_specs.yml | 121 ++++++++++++++++++ roles/virt/tasks/guest/_preflight.yml | 61 +++++++++ roles/virt/tasks/guest/create_disks.yml | 63 +++++++++ roles/virt/tasks/guest/create_guest.yml | 110 ++++++++++++++++ roles/virt/tasks/guest/destroy.yml | 34 +++++ roles/virt/tasks/guest/fetch_image.yml | 50 ++++++++ roles/virt/tasks/guests.yml | 82 ++++++++++++ roles/virt/tasks/hardening.yml | 23 ++++ roles/virt/tasks/install.yml | 52 ++++++++ roles/virt/tasks/main.yml | 19 +++ roles/virt/templates/guest.xml.j2 | 85 ++++++++++++ roles/virt/templates/network-config.yml.j2 | 29 +++++ roles/virt/templates/user-data.yml.j2 | 45 +++++++ roles/virt/vars/main.yml | 12 ++ 31 files changed, 1146 insertions(+) create mode 100644 group_vars/hypervisor/secrets.yml create mode 100644 group_vars/roy/main.yml create mode 100644 playbooks/virtualization.yml create mode 100644 roles/monitor/tasks/_rewrite_prom_config.yml create mode 100644 roles/prometheus_libvirt_exporter/README.md create mode 100644 roles/prometheus_libvirt_exporter/defaults/main.yml create mode 100644 roles/prometheus_libvirt_exporter/handlers/main.yml create mode 100644 roles/prometheus_libvirt_exporter/meta/argument_specs.yml create mode 100644 roles/prometheus_libvirt_exporter/tasks/main.yml create mode 100644 roles/prometheus_libvirt_exporter/templates/prometheus.service.j2 create mode 100644 roles/virt/README.md create mode 100644 roles/virt/defaults/main.yml create mode 100644 roles/virt/handlers/main.yml create mode 100644 roles/virt/meta/argument_specs.yml create mode 100644 roles/virt/tasks/guest/_preflight.yml create mode 100644 roles/virt/tasks/guest/create_disks.yml create mode 100644 roles/virt/tasks/guest/create_guest.yml create mode 100644 roles/virt/tasks/guest/destroy.yml create mode 100644 roles/virt/tasks/guest/fetch_image.yml create mode 100644 roles/virt/tasks/guests.yml create mode 100644 roles/virt/tasks/hardening.yml create mode 100644 roles/virt/tasks/install.yml create mode 100644 roles/virt/tasks/main.yml create mode 100644 roles/virt/templates/guest.xml.j2 create mode 100644 roles/virt/templates/network-config.yml.j2 create mode 100644 roles/virt/templates/user-data.yml.j2 create mode 100644 roles/virt/vars/main.yml diff --git a/group_vars/all/main.yml b/group_vars/all/main.yml index bc0ae1c3..fcb9a8d8 100644 --- a/group_vars/all/main.yml +++ b/group_vars/all/main.yml @@ -1,4 +1,7 @@ --- + +ansible_managed: "Ansible managed file, do not edit directly" + git_repo: https://github.com/okfde/fragdenstaat_de.git base_ssh_port: "2228" diff --git a/group_vars/hypervisor/main.yml b/group_vars/hypervisor/main.yml index 33656a01..6bc797cd 100644 --- a/group_vars/hypervisor/main.yml +++ b/group_vars/hypervisor/main.yml @@ -2,3 +2,5 @@ network_bridge_vlan: "br1" network_bridge_physical: "br2" + +virt_guest_default_password: "{{ secret_virt_guest_default_password }}" diff --git a/group_vars/hypervisor/secrets.yml b/group_vars/hypervisor/secrets.yml new file mode 100644 index 00000000..5cad94fa --- /dev/null +++ b/group_vars/hypervisor/secrets.yml @@ -0,0 +1,9 @@ +$ANSIBLE_VAULT;1.1;AES256 +33373064316433636366386230636635346362656364623064333536336330323730343237656232 +3665383538393861653436333533643263393432663934310a663533343337626561636661636436 +34393635396233323566393161313330643639643163646339343033646261323533646633373563 +3933396262633236340a326230656236306261643066643833646362353436303564656533396533 +33363339616637633531386661326338626562323639613633316561626335613231623766306561 +33626331616536376334333066656335623335303136306132373861646633386263393635333862 +61306131343333326632306636383130613439343535363663613365373232646264653362313839 +37306365613037663032 diff --git a/group_vars/roy/main.yml b/group_vars/roy/main.yml new file mode 100644 index 00000000..bfaeaa61 --- /dev/null +++ b/group_vars/roy/main.yml @@ -0,0 +1,3 @@ +--- + +ansible_ssh_common_args: "-o ProxyJump=roy.okfn.de" diff --git a/inventory b/inventory index b6be9405..fe26bed4 100644 --- a/inventory +++ b/inventory @@ -98,3 +98,8 @@ schoch.okfn.de [hypervisor] roy.okfn.de + +[roy] + +[guest:children] +roy diff --git a/playbooks/virtualization.yml b/playbooks/virtualization.yml new file mode 100644 index 00000000..89f1dfc2 --- /dev/null +++ b/playbooks/virtualization.yml @@ -0,0 +1,9 @@ +--- + +- name: Virtualization + hosts: hypervisor + roles: + - role: virt + tags: virt + - role: prometheus_libvirt_exporter + tags: prometheus_libvirt_exporter diff --git a/roles/monitor/tasks/_rewrite_prom_config.yml b/roles/monitor/tasks/_rewrite_prom_config.yml new file mode 100644 index 00000000..f3f9d18e --- /dev/null +++ b/roles/monitor/tasks/_rewrite_prom_config.yml @@ -0,0 +1,17 @@ +--- + +# Task is split out of ./monitor.yml to allow for easier reconciliation of scrape targets when adding or removing exporters. +# Include in other roles like this: +# ``` +# - name: Reconcile prometheus scrape targets +# ansible.builtin.include_role: +# name: monitor +# tasks_from: _rewrite_prom_config +# delegate_to: fds-mon.okfn.de +# ``` +- name: Rewrite prometheus.yml + ansible.builtin.template: + src: prometheus.yml.j2 + dest: "{{ monitor_dir }}/prometheus/etc/prometheus.yml" + mode: "0644" + notify: Restart prometheus diff --git a/roles/monitor/templates/prometheus.yml.j2 b/roles/monitor/templates/prometheus.yml.j2 index b78ab19c..125e59a9 100644 --- a/roles/monitor/templates/prometheus.yml.j2 +++ b/roles/monitor/templates/prometheus.yml.j2 @@ -35,3 +35,7 @@ scrape_configs: - job_name: "celery_exporter" static_configs: - targets: [{% for host in groups['all'] %}{% if hostvars[host].prometheus_exporter is defined and 'celery_exporter' in hostvars[host].prometheus_exporter %}"{{ hostvars[host].host_data.site_ipv4 }}:9808"{% endif %}{% if not loop.last %}{% if hostvars[loop.nextitem].prometheus_exporter is defined and 'celery_exporter' in hostvars[loop.nextitem].prometheus_exporter %},{% endif %}{% endif %}{% endfor %}] + + - job_name: "libvirt_exporter" + static_configs: + - targets: [{% for host in groups['hypervisor'] %}"{{ hostvars[host].host_data.site_ipv4 }}:9177"{% if not loop.last %},{% endif %}{% endfor %}] diff --git a/roles/prometheus_libvirt_exporter/README.md b/roles/prometheus_libvirt_exporter/README.md new file mode 100644 index 00000000..91edfc51 --- /dev/null +++ b/roles/prometheus_libvirt_exporter/README.md @@ -0,0 +1,3 @@ +# prometheus-libvirt-exporter + +This role installs the promtheus-libvirt-exporter binary from https://github.com/inovex/prometheus-libvirt-exporter and creates a systemd unit file. We do this manually since the ubuntu package of the same name is outdated and unmaintained. diff --git a/roles/prometheus_libvirt_exporter/defaults/main.yml b/roles/prometheus_libvirt_exporter/defaults/main.yml new file mode 100644 index 00000000..fc7fee51 --- /dev/null +++ b/roles/prometheus_libvirt_exporter/defaults/main.yml @@ -0,0 +1,4 @@ +--- + +# TODO: Track dependency with renovate +prometheus_libvirt_exporter_version: "2.6.2" diff --git a/roles/prometheus_libvirt_exporter/handlers/main.yml b/roles/prometheus_libvirt_exporter/handlers/main.yml new file mode 100644 index 00000000..6b7f2e26 --- /dev/null +++ b/roles/prometheus_libvirt_exporter/handlers/main.yml @@ -0,0 +1,10 @@ +--- + +- name: Restart prometheus-libvirt-exporter + ansible.builtin.systemd: + name: prometheus-libvirt-exporter + state: restarted + +- name: Reload daemon + ansible.builtin.systemd: + daemon_reload: true diff --git a/roles/prometheus_libvirt_exporter/meta/argument_specs.yml b/roles/prometheus_libvirt_exporter/meta/argument_specs.yml new file mode 100644 index 00000000..e737abed --- /dev/null +++ b/roles/prometheus_libvirt_exporter/meta/argument_specs.yml @@ -0,0 +1,9 @@ +--- + +argument_specs: + main: + options: + prometheus_libvirt_exporter_version: + type: "str" + required: true + description: "Version of prometheus-libvirt-exporter." diff --git a/roles/prometheus_libvirt_exporter/tasks/main.yml b/roles/prometheus_libvirt_exporter/tasks/main.yml new file mode 100644 index 00000000..adbe5c11 --- /dev/null +++ b/roles/prometheus_libvirt_exporter/tasks/main.yml @@ -0,0 +1,93 @@ +--- + +- name: Install prometheus-libvirt-exporter systemd file + ansible.builtin.template: + src: prometheus.service.j2 + dest: /etc/systemd/system/prometheus-libvirt-exporter.service + mode: "0644" + notify: Reload daemon + +- name: Flush handlers + ansible.builtin.meta: flush_handlers + +# TODO: Extract logic into generic role for verifying, fetching and extracting raw binaries from upstream repositories +- name: Check if prometheus-libvirt-exporter is installed + ansible.builtin.stat: + path: "/usr/local/bin/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}-linux-amd64" + register: _exporter_installed + +- name: Install prometheus-libvirt-exporter + when: > + _exporter_installed.stat.exists is false + notify: Restart prometheus-libvirt-exporter + block: + - name: Create tmpdir + ansible.builtin.tempfile: + state: directory + check_mode: false + register: _download_exporter + + - name: Download prometheus-libvirt-exporter + ansible.builtin.get_url: + url: "https://github.com/inovex/prometheus-libvirt-exporter/releases/download/v{{ prometheus_libvirt_exporter_version }}/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}.linux-amd64.tar.gz" + checksum: "sha256:https://github.com/inovex/prometheus-libvirt-exporter/releases/download/v{{ prometheus_libvirt_exporter_version }}/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}_checksums.txt" + dest: "{{ _download_exporter.path }}" + mode: "0644" + check_mode: false + + - name: Unarchive prometheus-libvirt-exporter + ansible.builtin.unarchive: + remote_src: true + src: "{{ _download_exporter.path }}/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}.linux-amd64.tar.gz" + dest: "{{ _download_exporter.path }}" + check_mode: false + + - name: Install prometheus-libvirt-exporter binary + ansible.builtin.copy: + remote_src: true + src: "{{ _download_exporter.path }}/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}.linux-amd64/prometheus-libvirt-exporter" + dest: "/usr/local/bin/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}-linux-amd64" + mode: "0755" + + always: + - name: Remove tmpdir + ansible.builtin.file: + path: "{{ _download_exporter }}" + state: absent + +- name: Find outdated versions of prometheus-libvirt-exporter + ansible.builtin.find: + paths: "/usr/local/bin" + file_type: file + excludes: + - "prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}" + patterns: "prometheus-libvirt-exporter.*" + use_regex: true + register: _prometheus_outdated + +- name: Remove outdated binaries + ansible.builtin.file: + path: "{{ item }}" + state: absent + loop: "{{ _prometheus_outdated.files | default([]) | map(attribute='path') | sort }}" + +- name: Ensure prometheus-libvirt-exporter is running + ansible.builtin.systemd: + name: prometheus-libvirt-exporter + state: started + enabled: true + +- name: Configure firewall for prometheus-libvirt-exporter + community.general.ufw: + port: "9177" + proto: tcp + from_ip: 192.168.100.5 + rule: allow + +- name: Reconcile prometheus scrape targets + ansible.builtin.include_role: + name: monitor + tasks_from: _rewrite_prom_config.yml + apply: + delegate_to: fds-mon.okfn.de + delegate_facts: true diff --git a/roles/prometheus_libvirt_exporter/templates/prometheus.service.j2 b/roles/prometheus_libvirt_exporter/templates/prometheus.service.j2 new file mode 100644 index 00000000..4cd573ca --- /dev/null +++ b/roles/prometheus_libvirt_exporter/templates/prometheus.service.j2 @@ -0,0 +1,29 @@ +{{ ansible_managed | comment }} +[Unit] +Description=Prometheus-libvirt-exporter service (host and vm metrics exposed for prometheus, written in Go with pluggable metric collectors) +Requires=network-online.target +After=network-online.target + +[Service] +ExecStart=/usr/local/bin/prometheus-libvirt-exporter-{{ prometheus_libvirt_exporter_version }}-linux-amd64 --web.listen-address ":9177" +ExecReload=/bin/kill -HUP $MAINPID +Restart=on-failure +KillSignal=SIGINT + +# Security +PrivateTmp=yes +ProtectClock=yes +ProtectControlGroups=yes +ProtectHome=yes +ProtectHostname=yes +ProtectKernelLogs=yes +ProtectKernelModules=yes +ProtectKernelTunables=yes +RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6 AF_NETLINK +RestrictNamespaces=yes +RestrictRealtime=yes +RestrictSUIDSGID=yes +SystemCallArchitectures=native + +[Install] +WantedBy=multi-user.target diff --git a/roles/virt/README.md b/roles/virt/README.md new file mode 100644 index 00000000..07a1c9a2 --- /dev/null +++ b/roles/virt/README.md @@ -0,0 +1,118 @@ +# Libvirt role + +Role for installation of libvirt/qemu and management of guest vms. + +## Variables + +See `defaults/main.yml` for defaults and `meta/argument_specs.yml` for valid values + +## Creating new guests + +1. Add guest to `virt_guests`: + ``` + # host_vars/roy.okfn.de/main.yml + + virt_guests: + - name: guest1.okfn.de + cpus: 3 + memory: 4096 + disk_size: 50 + ``` + +2. Add guest to the inventory below the group of the respective hypervisor: + ``` + # inventory + [roy] + guest1.okfn.de + ``` + +3. At least the internal vlan IPv4 address needs to be defined in `network_networks.pivate` in the host vars for the new guest. Addresses *must* include the netmask in CIDR notation (e.g. /24) + `network_networks.public` can only be defined if it contains all relevant variables to configure a public interface for the vm, otherwise it must be absent. + ``` + # host_vars + network_networks: + private: + address4: 192.168.100.103/24 + public: + mac: 00:50:56:00:FD:9C + address4: 5.9.108.212/27 + gateway4: 5.9.108.193 + address6: "2a01:4f8:162:44c7::3/64" + gateway6: 'fe80::1' + ``` + +4. Run playbook + ``` + ansible-playbook playbooks/virtualization.yml -t virt_guests --diff + ``` + +## Delete and recreate guests + +If you wish to delete a guest, remove it from `virt_guests`, the inventory file and its hostvars and run the playbook with `virt_guests_delete` including the guests to remove. This will also reconcile inventory configuration, like removing its entriy in other hosts `/etc/hosts`. + +``` +ansible-playbook playbooks/virtualization.yml -t virt_guests --diff -e '{ "virt_guests_delete":["guest1.okfn.de"] }' +``` + +Running the above command _without_ removing the guest from the hostvars and inventory will purge the storage and libvirt state and create and provision the guest from scratch, without attempting to reconcile the inventory configuration. + +## Disk virtualization + +### ZFS + +The ZFS file system is used to provision disks for virtualization. A pre-existing pool can be defined via the `virt_zpool` variable or created if it does not exist. + +Within the pool _datasets_ are created. A zfs _datasets_ can be _filesystems_ or a _zvolume_. +For caching cloud images (qcow2 iso files) a filesystem (`pool/images`) is created and mounted to `/srv/zpool/images`. _zfs filesystems_ have no fixed size. +Volumes are stored in the zfs dataset `pool/volumes` and each created as _zvolume_. In contrast to _zfs filesystems_, _zvolumes_ are block devices with with a fixed size and are mapped to `/dev/zvol//`. + +This roles does not make use of the libvirt wrapper for zfs, mainly because its was frustrated with its interface. +Instead of creating pool and volumes with virsh (`virsh pool-define-as ...` or `virsh vol-create-as `) or passing a _zvol_ to libvirt as a _volume_ (`virt-install --disk vol=/`) _zvols_ are managed directly with `zfs` and are passed to libvirt as block devices, i.e. `virt-install --disk path=/dev/zvol/volumes/host1.okfn.de/root`. + +### Defining disks + +Suppose the following guest configuration: + +``` +virt_guests: + - name: guest1.okfn.de + disksize: 20 + [...] + additional_disks: + - name: data + size: 300 + target: vdb + - name: db + size: 30 + target: vdc +``` + +The following datasets will be created on the host: +``` +% zfs list | grep guest.1 +pool/volumes/guest1.okfn.de +pool/volumes/guest1.okfn.de/root +pool/volumes/guest1.okfn.de/data +pool/volumes/guest1.okfn.de/db +``` + +All disks that are defined when the guest is created are mounted inside the guest according to their name. +``` +device target mountpoint +root vda / +data vdb /mnt/data +db vdc /mnt/db +``` + +Note that, while even additional disks that are defined for a guest _after_ its creation are provisioned by this role, attaching them to the guest and creating mountpoints inside the guest requires manual steps. + +### Snapshots + +The zvolumes are created as nested datasets under the respective `` to simplify snapshot operations in the zfs filesystem: +``` +# Create snapshot for only the `data` disk: +zfs snapshot pool/volumes/guest1.okfn.de/data@$(date "+%Y-%m-%d") + +# Create snapshot for all disk: +zfs snapshot pool/volumes/guest1.okfn.de@$(date "+%Y-%m-%d") +``` diff --git a/roles/virt/defaults/main.yml b/roles/virt/defaults/main.yml new file mode 100644 index 00000000..eb249c6d --- /dev/null +++ b/roles/virt/defaults/main.yml @@ -0,0 +1,36 @@ +--- + +# host defaults +# +virt_bridge_default: "virbr0" +virt_bridge_physical: "{{ network_bridge_physical | default('br2') }}" +virt_bridge_vlan: "{{ network_bridge_vlan | default('br1') }}" + +virt_packages: + - bridge-utils + - guestfs-tools + - libvirt-clients + - libvirt-daemon + - libvirt-daemon-system + - ovmf + - python3-libvirt + - python3-lxml + - qemu-system-x86 + - qemu-utils + - zfsutils-linux + - virt-install + +# Force download of current cloud image +virt_image_refresh: false +virt_ubuntu_image_version: resolute + +virt_zpool_mount: "/srv/zpool" +virt_zpool: "pool" +virt_zpool_vdev: "/dev/mapper/vg0-pool" + + +# guest defaults +virt_guests: [] +virt_guests_delete: [] +virt_guests_default_cpus: 2 +virt_guests_default_memory: 2046 diff --git a/roles/virt/handlers/main.yml b/roles/virt/handlers/main.yml new file mode 100644 index 00000000..b0dbd06a --- /dev/null +++ b/roles/virt/handlers/main.yml @@ -0,0 +1,6 @@ +--- + +- name: Restart libvirtd + ansible.builtin.systemd: + name: libvirtd + state: restarted diff --git a/roles/virt/meta/argument_specs.yml b/roles/virt/meta/argument_specs.yml new file mode 100644 index 00000000..ce947736 --- /dev/null +++ b/roles/virt/meta/argument_specs.yml @@ -0,0 +1,121 @@ +--- + +argument_specs: + # roles/libvirt/tasks/main.yml entry point + main: + options: + virt_guests_default_memory: + type: int + required: true + description: "default memory for new guests in mb. override in `virt_guests.[].memory`" + + virt_guests_default_cpus: + type: int + required: true + description: "default nummber of cpus for new guests. override in `virt_guests.[].cpus`" + + virt_zpool: + type: str + required: true + description: "name of the zfs storage pool to use for image and device storage" + + virt_zpool_mount: + type: str + required: true + description: "mountpoint for the zfs pool" + + virt_zpool_vdev: + type: str + required: true + description: "block device " + + virt_bridge_default: + type: str + required: true + description: "name of the default NAT network for libvirt guests" + + virt_bridge_vlan: + type: str + required: true + description: "name of the vlan network bridge" + + virt_bridge_physical: + type: str + required: true + description: "name of the wan network bridge" + + # Force download of current cloud image + virt_image_refresh: + type: bool + required: true + description: "wether to download a current ubuntu cloud image (will use cached otherwise)" + + virt_ubuntu_image_version: + type: str + required: true + description: "version string of the ubuntu image to use for guest provisioning." + + virt_guests: + type: "list" + elements: "dict" + required: true + description: "A list of guests vms." + options: + name: + type: "str" + required: true + description: "fqdn of the name as it will be defined in the inventory file" + + cpus: + type: "str" + required: false + description: "Number of virtual CPUs." + + memory: + type: "int" + required: false + description: "Amount of virtual memory" + + disk_size: + type: "int" + required: true + description: "Size of root disk (mounted to /dev/vda inside the guest)" + + state: + type: "str" + choices: + - "destroyed" + - "paused" + - "running" + - "shutdown" + required: false + + autostart: + type: bool + required: false + + additional_disks: + type: "list" + elements: "dict" + required: false + description: "List of additional devices to create." + options: + name: + type: "str" + required: true + description: "Name of the disk on the host (for example, 'data')" + size: + type: "int" + required: true + description: "size of the disk" + target: + type: "str" + required: true + description: "target device of the disk (i.e. vdb, vdc etc)" + + virt_guests_delete: + type: "list" + elements: "str" + required: false + default: [] + description: "A list of guest to delete." diff --git a/roles/virt/tasks/guest/_preflight.yml b/roles/virt/tasks/guest/_preflight.yml new file mode 100644 index 00000000..67f8a4d5 --- /dev/null +++ b/roles/virt/tasks/guest/_preflight.yml @@ -0,0 +1,61 @@ +--- + +- name: Assert that guest is configured in inventory + ansible.builtin.assert: + quiet: true + that: + - guest.name in groups[inventory_hostname | split('.') | first] + fail_msg: | + New guest `{{ guest.name }}` shall be provisioned but `{{ guest.name }}` does not seem to exist in inventory. + Please add `{{ guest.name }}` to inventory file and make sure to assign it to the group `{{ inventory_hostname | split('.') | first }}` + +- name: Validate hostfacts for new guest + ansible.builtin.validate_argument_spec: + provided_arguments: + network_networks: "{{ hostvars[guest.name]['network_networks'] | default({}) }}" + argument_spec: + network_networks: + type: dict + retuired: true + options: + private: + type: dict + required: true + options: + address4: + type: "str" + public: + type: dict + required: false + options: + mac: + type: "str" + required: true + address4: + type: "str" + required: true + address6: + type: "str" + required: true + gateway4: + type: "str" + required: true + gateway6: + type: "str" + required: true + +- name: Assert we have a unique internal ipv4 + ansible.builtin.assert: + quiet: true + that: > + hostvars[guest.name]['network_networks']['private']['address4'] | split('/') | first not in + (hostvars | dict2items | map(attribute='value') | selectattr('hostname','ne',guest.name) | map(attribute='host_data.site_ipv4')) + msg: | + Internal vlan IP address for `{{ guest.name }}` is already taken. + +- name: Generate mac addresses for internal networks + ansible.builtin.set_fact: + guest_mac_lan: "{{ '00' | community.general.random_mac(seed=guest.name) }}" + guest_mac_vlan: "{{ '02' | community.general.random_mac(seed=guest.name) }}" + delegate_to: "{{ guest.name }}" + delegate_facts: true diff --git a/roles/virt/tasks/guest/create_disks.yml b/roles/virt/tasks/guest/create_disks.yml new file mode 100644 index 00000000..d2fd9ae7 --- /dev/null +++ b/roles/virt/tasks/guest/create_disks.yml @@ -0,0 +1,63 @@ +--- + +# community.general.zfs prior to 14.0.0 will force recreate a volume if its volsize property has changed, instead of throwing an error. +# It's ugly and further complicates the code, but we `stat` the block device path and conditionally run the creation task +# TODO: Replace stat check with `create_only_mismatch: fail` when community.general >= 14.0.0 +- name: Check if root disk exists + ansible.builtin.stat: + path: "{{ virt_volumes_mapper }}/{{ guest.name }}/root" + register: _root_stat + +- name: Ensure root disk is created + community.general.zfs: + name: "{{ virt_volumes_dataset }}/{{ guest.name }}/root" + state: present + extra_zfs_properties: + volsize: "{{ guest.disk_size }}G" + register: _root_disk + when: _root_stat.stat.exists is false + +- name: Copy qcow image to root disk # noqa no-handler (does not got well with 'include_tasks') + ansible.builtin.command: + cmd: "qemu-img convert {{ virt_images_dir }}/{{ virt_ubuntu_image }} /dev/zvol/{{ virt_volumes_dataset }}/{{ guest.name }}/root" + when: _root_disk is defined and _root_disk is changed + changed_when: true + +# TODO: Replace stat check with `create_only_mismatch: fail` when community.general >= 14.0.0 +- name: Check for each additional disk if exists + ansible.builtin.stat: + path: "{{ virt_volumes_mapper }}/{{ guest.name }}/{{ disk.name }}" + loop: "{{ guest.additional_disks }}" + loop_control: + loop_var: disk + label: "{{ guest.name }}/{{ disk.name }}" + register: _additional_disks_stat + when: + - guest.additional_disks is defined and guest.additional_disks | length > 0 + +- name: Note existing disks + ansible.builtin.debug: + msg: "zvol already exists. Nothing to do." + loop: "{{ guest.additional_disks }}" + loop_control: + loop_var: disk + label: "{{ guest.name }}/{{ disk.name }}" + when: + - guest.additional_disks is defined and guest.additional_disks | length > 0 + - _additional_disks_stat is defined + - _additional_disks_stat.results | selectattr('disk.name','equalto',disk.name) | map(attribute='stat.exists') | default({}) | list | first is true + +- name: Ensure additional disks are created + community.general.zfs: + name: "{{ virt_volumes_dataset }}/{{ guest.name }}/{{ disk.name }}" + state: present + extra_zfs_properties: + volsize: "{{ disk.size }}G" + loop: "{{ guest.additional_disks }}" + loop_control: + loop_var: disk + label: "{{ guest.name }}/{{ disk.name }}" + when: + - guest.additional_disks is defined and guest.additional_disks | length > 0 + - _additional_disks_stat is defined + - _additional_disks_stat.results | selectattr('disk.name','equalto',disk.name) | map(attribute='stat.exists') | default({}) | list | first is false diff --git a/roles/virt/tasks/guest/create_guest.yml b/roles/virt/tasks/guest/create_guest.yml new file mode 100644 index 00000000..1b5a1ec1 --- /dev/null +++ b/roles/virt/tasks/guest/create_guest.yml @@ -0,0 +1,110 @@ +--- + +- name: Assert that host variables are set for new guests + ansible.builtin.import_tasks: _preflight.yml + +- name: Create cloud-init tmpdir + ansible.builtin.tempfile: + state: directory + register: _tmpdir + +- name: Fix ownership + ansible.builtin.file: + path: "{{ _tmpdir.path }}" + owner: libvirt-qemu + group: libvirt-qemu + +- name: Cloud-init - Write user-data + ansible.builtin.template: + src: user-data.yml.j2 + dest: "{{ _tmpdir.path }}/user-data" + mode: "0600" + validate: cloud-init schema --config-file=%s --annotate + no_log: true + +- name: Cloud-init - Write network-config + ansible.builtin.template: + src: network-config.yml.j2 + dest: "{{ _tmpdir.path }}/network-config" + mode: "0600" + validate: cloud-init schema --config-file=%s --schema-type network-config --annotate + +- name: Create meta-data file + ansible.builtin.copy: + content: | + local-hostname: {{ guest.name }} + dest: "{{ _tmpdir.path }}/meta-data" + mode: "0600" + +- name: Create seed iso with cloud init user-data + ansible.builtin.command: + cmd: cloud-localds -N network-config seed.iso user-data meta-data + chdir: "{{ _tmpdir.path }}" + creates: "{{ _tmpdir.path }}/seed.iso" + +- name: Define guest from xml + community.libvirt.virt: + command: define + xml: "{{ lookup('template', 'guest.xml.j2') }}" + +- name: Start guest + community.libvirt.virt: + name: "{{ guest.name }}" + state: running + +- name: Add host name to hosts file + ansible.builtin.lineinfile: + path: "/etc/hosts" + regex: ".*(\ {{ guest.name }})?\ {{ guest.name | split('.') | first }}?$" + line: "{{ hostvars[guest.name].host_data.site_ipv4 }} {{ guest.name }} {{ guest.name | split('.') | first }}" + state: present + +# For some reason, cloud init is able to reconfigure sshd to use `base_ssh_port` but cannot +# restart sshd. Therefore: Wait for guest to be online on default port 🤷‍♀️ +- name: Wait for guest to come online + ansible.builtin.wait_for: + port: 22 + host: "{{ guest.name }}" + search_regex: OpenSSH + delay: 10 + timeout: 300 + register: _guest_online + +- name: Detach cloud init disk + ansible.builtin.command: + cmd: virsh change-media {{ guest.name }} hda --eject --config --live + when: _guest_online is not failed + changed_when: false + +- name: Remove cloud-init tmpdir + ansible.builtin.file: + path: "{{ _tmpdir.path }}" + state: absent + +- name: Reboot guest + ansible.builtin.command: + cmd: "virsh reboot {{ guest.name }}" + changed_when: false + +- name: Wait for guest to come online + ansible.builtin.wait_for: + port: "{{ hostvars[guest.name].base_ssh_port }}" + host: "{{ guest.name }}" + search_regex: OpenSSH + delay: 10 + timeout: 300 + register: _guest_online_again + +- name: Success + ansible.builtin.debug: + msg: | + `{{ guest.name }} was successfully provisioned on `{{ inventory_hostname }}`. + + You might need to update your ssh config in order to connect to it: + Host {{ guest.name }} + Hostname {{ guest.name }} + User + Port 2228 + ProxyJump {{ inventory_hostname }} + when: _guest_online_again is success + changed_when: true diff --git a/roles/virt/tasks/guest/destroy.yml b/roles/virt/tasks/guest/destroy.yml new file mode 100644 index 00000000..aa5f95a7 --- /dev/null +++ b/roles/virt/tasks/guest/destroy.yml @@ -0,0 +1,34 @@ +--- + +- name: Destroy guest # noqa ignore-errors - will fail if is not present + community.libvirt.virt: + name: "{{ guest_name }}" + state: "destroyed" + ignore_errors: true + +- name: Undefine guest # noqa ignore-errors - will fail if is not present + community.libvirt.virt: + name: "{{ guest_name }}" + command: "undefine" + ignore_errors: true + +- name: Recursively destroy disks + community.general.zfs: + name: "{{ virt_volumes_dataset }}/{{ guest_name }}" + state: absent + +- name: Remove host name from /etc/hosts + ansible.builtin.lineinfile: + path: "/etc/hosts" + regex: ".*(\ {{ guest_name }})?\ {{ guest_name | split('.') | first }}?$" + state: absent + delegate_to: "{{ host }}" + loop: "{{ groups['all'] }}" + loop_control: + loop_var: host + when: + - host != guest_name + - > + hostvars[guest_name] is undefined or + hostvars[guest_name]['host_data'] is undefined or + hostvars[guest_name]['host_data']['site_ipv4'] is undefined diff --git a/roles/virt/tasks/guest/fetch_image.yml b/roles/virt/tasks/guest/fetch_image.yml new file mode 100644 index 00000000..35aae249 --- /dev/null +++ b/roles/virt/tasks/guest/fetch_image.yml @@ -0,0 +1,50 @@ +--- + +- name: Check if ubuntu image is cached + ansible.builtin.stat: + path: "{{ virt_images_dir }}/{{ virt_ubuntu_image }}" + register: iso_image + +- name: Download ubuntu image + when: + - iso_image.stat.exists is false or virt_image_refresh is true + block: + - name: Create temporary download directory + ansible.builtin.tempfile: + state: directory + suffix: "{{ virt_ubuntu_image }}" + register: tmpdir + + - name: Download checksum file and signature + ansible.builtin.get_url: + url: "https://cloud-images.ubuntu.com/{{ virt_ubuntu_image_version }}/current/{{ item }}" + dest: "{{ tmpdir.path }}" + mode: "0644" + loop: + - "SHA256SUMS" + - "SHA256SUMS.gpg" + - "{{ virt_ubuntu_image }}" + + - name: Verify gpg signature + ansible.builtin.command: + cmd: "{{ item }}" + chdir: "{{ tmpdir.path }}" + loop: + - "gpg --homedir . --keyserver hkp://keyserver.ubuntu.com --recv-keys 0x{{ virt_ubuntu_signing_key }}" + - "gpg --homedir . --verify --batch SHA256SUMS.gpg SHA256SUMS" + - "sha256sum --ignore-missing -c SHA256SUMS" + changed_when: false + + - name: Store image + ansible.builtin.copy: + remote_src: true + src: "{{ tmpdir.path }}/{{ virt_ubuntu_image }}" + dest: "{{ virt_images_dir }}/{{ virt_ubuntu_image }}" + mode: "0644" + owner: root + group: root + + - name: Delete tmpdir + ansible.builtin.file: + path: "{{ tmpdir.path }}" + state: absent diff --git a/roles/virt/tasks/guests.yml b/roles/virt/tasks/guests.yml new file mode 100644 index 00000000..303db224 --- /dev/null +++ b/roles/virt/tasks/guests.yml @@ -0,0 +1,82 @@ +--- + +- name: Ensure ubuntu image exists + ansible.builtin.import_tasks: guest/fetch_image.yml + +- name: Destroy guest and disks + ansible.builtin.include_tasks: guest/destroy.yml + loop: "{{ virt_guests_delete }}" + loop_control: + loop_var: guest_name + label: "{{ guest_name }}" + +- name: Get list of existing guest on remote host + community.libvirt.virt: + command: list_vms + register: _all_vms + +- name: Provision disks + ansible.builtin.include_tasks: guest/create_disks.yml + loop: "{{ virt_guests }}" + loop_control: + loop_var: guest + label: "{{ guest.name }}" + +- name: Provision guests + ansible.builtin.include_tasks: guest/create_guest.yml + loop: "{{ virt_guests }}" + loop_control: + loop_var: guest + label: "{{ guest.name }}" + when: not guest.name in _all_vms.list_vms + +- name: Ensure guests are running and autostarted + community.libvirt.virt: + name: "{{ guest.name }}" + state: "{{ guest.state | default('running') }}" + autostart: "{{ guest.autostart | default(true) }}" + loop: "{{ virt_guests }}" + loop_control: + loop_var: guest + label: "{{ guest.name }}" + +- name: Get list of zvolumes + ansible.builtin.command: + cmd: "ls {{ virt_volumes_mapper }}" + register: _all_volumes + changed_when: false + +- name: Get list of residual guests and volumes + ansible.builtin.set_fact: + _virt_residual_vms: > + {{ _all_vms.list_vms + | ansible.builtin.difference(virt_guests | map(attribute='name') | list) }} + _virt_residual_volumes: > + {{ _all_volumes.stdout | default('') | split + | ansible.builtin.difference(virt_guests | map(attribute='name') | list) }} + +- name: Assert there are no residual guests # noqa ignore-errors (just throw a warning, don't error) + ansible.builtin.assert: + that: _virt_residual_vms | length == 0 + quiet: true + fail_msg: | + Guests present on remote host but not defined in host_vars: + {% for guest in _virt_residual_vms %} + - {{ guest }} + {% endfor %} + + Re-run playbook with `-e virt_guests_delete=['']` to purge residual guests. + ignore_errors: true + +- name: Assert there are no residual volumes # noqa ignore-errors (just throw a warning, don't error) + ansible.builtin.assert: + that: _virt_residual_volumes | length == 0 + quiet: true + fail_msg: | + Datasets for guest volumes present on remote host but not defined in host_vars: + {% for guest in _virt_residual_volumes %} + - {{ guest }} + {% endfor %} + + Re-run playbook with `-e virt_guests_delete=['']` to purge residual disks. + ignore_errors: true diff --git a/roles/virt/tasks/hardening.yml b/roles/virt/tasks/hardening.yml new file mode 100644 index 00000000..5ff75264 --- /dev/null +++ b/roles/virt/tasks/hardening.yml @@ -0,0 +1,23 @@ +--- + +- name: Ensure apparmor packages are installed + ansible.builtin.apt: + name: "{{ item }}" + state: present + loop: + - apparmor + - apparmor-utils + +- name: Configure security driver and confinement for libvirt + ansible.builtin.lineinfile: + path: /etc/libvirt/qemu.conf + regexp: "^#?{{ item.key }}.*$" + line: "{{ item.key }} = {{ item.value }}" + loop: + - key: "security_default_confined" + value: 1 + - key: "security_require_confined" + value: 1 + - key: "security_driver" + value: "[ \"apparmor\" ]" + notify: Restart libvirtd diff --git a/roles/virt/tasks/install.yml b/roles/virt/tasks/install.yml new file mode 100644 index 00000000..00841061 --- /dev/null +++ b/roles/virt/tasks/install.yml @@ -0,0 +1,52 @@ +--- + +- name: Install VM toolchain + ansible.builtin.apt: + name: "{{ virt_packages }}" + +- name: Ensure libvirtd is enabled and running + ansible.builtin.systemd: + name: libvirtd + state: started + enabled: true + +- name: Add users to libvirt group + ansible.builtin.user: + name: "{{ item }}" + groups: libvirt-qemu + append: true + loop: "{{ virt_users }}" + +- name: Configure zpool + community.general.zpool: + name: "{{ virt_zpool }}" + mountpoint: "{{ virt_zpool_mount }}" + force: false + filesystem_properties: + compression: lz4 + recordsize: 64K + vdevs: + - disks: "{{ virt_zpool_vdev }}" + +# TODO: Add `create_only_mismatch: fail` when community.general >= 14.0.0 +- name: Create zfs dataset for volume storage + community.general.zfs: + name: "{{ virt_volumes_dataset }}" + state: present + extra_zfs_properties: + mountpoint: none + +# TODO: Add `create_only_mismatch: fail` when community.general >= 14.0.0 +- name: Create zfs dataset for image storage + community.general.zfs: + name: "{{ virt_images_dataset }}" + state: present + extra_zfs_properties: + mountpoint: "{{ virt_images_dir }}" + +- name: Set permissions for image storage + ansible.builtin.file: + path: "{{ virt_images_dir }}" + mode: "0755" + owner: root + group: root diff --git a/roles/virt/tasks/main.yml b/roles/virt/tasks/main.yml new file mode 100644 index 00000000..b4e94b75 --- /dev/null +++ b/roles/virt/tasks/main.yml @@ -0,0 +1,19 @@ +--- + +- name: Import installation tasks + ansible.builtin.import_tasks: install.yml + tags: + - virt_install + +- name: Import hardening tasks + ansible.builtin.import_tasks: hardening.yml + tags: + - virt_hardening + +- name: Flush handlers + ansible.builtin.meta: flush_handlers + +- name: Import guest tasks + ansible.builtin.import_tasks: guests.yml + tags: + - virt_guests diff --git a/roles/virt/templates/guest.xml.j2 b/roles/virt/templates/guest.xml.j2 new file mode 100644 index 00000000..b53c03d2 --- /dev/null +++ b/roles/virt/templates/guest.xml.j2 @@ -0,0 +1,85 @@ + + {{ guest.name }} + {{ guest.memory | default(virt_guests_default_memory) }} + {{ guest.cpus | default(virt_guests_default_cpus) }} + + /machine + + + hvm + + + + + + + + /usr/bin/qemu-system-x86_64 +{# Start configure disks #} + + + + + + + + + + + + +{% if guest.additional_disks is defined %} +{% for disk in guest.additional_disks | sort(attribute='target') %} + + + + +{% endfor %} +{% endif %} +{# End configure disks #} +{# Start configure network devices #} +{% set shortname = guest.name | ansible.builtin.split('.') | first %} +{# Interface attached to default bridge is named l- (=local) on host #} + + + + + +
+ +{# Interface attached to br1 will be named v- (=vlan) on host #} + + + + + +
+ +{% set _configure_wan = hostvars[guest.name]['network_networks']['public'] is defined | bool %} +{% if _configure_wan is true %} +{# Interface attached to br2 will be named w- (=wan) on host #} + + + + + +
+ +{% endif %} +{# Stop configure network devices #} +{# Start configure serial console #} + + + + + + + + + + + + +{# End configure serial console #} + + diff --git a/roles/virt/templates/network-config.yml.j2 b/roles/virt/templates/network-config.yml.j2 new file mode 100644 index 00000000..eb42a8b3 --- /dev/null +++ b/roles/virt/templates/network-config.yml.j2 @@ -0,0 +1,29 @@ +network: + version: 2 + renderer: networkd + ethernets: + net0: + match: + macaddress: '{{ hostvars[guest.name]['guest_mac_lan'] }}' + dhcp4: true + dhcp6: false + net1: + match: + macaddress: '{{ hostvars[guest.name]['guest_mac_vlan'] }}' + dhcp4: false + dhcp6: false + addresses: + - {{ hostvars[guest.name]['network_networks']['private']['address4'] }} +{% set _configure_net2 = hostvars[guest.name]['network_networks']['public'] is defined | bool %} +{% if _configure_net2 is true %} + net2: + match: + macaddress: '{{ hostvars[guest.name]['network_networks']['public']['mac'] }}' + dhcp4: false + dhcp6: false + addresses: + - {{ hostvars[guest.name]['network_networks']['public']['address4'] }} + - {{ hostvars[guest.name]['network_networks']['public']['address6'] }} + gateway4: {{ hostvars[guest.name]['network_networks']['public']['gateway4'] }} + gateway6: {{ hostvars[guest.name]['network_networks']['public']['gateway6'] }} +{% endif %} diff --git a/roles/virt/templates/user-data.yml.j2 b/roles/virt/templates/user-data.yml.j2 new file mode 100644 index 00000000..64a524fd --- /dev/null +++ b/roles/virt/templates/user-data.yml.j2 @@ -0,0 +1,45 @@ +#cloud-config +password: {{ virt_guest_default_password }} +chpasswd: + expire: False + +growpart: + mode: auto + devices: [\"/\"] + ignore_growroot_disabled: false + +users: + - default +{% for user in hostvars[guest.name]['base_ssh_users'] %} + - name: {{ user.name }} + groups: sudo + lock_passwd: true + sudo: "ALL=(ALL) NOPASSWD:ALL" +{% if user.public_keys is defined and user.public_keys | length > 0 %} + ssh_authorized_keys: +{% for pubkey in user.public_keys %} + - {{ pubkey }} +{% endfor %} +{% endif %} +{% endfor %} + +{# configure sshd #} +runcmd: + - [ sed, -i, 's/^[#]*Port.*$/Port {{ hostvars[guest.name]['base_ssh_port'] }}/g', /etc/ssh/sshd_config ] + +{% if guest.additional_disks is defined and guest.additional_disks | length > 0 %} +device_aliases: +{% for disk in guest.additional_disks %} + {{ disk.name }}: /dev/{{ disk.target }} +{% endfor %} +fs_setup: +{% for disk in guest.additional_disks %} + - label: {{ disk.name }} + filesystem: 'ext4' + device: /dev/{{ disk.target }}] +{% endfor %} +mounts: +{% for disk in guest.additional_disks %} + - [{{ disk.name }}, /mnt/{{ disk.name }}] +{% endfor %} +{% endif %} diff --git a/roles/virt/vars/main.yml b/roles/virt/vars/main.yml new file mode 100644 index 00000000..66270d76 --- /dev/null +++ b/roles/virt/vars/main.yml @@ -0,0 +1,12 @@ +--- + +virt_users: "{{ base_ssh_users | map(attribute='name') }}" + +virt_images_dataset: "{{ virt_zpool }}/images" +virt_images_dir: "{{ virt_zpool_mount }}/images" + +virt_volumes_dataset: "{{ virt_zpool }}/volumes" +virt_volumes_mapper: "/dev/zvol/{{ virt_volumes_dataset }}" + +virt_ubuntu_image: "{{ virt_ubuntu_image_version }}-server-cloudimg-amd64.img" +virt_ubuntu_signing_key: D2EB44626FDDC30B513D5BB71A5D6C4C7DB87C81 From 83fa8ae8978ca4ea717ad59b5324eb44802541c7 Mon Sep 17 00:00:00 2001 From: Peymaneh Date: Mon, 23 Nov 2026 19:13:28 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20network:=20skip=20netw?= =?UTF-8?q?orkd=20configuration=20for=20guests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group_vars/guest/main.yml | 2 +- roles/network/defaults/main.yml | 3 ++ roles/network/meta/argument_specs.yml | 33 +++++++++++++++++++++ roles/network/tasks/main.yml | 1 + roles/network/tasks/networkd.yml | 1 - roles/network/templates/ethernet.network.j2 | 2 -- 6 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 roles/network/meta/argument_specs.yml diff --git a/group_vars/guest/main.yml b/group_vars/guest/main.yml index 3204de2c..86151810 100644 --- a/group_vars/guest/main.yml +++ b/group_vars/guest/main.yml @@ -1,3 +1,3 @@ --- -network_vlan_id: 0 +network_host_type: "guest" diff --git a/roles/network/defaults/main.yml b/roles/network/defaults/main.yml index faf0acdf..5e4718db 100644 --- a/roles/network/defaults/main.yml +++ b/roles/network/defaults/main.yml @@ -1,5 +1,8 @@ --- +# Type of host. 'guest' will skip configuration of physical interface and vlan +network_host_type: physical + # Set VLAN ID to configure VLAN for hosts # For hosts without physical network device (i.e. guest vms), set value to 0 to disable configuration network_vlan_id: 4000 diff --git a/roles/network/meta/argument_specs.yml b/roles/network/meta/argument_specs.yml new file mode 100644 index 00000000..47cfbc80 --- /dev/null +++ b/roles/network/meta/argument_specs.yml @@ -0,0 +1,33 @@ +--- + +argument_specs: + main: + options: + network_host_type: + type: "str" + required: true + choices: + - "physical" + - "guest" + description: "Wether host is a physical machine with phyiscal interfaces or a virtualized guest" + + network_vlan_id: + type: "int" + required: true + description: "VLAN identifier, only applicable for physical hosts." + + network_bridge_vlan: + type: "str" + required: true + description: + - "Name of the bridge interface to attach to virtual vlan interface, i.e. 'brX'." + - "Leave empty to skip bridge creation" + - "Only applicable to physical hosts" + + network_bridge_physical: + type: "str" + required: true + description: + - "Name of the bridge interface to attach to virtual phyiscal interface, i.e. 'brX'." + - "Leave empty to skip bridge creation" + - "Only applicable to physical hosts" diff --git a/roles/network/tasks/main.yml b/roles/network/tasks/main.yml index 055bc6d8..dc0be09e 100644 --- a/roles/network/tasks/main.yml +++ b/roles/network/tasks/main.yml @@ -4,6 +4,7 @@ ansible.builtin.import_tasks: networkd.yml tags: - network_networkd + when: network_host_type == 'physical' - name: Import resolved tasks ansible.builtin.import_tasks: resolved.yml diff --git a/roles/network/tasks/networkd.yml b/roles/network/tasks/networkd.yml index f4dcf62c..890e3e93 100644 --- a/roles/network/tasks/networkd.yml +++ b/roles/network/tasks/networkd.yml @@ -23,7 +23,6 @@ notify: restart systemd-networkd - name: Ensure netdev configuration for vlan exist - when: network_vlan_id != 0 notify: restart systemd-networkd block: - name: Ensure netdev configuration for vlan exist diff --git a/roles/network/templates/ethernet.network.j2 b/roles/network/templates/ethernet.network.j2 index 6af23ee4..393d55fb 100644 --- a/roles/network/templates/ethernet.network.j2 +++ b/roles/network/templates/ethernet.network.j2 @@ -2,9 +2,7 @@ Name={{ network_physical_interface }} [Network] -{% if network_vlan_id != 0 %} VLAN={{ network_physical_interface }}.{{ network_vlan_id }} -{% endif %} {# If we configure a bridge for this interface, the IP address and gateway are defined in 'ethernet_bridge_network.j2' #} {% if network_bridge_physical is defined and network_bridge_physical | length > 0 %} Bridge={{ network_bridge_physical }} From dad7caa0ce690394b2ce59430f098abbdd525606 Mon Sep 17 00:00:00 2001 From: Peymaneh Date: Tue, 24 Nov 2026 17:40:12 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=A8=20virtualization:=20add=20edge=20?= =?UTF-8?q?vm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- host_vars/edge1.okfn.de/main.yml | 15 +++++++++++++++ host_vars/roy.okfn.de/main.yml | 6 ++++++ inventory | 1 + 3 files changed, 22 insertions(+) create mode 100644 host_vars/edge1.okfn.de/main.yml diff --git a/host_vars/edge1.okfn.de/main.yml b/host_vars/edge1.okfn.de/main.yml new file mode 100644 index 00000000..f687e463 --- /dev/null +++ b/host_vars/edge1.okfn.de/main.yml @@ -0,0 +1,15 @@ +--- + +# Keep host_data.site_ipv4 for compatibility +host_data: + site_ipv4: "{{ network_networks.private.address4 | split('/') | first }}" + +network_networks: + private: + address4: 192.168.100.103/24 + public: + mac: 00:50:56:00:FD:9C + address4: 5.9.108.212/27 + gateway4: 5.9.108.193 + address6: "2a01:4f8:162:44c7::3/64" + gateway6: 'fe80::1' diff --git a/host_vars/roy.okfn.de/main.yml b/host_vars/roy.okfn.de/main.yml index 139b0b3f..b5ee12a5 100644 --- a/host_vars/roy.okfn.de/main.yml +++ b/host_vars/roy.okfn.de/main.yml @@ -15,3 +15,9 @@ host_data: sidecar: false prometheus_exporter: - node_exporter + +virt_guests: + - name: edge1.okfn.de + cpus: 2 + memory: 4096 + disk_size: 15 diff --git a/inventory b/inventory index fe26bed4..f0bef073 100644 --- a/inventory +++ b/inventory @@ -100,6 +100,7 @@ schoch.okfn.de roy.okfn.de [roy] +edge1.okfn.de [guest:children] roy