Skip to content

Commit

Permalink
Refactor molecule kvm guest management playbooks
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyphreak committed Feb 27, 2024
1 parent 77592ab commit 5f28e0e
Show file tree
Hide file tree
Showing 26 changed files with 291 additions and 263 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ jobs:
release: jammy
- scenario: debian
release: bullseye
- scenario: debian
release: bookworm

steps:
- name: Check out the codebase.
Expand Down
4 changes: 4 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ fileignoreconfig:
ignore_detectors: [filecontent]
- filename: tasks/lvm.yml
ignore_detectors: [filecontent]
- filename: tasks/metadata/storage.yml
ignore_detectors: [filecontent]
- filename: molecule/ubuntu/molecule.yml
ignore_detectors: [filecontent]
- filename: molecule/debian/molecule.yml
ignore_detectors: [filecontent]
- filename: molecule/common/inventory.yml
ignore_detectors: [filecontent]
- filename: molecule/common/create.yml
ignore_detectors: [filecontent]
- filename: molecule/common/converge.yml
Expand Down
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,28 @@ UBUNTU_ISO=${UBUNTU_MIRROR}/${UBUNTU_BASENAME}
REQUIREMENTS = requirements.yml

ifeq (${MOLECULE_SCENARIO}, ubuntu)
MOLECULE_DISTRO=${UBUNTU_DISTRO}
MOLECULE_ISO=${UBUNTU_ISO}
else ifeq (${MOLECULE_SCENARIO}, debian)
MOLECULE_DISTRO=${DEBIAN_DISTRO}
MOLECULE_ISO=${DEBIAN_ISO}
endif

all: install version lint test

test: lint
MOLECULE_ISO=${MOLECULE_ISO} poetry run molecule $@ -s ${MOLECULE_SCENARIO}
MOLECULE_DISTRO=${MOLECULE_DISTRO} \
MOLECULE_ISO=${MOLECULE_ISO} \
poetry run molecule $@ -s ${MOLECULE_SCENARIO}

install:
@type poetry >/dev/null || pip3 install poetry
@sudo apt-get install -y libvirt-dev
@poetry install
@poetry install --no-root

lint: install
poetry run yamllint .
poetry run ansible-lint .
poetry run molecule syntax

roles:
[ -f ${REQUIREMENTS} ] && yq '.$@[] | .name' -r < ${REQUIREMENTS} \
Expand All @@ -49,7 +52,9 @@ collections:
requirements: roles collections

dependency create prepare converge idempotence side-effect verify destroy login reset:
MOLECULE_ISO=${MOLECULE_ISO} poetry run molecule $@ -s ${MOLECULE_SCENARIO}
MOLECULE_DISTRO=${MOLECULE_DISTRO} \
MOLECULE_ISO=${MOLECULE_ISO} \
poetry run molecule $@ -s ${MOLECULE_SCENARIO}

ignore:
poetry run ansible-lint --generate-ignore
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

An opinionated [ansible role](https://galaxy.ansible.com/nephelaiio/growfs) to grow filesystems for linux systems

## Dependencies

Role execution requires filters defined in [nephelaiio.plugins](https://galaxy.ansible.com/ui/repo/published/nephelaiio/plugins/) collection

## Use case

The main use case for the role is to automatically grow partitions/filesystems on systems installed using [nephelaiio.centos_installer](https://galaxy.ansible.com/nephelaiio/centos_installer) and [nephelaiio.ubuntu_installer](https://galaxy.ansible.com/nephelaiio/ubuntu_installer)

## Logic
Expand Down
3 changes: 1 addition & 2 deletions meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ galaxy_info:
- stretch
galaxy_tags: ['growfs']

dependencies:
- nephelaiio.plugins
dependencies: []
90 changes: 53 additions & 37 deletions molecule/common/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,117 +6,133 @@
- name: Spawn KVM guests
hosts: localhost
become: true
vars_files:
- vars.yml
pre_tasks:

- name: Install disk utils
ansible.builtin.apt:
name:
- parted
- lvm2

tasks:

- name: Query KVM guest info
community.libvirt.virt:
command: info
register: libvirt_info

- name: Filter uninitialized KVM guests
ansible.builtin.set_fact:
guest_info: "{{ _guest_info }}"
vars:
_guest_names: "{{ libvirt_info.keys() }}"
_target_names: "{{ guests | map(attribute='installer_hostname') | list }}"
_target_create: "{{ _target_names | difference(_guest_names) }}"
_guest_info: "{{ guests | selectattr('installer_hostname', 'in', _target_create) }}"

- name: Cache installer iso
ansible.builtin.get_url:
url: "{{ iso_installer.url }}"
dest: "{{ iso_installer.dest | urlsplit('path') }}"
url: "{{ molecule_installer_url }}"
dest: "{{ molecule_installer_dest | urlsplit('path') }}"

- name: Build ubuntu installer isos
ansible.builtin.include_role:
name: nephelaiio.ubuntu_installer
vars:
ubuntu_installer_interface_name: enp1s0
ubuntu_installer_target_dir: "{{ cache_dir }}"
ubuntu_installer_image_url: "{{ iso_installer.dest }}"
ubuntu_installer_target_dir: "{{ molecule_libvirt_path }}"
ubuntu_installer_image_url: "{{ molecule_installer_dest }}"
ubuntu_installer_hostname: "{{ guest.installer_hostname }}"
ubuntu_installer_username: molecule
ubuntu_installer_password: "{{ 'molecule' | password_hash('sha512') }}"
ubuntu_installer_sshkey:
- "{{ lookup('file', kvm_keypair + '.pub') }}"
- "{{ lookup('file', molecule_kvm_keypair + '.pub') }}"
ubuntu_installer_interface: "{{ guest.installer_interface }}"
ubuntu_installer_partman_method: "{{ guest.installer_partitioning_method }}"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ guest_info }}"
loop: "{{ molecule_kvm_guests }}"
when: molecule_scenario == "ubuntu"

- name: Build debian installer isos
ansible.builtin.include_role:
name: nephelaiio.debian_installer
vars:
debian_installer_interface_name: enp1s0
debian_installer_target_dir: "{{ cache_dir }}"
debian_installer_image_url: "{{ iso_installer.dest }}"
debian_installer_target_dir: "{{ molecule_libvirt_path }}"
debian_installer_image_url: "{{ debian_installer_dest }}"
debian_installer_hostname: "{{ guest.installer_hostname }}"
debian_installer_username: molecule
debian_installer_password: "{{ 'molecule' | password_hash('sha512') }}"
debian_installer_sshkey:
- "{{ lookup('file', kvm_keypair + '.pub') }}"
- "{{ lookup('file', molecule_kvm_keypair + '.pub') }}"
debian_installer_interface: "{{ guest.installer_interface }}"
debian_installer_partman_method: "{{ guest.installer_partitioning_method }}"
debian_installer_preseed_include: "{{ guest.installer_preseed_include | default('') }}"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ guest_info }}"
loop: "{{ molecule_kvm_guests }}"
when: molecule_scenario == "debian"

- name: Destroy KVM guest
community.libvirt.virt:
state: destroyed
name: "{{ guest.installer_hostname }}"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ molecule_kvm_guests }}"
failed_when: false

- name: Undefine KVM guest
community.libvirt.virt:
command: undefine
name: "{{ guest.installer_hostname }}"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ molecule_kvm_guests }}"
failed_when: false

- name: Destroy KVM guest disks
ansible.builtin.file:
path: "{{ guest_disk }}"
state: absent
vars:
guest_disk: "{{ molecule_libvirt_path }}/{{ guest.installer_hostname }}.img"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ molecule_kvm_guests }}"

- name: Create KVM guest disks
ansible.builtin.command: "qemu-img create {{ guest_disk }} {{ guest_disk_size }}"
args:
creates: "{{ guest_disk }}"
vars:
guest_disk: "{{ cache_dir }}/{{ guest.installer_hostname }}.img"
guest_disk: "{{ molecule_libvirt_path }}/{{ guest.installer_hostname }}.img"
guest_disk_size: "{{ guest.installer_disk_size }}"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ guest_info }}"
loop: "{{ molecule_kvm_guests }}"

- name: Manage permissions for KVM guest disks
ansible.builtin.file:
path: "{{ guest_disk }}"
owner: libvirt-qemu
group: kvm
mode: 0640
mode: 0666
vars:
guest_disk: "{{ cache_dir }}/{{ guest.installer_hostname }}.img"
guest_disk: "{{ molecule_libvirt_path }}/{{ guest.installer_hostname }}.img"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ guest_info }}"
loop: "{{ molecule_kvm_guests }}"

- name: Create KVM guest
community.libvirt.virt:
xml: "{{ xml_template }}"
command: define
vars:
guest_hostname: "{{ guest.installer_hostname }}"
guest_bridge: "{{ bridge_name }}"
guest_iso: "{{ cache_dir }}/{{ guest_hostname }}.iso"
guest_disk_path: "{{ cache_dir }}/{{ guest.installer_hostname }}.img"
guest_network: "{{ molecule_libvirt_network }}"
guest_iso: "{{ molecule_libvirt_path }}/{{ guest_hostname }}.iso"
guest_disk_path: "{{ molecule_libvirt_path }}/{{ guest.installer_hostname }}.img"
guest_mac: "{{ '52:54:00' | random_mac(seed=guest_hostname) }}"
guest_disk_format: raw
xml_template: "{{ lookup('template', 'vm.xml.j2') }}"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ guest_info }}"
loop: "{{ molecule_kvm_guests }}"

- name: Start KVM guest
community.libvirt.virt:
Expand All @@ -127,4 +143,4 @@
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ guest_info }}"
loop: "{{ molecule_kvm_guests }}"
52 changes: 29 additions & 23 deletions molecule/common/cleanup.yml → molecule/common/destroy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
hosts: localhost
become: true
tasks:

- name: Destroy KVM guest
community.libvirt.virt:
state: destroyed
name: "{{ guest.installer_hostname }}"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ guests }}"
loop: "{{ molecule_kvm_guests }}"
failed_when: false

- name: Undefine KVM guest
Expand All @@ -21,39 +20,46 @@
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ guests }}"
loop: "{{ molecule_kvm_guests }}"
failed_when: false

- name: Destroy KVM guest disks
ansible.builtin.file:
path: "{{ guest_disk }}"
state: absent
vars:
guest_disk: "{{ cache_dir }}/{{ guest.installer_hostname }}.img"
guest_disk: "{{ molecule_libvirt_path }}/{{ guest.installer_hostname }}.img"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ guests }}"
loop: "{{ molecule_kvm_guests }}"

- name: Destroy KVM extra disks
ansible.builtin.file:
path: "{{ guest_disk }}"
state: absent
vars:
guest_disk: "{{ cache_dir }}/{{ guest.installer_hostname }}.add.img"
loop_control:
loop_var: guest
label: "{{ guest.installer_hostname }}"
loop: "{{ guests }}"

- name: Destroy dummy bridge
ansible.builtin.command: "nmcli con del {{ bridge_name }}"
- name: Destroy libvirt network and storage
hosts: localhost
become: true
vars_files:
- vars.yml
tasks:
- name: Stop libvirt network
community.libvirt.virt_net:
command: destroy
name: "{{ _libvirt_network }}"
when: _libvirt_network != "default"
failed_when: false

- name: Destroy dummy interface
ansible.builtin.command: "ip link del {{ bridge_iface }}"
failed_when: false
- name: Delete libvirt network
community.libvirt.virt_net:
command: undefine
name: "{{ _libvirt_network }}"
when: _libvirt_network != "default"

- name: Destroy dummy bridge
ansible.builtin.command: "ip link del {{ bridge_name }} type bridge"
failed_when: false
- name: Undefine libvirt pool
community.libvirt.virt_pool:
state: undefined
name: "{{ _libvirt_pool }}"

- name: Destroy libvirt pool
ansible.builtin.file:
path: "{{ _libvirt_pool }}"
state: absent
9 changes: 4 additions & 5 deletions molecule/common/inventory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
ansible_user: molecule
ansible_host: "{{ guest_address }}"
ansible_port: "{{ guest_port }}"
ansible_ssh_private_key_file: "{{ kvm_keypair }}"
ansible_ssh_private_key_file: "{{ molecule_kvm_keypair }}"
partitioning_method: "{{ guest.installer_partitioning_method }}"
disk_location: "{{ cache_dir }}/{{ guest.installer_hostname }}.img"
disk_location: "{{ molecule_libvirt_path }}/{{ guest.installer_hostname }}.img"
disk_size: "{{ guest.installer_disk_size }}"
disk_index: "{{ disk_index }}"
vars:
guest_hostname: "{{ guest.installer_hostname }}"
bridge_address: "{{ guest.installer_interface.ipaddress }}"
guest_address: "{{ bridge_address }}"
guest_address: "{{ guest.installer_interface.ipaddress }}"
guest_port: 22
loop_control:
loop_var: guest
index_var: disk_index
label: "{{ guest.installer_hostname }}"
loop: "{{ guests }}"
loop: "{{ molecule_kvm_guests }}"
Loading

0 comments on commit 5f28e0e

Please sign in to comment.