/home/zuul/src/opendev.org/openstack/openstack-ansible-lxc_container_create/tasks/main.yml
---
# Copyright 2014, 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 lxc version
  command: "lxc-ls --version"
  changed_when: false
  register: lxc_version
  delegate_to: "{{ physical_host }}"
  tags:
    - skip_ansible_lint

- name: Enable or Disable lxc three syntax
  set_fact:
    lxc_three_syntax: "{{ (lxc_version.stdout.split('.')[0] | int) >= 3 }}"
    lxc_major_version: "{{ lxc_version.stdout.split('.')[0] }}"

- name: Allow the usage of local facts
  file:
    path: /etc/ansible/facts.d/
    state: directory
  delegate_to: "{{ physical_host }}"
  tags:
    - always

- name: Check the physical_host variable is set
  fail:
    msg: "The physical_host variable is not defined."
  when:
    - physical_host is undefined
  tags:
    - always

- name: Collect physical host facts if missing
  action:
    module: setup
  delegate_to: "{{ physical_host }}"
  delegate_facts: yes
  when:
    - hostvars[physical_host]['ansible_kernel'] is not defined

- name: Kernel version and LXC backing store check
  debug:
    msg: "Using overlayfs is not recommended when using Kernel version < 3.18"
  when:
    - lxc_container_backing_store == "overlayfs"
    - hostvars[physical_host]['ansible_kernel'] | version('3.18.0-0-generic', '<')
  tags:
    - always

- name: Gather variables for each operating system
  include_vars: "{{ item }}"
  with_first_found:
    - "{{ lxc_user_defined_container | default([]) }}"
    - "{{ 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

- name: Read custom facts from previous runs
  setup:
    filter: ansible_local
  delegate_to: "{{ physical_host }}"
  tags:
    - always

# NOTE(cloudnull): Check for the LXC volume group when creating LVM backed
#                  containers.
- block:
  - name: Check for lxc volume group
    shell: "(which vgs > /dev/null && vgs | grep -o '{{ lxc_container_vg_name }}') || false"
    register: vg_result
    failed_when: false
    changed_when: vg_result.rc != 0
    delegate_to: "{{ physical_host }}"
  - name: LXC VG check
    fail:
      msg: >-
        The "lxc_container_backing_store" option was set to "lvm" but no LXC
        volume group was found on the physical host. Please check your settings
        and host setup.
    when:
      - vg_result.rc != 0
  when:
    - lxc_container_backing_store == 'lvm'
  tags:
    - always

- include_tasks: lxc_container_destroy.yml
  when:
    - physical_host != inventory_hostname
    - lxc_container_recreate | bool

- include_tasks: lxc_container_create.yml
  when:
    - physical_host != inventory_hostname
  tags:
    - lxc-create

- include_tasks: lxc_container_config.yml
  when:
    - physical_host != inventory_hostname
  tags:
    - lxc-config

- name: Force all notified handlers now
  meta: flush_handlers

- name: (RE)Gather facts post setup
  setup:
    gather_subset: "network,hardware,virtual"