openstack-ansible/playbooks/common-tasks/os-nspawn-container-setup.yml
Kevin Carter fd9cda8df9
Add nspawn container driver
This change adds an nspawn container driver which will enable deployers
to run clouds with systemd-nspawn instead of LXC. This adds "nspawn" to
as an option to the `container_tech` variable. To support this change,
The inventory generation tools have been updated to allow for a
new group named `nspawn_hosts`. All of the container connectivity and
setup are stored within the integrated repo under the new templates
directory.

The addition of "nspawn" container driver enables the ability for
deployers to change, or mix container technologies within a single
deployment without needing to change our well defined network
topology or storage layout.

Depends-On: I13d05ba8bcfe785257a9cf98dbdb6024ec937816
Change-Id: I41cfec63c423cd56a91c25dabae9aa1031c27e03
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
2018-02-11 19:02:24 -06:00

135 lines
3.8 KiB
YAML

---
# Copyright 2017, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Pull systemd version
command: "systemctl --version"
changed_when: false
register: systemd_version
delegate_to: "{{ physical_host }}"
tags:
- skip_ansible_lint
- always
- name: Set facts
set_fact:
nspawn_systemd_version: "{{ systemd_version.stdout_lines[0].split()[-1] }}"
tags:
- always
- name: Escape quote container name
command: "systemd-escape {{ inventory_hostname }}"
changed_when: false
register: systemd_escape
delegate_to: "{{ physical_host }}"
tags:
- skip_ansible_lint
- always
- name: Ensure mount directories exists (container)
file:
path: "{{ item['mount_path'] }}"
state: "directory"
with_items:
- "{{ list_of_bind_mounts | default([]) }}"
delegate_to: "{{ physical_host }}"
when:
- not is_metal | bool
tags:
- common-nspawn
- name: Ensure mount directories exists (physical host)
file:
path: "{{ item['bind_dir_path'] }}"
state: "directory"
with_items:
- "{{ list_of_bind_mounts | default([]) }}"
when:
- not is_metal | bool
tags:
- common-nspawn
- name: Create container bind mount config
lineinfile:
dest: "/etc/systemd/nspawn/{{ inventory_hostname }}.nspawn"
line: "Bind={{ item['mount_path'] }}:{{ item['bind_dir_path'] }}"
insertafter: "^Bind"
backup: "true"
with_items:
- "{{ list_of_bind_mounts | default([]) }}"
delegate_to: "{{ physical_host }}"
register: _ec
when:
- not is_metal | bool
- nspawn_systemd_version | int > 219
tags:
- common-nspawn
- name: Create container bind mount config (old)
block:
- name: Get ExecStart from config
shell: >-
grep -w '^ExecStart=/usr/bin/systemd-nspawn'
/etc/systemd/system/systemd-nspawn@$(/usr/bin/systemd-escape {{ inventory_hostname }}).service
delegate_to: "{{ physical_host }}"
register: _ec_old_start
changed_when: false
- name: set flag fact
set_fact:
nspawn_flags: "{{ _ec_old_start.stdout.split('ExecStart=/usr/bin/systemd-nspawn')[-1] }}"
nspawn_extra_flags: "{% for item in list_of_bind_mounts %} --bind={{ item['mount_path'] }}:{{ item['bind_dir_path'] }}{% endfor %}"
- name: set flag list
set_fact:
nspawn_flag_list: "{{ nspawn_flags.split() | union(nspawn_extra_flags.split()) | unique }}"
- name: Add line in container start config
lineinfile:
dest: "/etc/systemd/system/systemd-nspawn@{{ systemd_escape.stdout }}.service"
line: "ExecStart=/usr/bin/systemd-nspawn {{ nspawn_flag_list | join(' ') }}"
regexp: "^ExecStart"
backup: "true"
delegate_to: "{{ physical_host }}"
register: _ec
when:
- not is_metal | bool
- list_of_bind_mounts | default([])
- nspawn_systemd_version | int < 220
tags:
- common-nspawn
- name: Restart container
systemd:
name: "systemd-nspawn@{{ systemd_escape.stdout }}"
state: restarted
register: _container_restart
until: _container_restart | success
retries: 3
delay: 5
delegate_to: "{{ physical_host }}"
when:
- _ec | changed
tags:
- common-nspawn
- name: Wait for container connectivity
wait_for_connection:
delay: 3
timeout: 60
when:
- _container_restart | changed
tags:
- common-nspawn