diff --git a/README.md b/README.md index e8c577c..2126b26 100644 --- a/README.md +++ b/README.md @@ -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 ------------ diff --git a/defaults/main.yml b/defaults/main.yml index d791c3d..eca224f 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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 %} diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..8193fde --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,7 @@ +--- + +- name: restart libvirt + service: + name: libvirtd + state: restarted + become: true diff --git a/tasks/config.yml b/tasks/config.yml new file mode 100644 index 0000000..a342357 --- /dev/null +++ b/tasks/config.yml @@ -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 diff --git a/tasks/install.yml b/tasks/install.yml index 99864e3..32dde1f 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -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: # @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml index 8bc2268..e4b1a6e 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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 diff --git a/tasks/networks.yml b/tasks/networks.yml index aa4c7fa..3c74095 100644 --- a/tasks/networks.yml +++ b/tasks/networks.yml @@ -4,6 +4,7 @@ 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 @@ -11,6 +12,7 @@ virt_net: name: "{{ item.name }}" state: active + uri: "{{ libvirt_host_uri | default(omit, true) }}" with_items: "{{ libvirt_host_networks }}" become: True @@ -18,5 +20,6 @@ virt_net: name: "{{ item.name }}" autostart: yes + uri: "{{ libvirt_host_uri | default(omit, true) }}" with_items: "{{ libvirt_host_networks }}" become: True diff --git a/tasks/pools.yml b/tasks/pools.yml index 95b7cc9..ab5e064 100644 --- a/tasks/pools.yml +++ b/tasks/pools.yml @@ -15,6 +15,7 @@ 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 @@ -22,6 +23,7 @@ virt_pool: name: "{{ item.name }}" state: active + uri: "{{ libvirt_host_uri | default(omit, true) }}" with_items: "{{ libvirt_host_pools }}" become: True @@ -29,5 +31,6 @@ virt_pool: name: "{{ item.name }}" autostart: yes + uri: "{{ libvirt_host_uri | default(omit, true) }}" with_items: "{{ libvirt_host_pools }}" become: True diff --git a/tasks/post-install-Debian.yml b/tasks/post-install-Debian.yml new file mode 100644 index 0000000..d7d16b2 --- /dev/null +++ b/tasks/post-install-Debian.yml @@ -0,0 +1,25 @@ +--- + +- name: Determine path to libvirt environment file + # On Debian >= 8 and Ubuntu >= 16.04 the libvirt-bin package has been + # 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 diff --git a/tasks/prelude.yml b/tasks/prelude.yml new file mode 100644 index 0000000..e4939e4 --- /dev/null +++ b/tasks/prelude.yml @@ -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 diff --git a/vars/Debian.yml b/vars/Debian.yml index 733ce30..7353d0b 100644 --- a/vars/Debian.yml +++ b/vars/Debian.yml @@ -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: [] diff --git a/vars/RedHat.yml b/vars/RedHat.yml index 38b8a19..b522335 100644 --- a/vars/RedHat.yml +++ b/vars/RedHat.yml @@ -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 != '' }}"