/home/zuul/src/opendev.org/openstack/openstack-ansible-nspawn_hosts/tasks/main.yml
---
# 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
  tags:
    - skip_ansible_lint

- name: Set facts
  set_fact:
    nspawn_systemd_version: "{{ systemd_version.stdout_lines[0].split()[-1] }}"

- name: Gather variables for each operating system
  include_vars: "{{ item }}"
  with_first_found:
    - "{{ hostvars[physical_host]['ansible_distribution'] | lower }}-{{ hostvars[physical_host]['ansible_distribution_version'] | lower }}.yml"
    - "{{ hostvars[physical_host]['ansible_distribution'] | lower }}-{{ hostvars[physical_host]['ansible_distribution_major_version'] | lower }}.yml"
    - "{{ hostvars[physical_host]['ansible_os_family'] | lower }}-{{ hostvars[physical_host]['ansible_distribution_major_version'] | lower }}.yml"
    - "{{ hostvars[physical_host]['ansible_distribution'] | lower }}.yml"
    - "{{ hostvars[physical_host]['ansible_os_family'] | lower }}.yml"
  tags:
    - always

# NOTE(cloudnull): Because DNSMasq will be installed this task is required to
#                  ensure we're never starting that service in an unknown state.
- name: Prevent service from starting on install
  copy:
    content: |
      #!/bin/bash
      exit 101
    dest: "/usr/sbin/policy-rc.d"
    mode: "0755"
  when:
    - ansible_pkg_mgr == 'apt'
  notify:
    - Remove policy-rc

- name: Install distro packages
  package:
    pkg: "{{ nspawn_hosts_distro_packages }}"
    state: "{{ nspawn_hosts_package_state }}"
  register: install_packages
  until: install_packages is success
  retries: 5
  delay: 2
  notify:
    - Disable host dnsmasq service

- name: Move nspawn-attach into place
  copy:
    src: nspawn-attach.sh
    dest: /usr/local/bin/nspawn-attach
    mode: '0755'

- name: Create networkd and nspawn directories
  file:
    path: "{{ item }}"
    state: directory
  with_items:
    - /etc/systemd/journald.conf.d
    - /etc/systemd/network
    - /etc/systemd/nspawn
    - /etc/systemd/system/machines.target.wants
    - /var/log/journal

- name: Create journald directories
  file:
    path: "{{ item }}"
    state: directory
    owner: root
    group: systemd-journal
    mode: "02755"
  with_items:
    - /var/log/journal

- name: Create journald tempfiles
  template:
    src: "systemd-journald-nspawn_host.conf.j2"
    dest: "/etc/systemd/journald.conf.d/systemd-journald-nspawn_host.conf"
  notify:
    - Restart systemd-journald

- include_tasks: nspawn_cache_prestage.yml
  tags:
    - always

- include_tasks: nspawn_volume.yml

- include_tasks: nspawn_networking.yml

- include_tasks: nspawn_cache.yml

- name: (RE)Gather facts post setup
  setup: {}