Skip to content

Commit

Permalink
Add option to pxe boot unprovisioned guests (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyphreak authored Aug 12, 2024
1 parent 19767af commit e489df5
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ where <node_object> follows the following json schema
},
"image": {
"type": "url"
"default": ""
},
"dhcp": {
"type": "boolean",
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
namespace: nephelaiio
name: libvirt
version: 0.0.15
version: 0.1.0
readme: README.md
authors:
- Ted Cook <[email protected]>
Expand Down
3 changes: 3 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ platforms:
image: "${MOLECULE_KVM_IMAGE}"
- name: libvirt-${MOLECULE_SCENARIO_NAME}-02
image: "${MOLECULE_KVM_IMAGE}"
- name: libvirt-${MOLECULE_SCENARIO_NAME}-03
groups:
- molecule_pxe
provisioner:
name: ansible
config_options:
Expand Down
2 changes: 1 addition & 1 deletion molecule/default/prepare.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Prepare
hosts: all
hosts: "all:!molecule_pxe"
become: true
gather_facts: true
tasks:
Expand Down
2 changes: 1 addition & 1 deletion molecule/default/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


- name: Verify KVM guests
hosts: all
hosts: "all:!molecule_pxe"
gather_facts: false
vars_files:
- ../../playbooks/vars.yml
Expand Down
36 changes: 25 additions & 11 deletions playbooks/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
recurse: false
register: _cache_query

- name: Cache platform images
- name: Cache plat form images
ansible.builtin.get_url:
url: "{{ item.url }}"
dest: "{{ item.path }}"
Expand All @@ -84,7 +84,8 @@
mode: 0600
vars:
_platform_image_cached: "{{ _cache_query.files | map(attribute='path') }}"
_platform_image_urls: "{{ _libvirt_platforms | map(attribute='image') | unique }}"
_platform_image_defined: "{{ _libvirt_platforms | selectattr('image', 'defined') }}"
_platform_image_urls: "{{ _platform_image_defined | map(attribute='image') | unique }}"
_platform_image_basenames: "{{ _platform_image_urls | map('urlsplit', 'path') | map('basename') }}"
_platform_image_paths: "{{ [_libvirt_path] | zip(_platform_image_basenames) | map('join', '/') }}"
_platform_image_tuples: "{{ (['url'] | zip(_platform_image_urls)) | zip(['path'] | zip(_platform_image_paths)) }}"
Expand Down Expand Up @@ -126,7 +127,7 @@

- name: Set platform facts
ansible.builtin.set_fact:
guest_image: "{{ _platform.image }}"
guest_image: "{{ _platform.image | default('') }}"
guest_dhcp: "{{ _platform.dhcp | default(False) | bool }}"
guest_disk_path: "{{ _libvirt_path }}/{{ _platform.name }}.img"
guest_disk_size: "{{ _platform.size | default('20G') }}"
Expand Down Expand Up @@ -159,21 +160,33 @@
when: inventory_hostname not in guest_query.stdout_lines
block:
- name: Create KVM guest disks
ansible.builtin.copy:
remote_src: true
src: "{{ _libvirt_path }}/{{ guest_image | urlsplit('path') | basename }}"
when: guest_image != ''
block:
- name: Create KVM guest disks
ansible.builtin.copy:
src: "{{ _libvirt_path }}/{{ guest_image | urlsplit('path') | basename }}"
dest: "{{ guest_disk_path }}"
remote_src: true

- name: Resize KVM guest disks
ansible.builtin.command:
cmd: qemu-img resize {{ guest_disk_path }} {{ guest_disk_size }}

- name: Create KVM pxe disks
ansible.builtin.command:
cmd: "qemu-img create -f qcow2 {{ guest_disk_path }} {{ guest_disk_size }}"
when: guest_image == ''

- name: Manage KVM disk permissions
ansible.builtin.file:
dest: "{{ guest_disk_path }}"
state: file
owner: "{{ (_host_os_family == 'Debian') | ternary('libvirt-qemu', 'qemu') }}"
group: kvm
mode: 0666
force: true
vars:
_host_os_family: "{{ hostvars['localhost']['ansible_os_family'] }}"

- name: Resize KVM guest disks
ansible.builtin.command:
cmd: qemu-img resize {{ guest_disk_path }} {{ guest_disk_size }}

- name: Create cloud-init tempdir
ansible.builtin.tempfile:
state: directory
Expand Down Expand Up @@ -286,3 +299,4 @@
- name: Wait for connection
ansible.builtin.wait_for_connection:
timeout: "{{ _libvirt_conn_timeout }}"
when: _libvirt_image != ''
6 changes: 6 additions & 0 deletions playbooks/facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
ansible.builtin.set_fact:
_libvirt_macaddr: "{{ _libvirt_mac_prefix | community.general.random_mac(seed=inventory_hostname) }}"

- name: Set image facts
ansible.builtin.set_fact:
_libvirt_image: "{{ _libvirt_instance_data.image | default('') }}"
vars:
_libvirt_instance_data: "{{ libvirt_platforms | selectattr('name', 'equalto', inventory_hostname) | first }}"

- name: Set platform facts
ansible.builtin.set_fact:
_libvirt_platforms: "{{ _libvirt_platform_data | zip(_macaddr_list) | map(_libvirt_filter_combine) }}"
Expand Down
1 change: 1 addition & 0 deletions playbooks/templates/vm.j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<os>
<type arch='x86_64' machine='pc-q35-6.2'>hvm</type>
<boot dev='hd'/>
<boot dev='network'/>
</os>
<features>
<acpi/>
Expand Down

0 comments on commit e489df5

Please sign in to comment.