Skip to content

Commit e393f38

Browse files
author
jovial
authored
Add support for setting the libvirt connection URI (#16)
Add support for setting the libvirt connection URI. This allows you to use connection URIs that differ from the default of: `qemu:///system'.
1 parent a2287d2 commit e393f38

File tree

9 files changed

+32
-22
lines changed

9 files changed

+32
-22
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ Role Variables
2727

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

30+
- `libvirt_vm_uri`: Override the libvirt connection URI. See the
31+
[libvirt docs](https://libvirt.org/remote.html) docs for more details.
32+
33+
- `libvirt_vm_virsh_default_env`: Variables contained within this dictionary are
34+
added to the environment used when executing virsh commands.
35+
3036
- `libvirt_vms`: list of VMs to be created/destroyed. Each one may have the
3137
following attributes:
3238

defaults/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ libvirt_vms:
6161
# Path to console log file.
6262
console_log_path: "{{ libvirt_vm_console_log_path }}"
6363

64+
# Variables to add to the enviroment that is used to execute virsh commands
65+
libvirt_vm_virsh_default_env: "{{ { 'LIBVIRT_DEFAULT_URI': libvirt_vm_uri } if libvirt_vm_uri else {} }}"
66+
67+
# Override for the libvirt connection uri. Leave unset to use the default.
68+
libvirt_vm_uri: ""
6469

6570
### DEPRECATED ###
6671
# Use the above settings for each item within `libvirt_vms`, instead of the

tasks/destroy-vm.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
virt:
66
name: "{{ vm.name }}"
77
command: list_vms
8+
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
89
register: result
910
become: yes
1011

@@ -13,11 +14,13 @@
1314
virt:
1415
name: "{{ vm.name }}"
1516
state: destroyed
17+
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
1618
become: yes
1719

1820
- name: Ensure the VM is undefined
1921
virt:
2022
name: "{{ vm.name }}"
2123
command: undefine
24+
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
2225
become: yes
2326
when: vm.name in result.list_vms

tasks/destroy-volumes.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
{{ item.pool }}
77
with_items: "{{ volumes }}"
88
register: volume_result
9+
environment: "{{ libvirt_vm_script_env }}"
910
changed_when:
1011
- volume_result is success
1112
- (volume_result.stdout | from_json).changed | default(True)

tasks/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
---
2+
- name: Gather os specific variables
3+
include_vars: "{{ item }}"
4+
with_first_found:
5+
- files:
6+
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version}}.yml"
7+
- "{{ ansible_distribution }}.yml"
8+
- "{{ ansible_os_family }}.yml"
9+
tags: vars
10+
211
- include_tasks: autodetect.yml
312
# We don't need to know the engine and emulator if we're not creating any new
413
# VMs.

tasks/vm.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
---
2-
- name: Gather os specific variables
3-
include_vars: "{{ item }}"
4-
with_first_found:
5-
- files:
6-
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version}}.yml"
7-
- "{{ ansible_distribution }}.yml"
8-
- "{{ ansible_os_family }}.yml"
9-
skip: true
10-
tags: vars
11-
122
- name: Ensure the VM console log directory exists
133
file:
144
path: "{{ console_log_path | dirname }}"
@@ -31,11 +21,13 @@
3121
name: "{{ vm.name }}"
3222
command: define
3323
xml: "{{ lookup('template', 'vm.xml.j2') }}"
24+
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
3425
become: true
3526

3627
- name: Ensure the VM is running and started at boot
3728
virt:
3829
name: "{{ vm.name }}"
3930
autostart: "{{ autostart | bool }}"
4031
state: "{{ 'running' if (start | bool) else 'shutdown' }}"
32+
uri: "{{ libvirt_vm_uri | default(omit, true) }}"
4133
become: true

tasks/volumes.yml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
---
2-
- name: Gather os specific variables
3-
include_vars: "{{ item }}"
4-
with_first_found:
5-
- files:
6-
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version}}.yml"
7-
- "{{ ansible_distribution }}.yml"
8-
- "{{ ansible_os_family }}.yml"
9-
tags: vars
10-
112
- name: Ensure remote images are downloaded
123
get_url:
134
url: "{{ item }}"
@@ -33,7 +24,7 @@
3324
{{ libvirt_vm_image_cache_path }}/{{ item.image | basename }}
3425
{% endif %}
3526
with_items: "{{ volumes }}"
36-
environment: "{{ libvirt_vm_volume_creation_env }}"
27+
environment: "{{ libvirt_vm_script_env }}"
3728
register: volume_result
3829
changed_when:
3930
- volume_result is success

vars/Debian.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
libvirt_vm_log_owner: libvirt-qemu
55

66
# The environment passed to virt_volume.sh
7-
libvirt_vm_volume_creation_env: {}
7+
libvirt_vm_script_env: "{{ libvirt_vm_virsh_default_env }}"

vars/RedHat.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
libvirt_vm_log_owner: qemu
55

66
# The environment passed to virt_volume.sh
7-
libvirt_vm_volume_creation_env:
7+
libvirt_vm_script_env_redhat:
88
VOLUME_GROUP: qemu
99
VOLUME_OWNER: qemu
10+
11+
libvirt_vm_script_env: >-
12+
{{ libvirt_vm_script_env_redhat | combine(libvirt_vm_virsh_default_env) }}

0 commit comments

Comments
 (0)