---
- name: Set variables for this iteration of the loop.
  set_fact:
    manifest_directory: "{{ k8s_manifests_base_dir }}{{ outer_item.dir | default(outer_item) }}"
    manifest_lookup_type: "{{ outer_item.lookup_type | default('template') }}"
    manifest_namespace: "{{ outer_item.namespace | default('') }}"

- name: Print current manifest directory.
  debug: var=manifest_directory

- name: Ensure manifest-specific namespace exists.
  k8s:
    api_version: v1
    kind: Namespace
    name: "{{ manifest_namespace }}"
    state: present
    kubeconfig: "{{ k8s_kubeconfig }}"
  when: manifest_namespace | default(false)

- name: Include vars specific to this manifest.
  include_vars: "{{ item }}"
  with_first_found:
    - files:
        - "{{ manifest_directory }}/vars.yml"
      skip: true

- name: Deploy the resources defined inside the manifest.
  k8s:
    definition: "{{ item }}"
    kubeconfig: "{{ k8s_kubeconfig }}"
    state: "{{ k8s_manifests_state }}"
    force: "{{ k8s_force }}"
  loop: "{{ lookup(manifest_lookup_type, manifest_directory + '/manifest.yml') | from_yaml_all | list }}"
  register: k8s_result
  until: k8s_result is success
  retries: 10
  delay: 2
  no_log: "{{ k8s_no_log }}"
