Skip to content

Yamllint + ansible-lint #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ libvirt_vm_default_console_log_dir: "/var/log/libvirt-consoles"

# Whether UUID should be calculated by hashing the VM name. If not, the UUID is
# randomly generated by libvirt when the VM is defined.
libvirt_vm_default_uuid_deterministic: False
libvirt_vm_default_uuid_deterministic: false

# The default location for libvirt images
libvirt_volume_default_images_path: '/var/lib/libvirt/images'
Expand Down Expand Up @@ -42,7 +42,7 @@ libvirt_vm_emulator:
# Default value for clock syncing. The default (false) uses <clock sync="localtime">
# to configure the instances clock synchronisation. Change to a timezone to make
# configuration use <clock offset="specified offset">
libvirt_vm_clock_offset: False
libvirt_vm_clock_offset: false

# Default value for whether to trust guest receive filters. This gets mapped to
# the trustGuestRxFilters attribute of VM interfaces.
Expand All @@ -53,7 +53,7 @@ libvirt_vm_trust_guest_rx_filters: false
# the values of the deprecated variables below.
# See README.md or tasks/main.yml for these attributes' defaults.
libvirt_vms:
# State of the VM. May be 'present' or 'absent'.
# State of the VM. May be 'present' or 'absent'.
- state: "{{ libvirt_vm_state }}"

# Name of the VM.
Expand Down Expand Up @@ -85,7 +85,7 @@ libvirt_vms:

# XML template file to source domain definition
xml_file: vm.xml.j2

# May be one of: bios, or efi.
boot_firmware: bios

Expand Down Expand Up @@ -113,6 +113,6 @@ libvirt_vm_volumes:
libvirt_vm_interfaces:
libvirt_vm_console_log_path:
# Required, so we should fail if not set.
#libvirt_vm_name:
#libvirt_vm_memory_mb:
#libvirt_vm_vcpus:
# libvirt_vm_name:
# libvirt_vm_memory_mb:
# libvirt_vm_vcpus:
2 changes: 1 addition & 1 deletion meta/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
galaxy_info:
#role_name: libvirt_vm
# role_name: libvirt_vm
author: Mark Goddard
description: >
Role to configure and create VMs on a Libvirt/KVM hypervisor
Expand Down
27 changes: 15 additions & 12 deletions tasks/autodetect.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
failed_when: false

- name: Check for the KVM device
stat:
ansible.builtin.stat:
path: /dev/kvm
register: stat_kvm

- name: Set a fact containing the virtualisation engine
set_fact:
ansible.builtin.set_fact:
libvirt_vm_engine: >-
{%- if ansible_facts.architecture != libvirt_vm_arch -%}
{# Virtualisation instructions are generally available only for the host
Expand All @@ -30,9 +30,10 @@

- name: Detect the virtualisation emulator
block:
- block:
- name: Set fact when vm engine is kvm
block:
- name: Detect the KVM emulator binary path
stat:
ansible.builtin.stat:
path: "{{ item }}"
register: kvm_emulator_result
with_items:
Expand All @@ -41,43 +42,45 @@
- /usr/libexec/qemu-kvm

- name: Set a fact containing the KVM emulator binary path
set_fact:
ansible.builtin.set_fact:
libvirt_vm_emulator: "{{ item.item }}"
with_items: "{{ kvm_emulator_result.results }}"
when: item.stat.exists
when: libvirt_vm_engine == 'kvm'

- block:
- name: Set a fact when RedHat and vm engine is qemu
block:
- name: Detect the QEMU emulator binary path
stat:
ansible.builtin.stat:
path: /usr/libexec/qemu-kvm
register: kvm_emulator_result

- name: Set a fact containing the QEMU emulator binary path
set_fact:
ansible.builtin.set_fact:
libvirt_vm_emulator: "{{ kvm_emulator_result.stat.path }}"
when: kvm_emulator_result.stat.exists
when:
- libvirt_vm_engine == 'qemu'
- ansible_facts.os_family == 'RedHat'
- ansible_facts.distribution_major_version | int >= 8

- block:
- name: Set a fact when not RedHat and vm engine is qemu
block:
- name: Detect the QEMU emulator binary path
shell: which qemu-system-{{ libvirt_vm_arch }}
ansible.builtin.shell: which qemu-system-{{ libvirt_vm_arch }}
register: qemu_emulator_result
changed_when: false

- name: Set a fact containing the QEMU emulator binary path
set_fact:
ansible.builtin.set_fact:
libvirt_vm_emulator: "{{ qemu_emulator_result.stdout }}"

when:
- libvirt_vm_engine == 'qemu'
- ansible_facts.os_family != 'RedHat' or ansible_facts.distribution_major_version | int == 7

- name: Fail if unable to detect the emulator
fail:
ansible.builtin.fail:
msg: Unable to detect emulator for engine {{ libvirt_vm_engine }}.
when: libvirt_vm_emulator is none
when: libvirt_vm_emulator is none or libvirt_vm_emulator | length == 0
4 changes: 2 additions & 2 deletions tasks/check-interface.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Check network interface has a network name
fail:
ansible.builtin.fail:
msg: >
The interface definition {{ interface }} has type 'network', but does not have
a network name defined.
Expand All @@ -10,7 +10,7 @@
- interface.network is not defined

- name: Check direct interface has an interface device name
fail:
ansible.builtin.fail:
msg: >
The interface definition {{ interface }} has type 'direct', but does not have
a host source device defined.
Expand Down
6 changes: 3 additions & 3 deletions tasks/check-usb-devices.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
- name: List USB hardware
command: lsusb -d {{ usb_device.vendor }}:{{ usb_device.product }}
ansible.builtin.command: lsusb -d {{ usb_device.vendor }}:{{ usb_device.product }}
register: host_attached_usb_device
become: true
changed_when: false
changed_when: false
failed_when: false

- name: Check USB device is present on Host system
fail:
ansible.builtin.fail:
msg: >
The USB Device with Vendor ID:{{ usb_device.vendor }} and Product ID:{{ usb_device.product }} is not seen on host system
Is the USB device plugged in correctly ?
Expand Down
15 changes: 8 additions & 7 deletions tasks/destroy-vm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,35 @@
# The destroyed state does not seem to be idempotent, so check whether the VM
# exists before destroying it.
- name: Check the VM's status
virt:
community.libvirt.virt:
name: "{{ vm.name }}"
command: list_vms
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
register: result
become: yes
become: true

- block:
- name: Destory the VM is existing
block:
- name: Ensure the VM is absent
virt:
community.libvirt.virt:
name: "{{ vm.name }}"
state: destroyed
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
become: yes
become: true

# note(wszumski): the virt module does not seem to support
# removing vms with nvram defined - as a workaround, use the
# virsh cli directly. It may be better to detect if dumpxml
# actually contains an nvram element rather than relying on
# boot_firmware having the correct value.
- name: Ensure the VM is undefined
command:
ansible.builtin.command:
cmd: >-
virsh
{% if libvirt_vm_uri %}-c {{ libvirt_vm_uri }}{% endif %}
undefine
{% if boot_firmware == 'efi' %} --nvram{% endif %}
{{ vm.name }}
become: yes
become: true
changed_when: true
when: vm.name in result.list_vms
4 changes: 2 additions & 2 deletions tasks/destroy-volumes.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Ensure the VM volumes do not exist
script: >
ansible.builtin.script: >
destroy_virt_volume.sh
{{ item.name }}
{{ item.pool | default('default') }}
Expand All @@ -11,4 +11,4 @@
changed_when:
- volume_result is success
- (volume_result.stdout | from_json).changed | default(True)
become: yes
become: true
6 changes: 3 additions & 3 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
- name: Gather os specific variables
include_vars: "{{ item }}"
ansible.builtin.include_vars: "{{ item }}"
with_first_found:
- files:
- "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml"
- "{{ ansible_facts.distribution }}.yml"
- "{{ ansible_facts.os_family }}.yml"
tags: vars

- include_tasks: autodetect.yml
- ansible.builtin.include_tasks: autodetect.yml
# We don't need to know the engine and emulator if we're not creating any new
# VMs.
when: >-
Expand All @@ -17,7 +17,7 @@

# Libvirt requires qemu-img to create qcow2 files.
- name: Ensure qemu-img is installed
package:
ansible.builtin.package:
name: "{{ 'qemu-img' if ansible_facts.os_family == 'RedHat' else 'qemu-utils' }}"
update_cache: "{{ True if ansible_facts.pkg_mgr == 'apt' else omit }}"
become: true
Expand Down
10 changes: 5 additions & 5 deletions tasks/vm.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: Ensure the VM console log directory exists
file:
ansible.builtin.file:
path: "{{ console_log_path | dirname }}"
state: directory
owner: "{{ libvirt_vm_log_owner }}"
Expand All @@ -11,26 +11,26 @@
become: "{{ libvirt_vm_sudo }}"

- name: Validate VM interfaces
include_tasks: check-interface.yml
ansible.builtin.include_tasks: check-interface.yml
vars:
interface: "{{ item }}"
with_items: "{{ interfaces }}"

- name: Validate Host USB Devices
include_tasks: check-usb-devices.yml
ansible.builtin.include_tasks: check-usb-devices.yml
vars:
usb_device: "{{ item }}"
with_items: "{{ usb_devices }}"

- name: Ensure the VM is defined
virt:
community.libvirt.virt:
command: define
xml: "{{ lookup('template', vm.xml_file | default('vm.xml.j2')) }}"
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
become: "{{ libvirt_vm_sudo }}"

- name: Ensure the VM is running and started at boot
virt:
community.libvirt.virt:
name: "{{ vm.name }}"
autostart: "{{ autostart | bool }}"
state: "{{ 'running' if (start | bool) else 'shutdown' }}"
Expand Down
12 changes: 6 additions & 6 deletions tasks/volumes.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
- name: Ensure remote images are downloaded
get_url:
ansible.builtin.get_url:
url: "{{ item.image }}"
dest: "{{ libvirt_vm_image_cache_path }}/{{ item.image | basename }}"
checksum: "{{ item.checksum | default(omit) }}"
with_items: "{{ volumes | selectattr('image', 'defined') | list }}"
when: "'http' in item.image"

- name: Ensure local images are copied
copy:
ansible.builtin.copy:
src: "{{ item.image }}"
dest: "{{ libvirt_vm_image_cache_path }}/{{ item.image | basename }}"
checksum: "{{ item.checksum | default(omit) }}"
Expand All @@ -17,7 +17,7 @@
when: "'http' not in item.image"

- name: Ensure the VM disk volumes exist
script: >
ansible.builtin.script: >
virt_volume.sh
-n {{ item.name }}
-p {{ item.pool |default('default') }}
Expand All @@ -36,11 +36,11 @@
changed_when:
- volume_result is success
- (volume_result.stdout | from_json).changed | default(True)
check_mode: False
check_mode: false
become: true

- name: Ensure the VM network volumes exist
command: qemu-img create -f {{ item.source.protocol }} {{ item.source.protocol }}:{{ item.source.name }} {{ item.capacity }}
ansible.builtin.command: qemu-img create -f {{ item.source.protocol }} {{ item.source.protocol }}:{{ item.source.name }} {{ item.capacity }}
with_items: "{{ volumes }}"
when: item.type | default(libvirt_volume_default_type) == 'network'
register: volume_result_network
Expand All @@ -49,5 +49,5 @@
changed_when:
- volume_result_network is success
- volume_result_network.rc == 1
check_mode: False
check_mode: false
become: true