Skip to content

Commit

Permalink
Add support for setting the libvirt connection URI (#16)
Browse files Browse the repository at this point in the history
Add support for setting the libvirt connection URI. This allows you to use connection URIs that differ from the default of: `qemu:///system'.
  • Loading branch information
jovial authored Nov 19, 2018
1 parent a2287d2 commit e393f38
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 22 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ Role Variables

- `libvirt_vm_arch`: CPU architecture, default is `x86_64`.

- `libvirt_vm_uri`: Override the libvirt connection URI. See the
[libvirt docs](https://libvirt.org/remote.html) docs for more details.

- `libvirt_vm_virsh_default_env`: Variables contained within this dictionary are
added to the environment used when executing virsh commands.

- `libvirt_vms`: list of VMs to be created/destroyed. Each one may have the
following attributes:

Expand Down
5 changes: 5 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ libvirt_vms:
# Path to console log file.
console_log_path: "{{ libvirt_vm_console_log_path }}"

# Variables to add to the enviroment that is used to execute virsh commands
libvirt_vm_virsh_default_env: "{{ { 'LIBVIRT_DEFAULT_URI': libvirt_vm_uri } if libvirt_vm_uri else {} }}"

# Override for the libvirt connection uri. Leave unset to use the default.
libvirt_vm_uri: ""

### DEPRECATED ###
# Use the above settings for each item within `libvirt_vms`, instead of the
Expand Down
3 changes: 3 additions & 0 deletions tasks/destroy-vm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
virt:
name: "{{ vm.name }}"
command: list_vms
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
register: result
become: yes

Expand All @@ -13,11 +14,13 @@
virt:
name: "{{ vm.name }}"
state: destroyed
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
become: yes

- name: Ensure the VM is undefined
virt:
name: "{{ vm.name }}"
command: undefine
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
become: yes
when: vm.name in result.list_vms
1 change: 1 addition & 0 deletions tasks/destroy-volumes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{{ item.pool }}
with_items: "{{ volumes }}"
register: volume_result
environment: "{{ libvirt_vm_script_env }}"
changed_when:
- volume_result is success
- (volume_result.stdout | from_json).changed | default(True)
Expand Down
9 changes: 9 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
---
- name: Gather os specific variables
include_vars: "{{ item }}"
with_first_found:
- files:
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version}}.yml"
- "{{ ansible_distribution }}.yml"
- "{{ ansible_os_family }}.yml"
tags: vars

- include_tasks: autodetect.yml
# We don't need to know the engine and emulator if we're not creating any new
# VMs.
Expand Down
12 changes: 2 additions & 10 deletions tasks/vm.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
---
- name: Gather os specific variables
include_vars: "{{ item }}"
with_first_found:
- files:
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version}}.yml"
- "{{ ansible_distribution }}.yml"
- "{{ ansible_os_family }}.yml"
skip: true
tags: vars

- name: Ensure the VM console log directory exists
file:
path: "{{ console_log_path | dirname }}"
Expand All @@ -31,11 +21,13 @@
name: "{{ vm.name }}"
command: define
xml: "{{ lookup('template', 'vm.xml.j2') }}"
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
become: true

- name: Ensure the VM is running and started at boot
virt:
name: "{{ vm.name }}"
autostart: "{{ autostart | bool }}"
state: "{{ 'running' if (start | bool) else 'shutdown' }}"
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
become: true
11 changes: 1 addition & 10 deletions tasks/volumes.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
---
- name: Gather os specific variables
include_vars: "{{ item }}"
with_first_found:
- files:
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version}}.yml"
- "{{ ansible_distribution }}.yml"
- "{{ ansible_os_family }}.yml"
tags: vars

- name: Ensure remote images are downloaded
get_url:
url: "{{ item }}"
Expand All @@ -33,7 +24,7 @@
{{ libvirt_vm_image_cache_path }}/{{ item.image | basename }}
{% endif %}
with_items: "{{ volumes }}"
environment: "{{ libvirt_vm_volume_creation_env }}"
environment: "{{ libvirt_vm_script_env }}"
register: volume_result
changed_when:
- volume_result is success
Expand Down
2 changes: 1 addition & 1 deletion vars/Debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
libvirt_vm_log_owner: libvirt-qemu

# The environment passed to virt_volume.sh
libvirt_vm_volume_creation_env: {}
libvirt_vm_script_env: "{{ libvirt_vm_virsh_default_env }}"
5 changes: 4 additions & 1 deletion vars/RedHat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
libvirt_vm_log_owner: qemu

# The environment passed to virt_volume.sh
libvirt_vm_volume_creation_env:
libvirt_vm_script_env_redhat:
VOLUME_GROUP: qemu
VOLUME_OWNER: qemu

libvirt_vm_script_env: >-
{{ libvirt_vm_script_env_redhat | combine(libvirt_vm_virsh_default_env) }}

0 comments on commit e393f38

Please sign in to comment.