---

- name: Flannel | Stop if kernel version is too low for Flannel Wireguard encryption
  assert:
    that: ansible_kernel.split('-')[0] is version('5.6.0', '>=')
  when:
    - kube_network_plugin == 'flannel'
    - flannel_backend_type == 'wireguard'
    - not ignore_assert_errors

- name: Flannel | Create Flannel manifests
  template:
    src: "{{ item.file }}.j2"
    dest: "{{ kube_config_dir }}/{{ item.file }}"
    mode: "0644"
  with_items:
    - {name: flannel, file: cni-flannel-rbac.yml, type: sa}
    - {name: kube-flannel, file: cni-flannel.yml, type: ds}
  register: flannel_node_manifests
  when:
    - inventory_hostname == groups['kube_control_plane'][0]

- name: Flannel | Start Resources
  kube:
    name: "{{ item.item.name }}"
    namespace: "kube-system"
    kubectl: "{{ bin_dir }}/kubectl"
    resource: "{{ item.item.type }}"
    filename: "{{ kube_config_dir }}/{{ item.item.file }}"
    state: "latest"
  with_items: "{{ flannel_node_manifests.results }}"
  when: inventory_hostname == groups['kube_control_plane'][0] and not item is skipped

- name: Flannel | Wait for flannel subnet.env file presence
  wait_for:
    path: /run/flannel/subnet.env
    delay: 5
    timeout: 600
