Skip to content
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,35 @@ default value is `true`.

`libvirt_host_qemu_emulators`: List of architectures for which to install QEMU
system emulators, e.g. `x86`. The default value is `['x86']` if

`libvirt_host_require_vt` is `false`, otherwise the default value is an empty
list.

`libvirt_host_enable_efi_support`: Whether to enable EFI support. This defaults
to false as extra packages need to be installed.

`libvirt_host_var_prefix`: This determines The directory under /var/run that libvirt
uses to store state, e.g unix domain sockets, as well as the default name of the
PID file. Override this if you have a conflict with the default socket e.g it
could be in use by the nova_libvirt container. Defaults to `""`.

`libvirt_host_socket_dir`: Where the libvirtd socket is created. Defaults to
`/var/run/{{ libvirt_host_var_prefix }}` if `libvirt_host_var_prefix` is set,
otherwise `""`.

`libvirt_host_pid_path`: Path to PID file which prevents multiple instances of
the daemon from spawning. Defaults to `/var/run/{{ libvirt_host_var_prefix }}.pid`
if `libvirt_host_var_prefix` is set, otherwise `""`.

`libvirt_host_libvirtd_args`: Command line arguments passed to libvirtd by the
init system when libvirtd is started - quotes will be added

`libvirt_host_uri`: The libvirt connnection URI. Defaults to
`qemu+unix:///system?socket={{ libvirt_host_socket_dir }}/libvirt-sock` if
`libvirt_host_var_prefix` is set, otherwise `""`. If set to a falsey value,
an explicit connection URI will not be set when calling virsh or any of
the virt_ ansible modules.

Dependencies
------------

Expand Down
32 changes: 32 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,35 @@ libvirt_host_qemu_emulators: "{{ [] if libvirt_host_require_vt | bool else ['x86
# Whether or not to enable UEFI support. In some cases this requires installing
# extra packages.
libvirt_host_enable_efi_support: false

# This determines The directory under /var/run that libvirt uses to store state,
# e.g unix domain sockets, as well as the default name of the PID file. Override
# this if you have a conflict with the default socket e.g it could be in use by the
# nova_libvirt container
libvirt_host_var_prefix: ""

# Where the Unix Domain sockets are stored
libvirt_host_socket_dir: >-
{%- if libvirt_host_var_prefix -%}
/var/run/{{ libvirt_host_var_prefix }}
{%- endif -%}

# Path to PID file which prevents mulitple instances of the daemon from
# spawning
libvirt_host_pid_path: >-
{%- if libvirt_host_var_prefix -%}
/var/run/{{ libvirt_host_var_prefix }}.pid
{%- endif -%}

# Command line arguments passed to libvirtd by the init system when
# libvirtd is started - quotes will be added
libvirt_host_libvirtd_args: >-
{%- if libvirt_host_pid_path -%}
-p {{ libvirt_host_pid_path }}
{%- endif %}

# The libvirt connnection URI
libvirt_host_uri: >-
{%- if libvirt_host_socket_dir -%}
qemu+unix:///system?socket={{ libvirt_host_socket_dir }}/libvirt-sock
{%- endif %}
7 changes: 7 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---

- name: restart libvirt
service:
name: libvirtd
state: restarted
become: true
42 changes: 42 additions & 0 deletions tasks/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
# Configure services - runs after the install stage

- name: Set socket directory in libvirtd.conf
lineinfile:
path: /etc/libvirt/libvirtd.conf
insertafter: '^#unix_sock_dir ='
regexp: '^unix_sock_dir ='
line: unix_sock_dir = "{{ libvirt_host_socket_dir }}"
become: true
when: libvirt_host_socket_dir != ""
notify: restart libvirt

- name: Create directory for libvirt socket
file:
state: directory
path: "{{ libvirt_host_socket_dir }}"
owner: root
group: root
mode: 0755
become: true
when: libvirt_host_socket_dir != ""

- name: Process lineinfile rules
lineinfile: "{{ rule.args }}"
become: true
loop: "{{ libvirt_host_lineinfile_extra_rules | default([]) }}"
loop_control:
loop_var: rule
when: rule.condition
notify:
- restart libvirt

- name: Flush handlers
meta: flush_handlers

- name: Ensure the libvirt daemon is started and enabled
service:
name: libvirtd
state: started
enabled: yes
become: True
16 changes: 0 additions & 16 deletions tasks/install.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:
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version}}.yml"
- "{{ ansible_distribution }}.yml"
- "{{ ansible_os_family }}.yml"
tags: vars

- name: Install custom yum repositories
# Although argument splatting is marked as deprecated:
#
Expand Down Expand Up @@ -46,10 +37,3 @@
become: True
vars:
package: "qemu-system-{{ item }}"

- name: Ensure the libvirt daemon is started and enabled
service:
name: libvirtd
state: started
enabled: yes
become: True
11 changes: 11 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
---
- include: prelude.yml
- include: validate.yml
- include: install.yml
- name: Run post-install stage
include: "{{ post_install_path }}"
with_first_found:
- files:
- post-install-{{ ansible_distribution }}.yml
- post-install-{{ ansible_os_family }}.yml
skip: true
loop_control:
loop_var: post_install_path
- include: config.yml
- include: pools.yml
- include: networks.yml
3 changes: 3 additions & 0 deletions tasks/networks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
name: "{{ item.name }}"
command: define
xml: "{{ item.xml | default(lookup('template', 'network.xml.j2')) }}"
uri: "{{ libvirt_host_uri | default(omit, true) }}"
with_items: "{{ libvirt_host_networks }}"
become: True

- name: Ensure libvirt networks are active
virt_net:
name: "{{ item.name }}"
state: active
uri: "{{ libvirt_host_uri | default(omit, true) }}"
with_items: "{{ libvirt_host_networks }}"
become: True

- name: Ensure libvirt networks are started on boot
virt_net:
name: "{{ item.name }}"
autostart: yes
uri: "{{ libvirt_host_uri | default(omit, true) }}"
with_items: "{{ libvirt_host_networks }}"
become: True
3 changes: 3 additions & 0 deletions tasks/pools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
name: "{{ item.name }}"
command: define
xml: "{{ item.xml | default(lookup('template', 'pool.xml.j2')) }}"
uri: "{{ libvirt_host_uri | default(omit, true) }}"
with_items: "{{ libvirt_host_pools }}"
become: True

- name: Ensure libvirt storage pools are active
virt_pool:
name: "{{ item.name }}"
state: active
uri: "{{ libvirt_host_uri | default(omit, true) }}"
with_items: "{{ libvirt_host_pools }}"
become: True

- name: Ensure libvirt storage pools are started on boot
virt_pool:
name: "{{ item.name }}"
autostart: yes
uri: "{{ libvirt_host_uri | default(omit, true) }}"
with_items: "{{ libvirt_host_pools }}"
become: True
25 changes: 25 additions & 0 deletions tasks/post-install-Debian.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---

- name: Determine path to libvirt environment file
# On Debian >= 8 and Ubuntu >= 16.04 the libvirt-bin package has been

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have this condition elsewhere, so presumably we could extract it into a variable and set the path based on the variable. This works though.

# split into libvirt-daemon-system and libvirt-clients. They also seem
# to have changed to location to enviroment file. To prevent the need
# to hard code paths for every major version we determine these
# dynamically. This must be done after installing the package.
# You cannot guard the with_first_found with a condition without
# skip being set to true. This is undeseriable so we have to
# put it behind an include (a block doesn't work).
set_fact:
libvirt_host_lineinfile_extra_rules:
- args:
path: "{{ libvirt_env_path }}"
insertafter: '^#libvirtd_opts='
regexp: '^libvirtd_opts='
line: libvirtd_opts="{{ libvirt_host_libvirtd_args }}"
condition: "{{ libvirt_host_libvirtd_args != '' }}"
with_first_found:
- /etc/default/libvirt-bin
- /etc/default/libvirtd
loop_control:
loop_var: libvirt_env_path
tags: vars
10 changes: 10 additions & 0 deletions tasks/prelude.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
# This file is intended to be included at the beginning of a playbook.

- name: gather os specific variables
include_vars: "{{ item }}"
with_first_found:
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version}}.yml"
- "{{ ansible_distribution }}.yml"
- "{{ ansible_os_family }}.yml"
tags: vars
21 changes: 17 additions & 4 deletions vars/Debian.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
---
# List of libvirt package dependencies.
libvirt_host_libvirt_packages_default:
- libvirt-bin
# List of package dependencies common to all Debian distributions
libvirt_host_libvirt_packages_common:
- qemu-kvm
- python-libvirt
- python-lxml

# Package that contains the libvirt daemon
libvirt_host_libvirt_packages_libvirt_daemon: >-
{%- if (ansible_distribution == "Ubuntu" and
ansible_distribution_major_version is version_compare('16.04', '<')) or
(ansible_distribution == "Debian" and
ansible_distribution_major_version is version_compare('8', '<')) -%}
libvirt-bin
{%- else -%}
libvirt-daemon-system
{%- endif -%}

# Packages that are only necessary if you require EFI support
libvirt_host_packages_efi:
- ovmf

# List of all packages to install
libvirt_host_libvirt_packages: >
{{ libvirt_host_libvirt_packages_default +
{{ libvirt_host_libvirt_packages_common + [libvirt_host_libvirt_packages_libvirt_daemon] +
(libvirt_host_packages_efi if libvirt_host_enable_efi_support else []) | unique
}}

# These are passed to the lineinfile module to customize configuration files
libvirt_host_lineinfile_extra_rules: []
8 changes: 8 additions & 0 deletions vars/RedHat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ libvirt_host_custom_yum_repos_efi:

libvirt_host_custom_yum_repos: "{{ libvirt_host_custom_yum_repos_efi if libvirt_host_enable_efi_support else [] | unique }}"

# These are passed to the lineinfile module to customize configuration files
libvirt_host_lineinfile_extra_rules:
- args:
path: /etc/sysconfig/libvirtd
insertafter: '^#LIBVIRTD_ARGS='
regexp: '^LIBVIRTD_ARGS='
line: LIBVIRTD_ARGS="{{ libvirt_host_libvirtd_args }}"
condition: "{{ libvirt_host_libvirtd_args != '' }}"