---
  - hosts: master:worker:ingress:storage
    any_errors_fatal: true
    name: "{{ play_name | default('Configure Cluster Network') }}"
    serial: "{{ serial_count | default('100%') }}"
    remote_user: root
    become_method: sudo
    vars_files:
      - group_vars/all.yaml

    pre_tasks:
      - name: download networking images
        command: docker pull {{ item }}
        with_items:
          - "{{ calico_node_img }}"
          - "{{ calico_cni_img }}"
          - "{{ calico_ctl_img }}"

      - name: check if calico-node is active
        command: systemctl is-active -q calico-node.service
        register: status
        failed_when: status.rc !=0 and status.rc != 3 # 0=running, 3=stopped or doesnt exist
        when: upgrading is defined and upgrading|bool == true

      # always stop and disable if service exists
      - name: stop calico-node service if running
        service:
          name: calico-node.service
          state: stopped
          enabled: no
        when: upgrading is defined and upgrading|bool == true and status is defined and status.rc == 0

      - name: remove unnecessary calico-node unit file
        file:
          path: "{{ init_system_dir }}/calico-node.service"
          state: absent
        when: upgrading is defined and upgrading|bool == true

    roles:
      - calico

---
  - hosts: master:worker:ingress:storage
    any_errors_fatal: true
    name: "{{ play_name | default('Configure Cluster Network') }}"
    serial: "{{ serial_count | default('100%') }}"
    remote_user: root
    become_method: sudo
    vars_files:
      - group_vars/all.yaml

    pre_tasks:
      - name: download networking images
        command: docker pull {{ item }}
        with_items:
          - "{{ calico_node_img }}"
          - "{{ calico_cni_img }}"
          - "{{ calico_ctl_img }}"

      - name: check if calico-node is active
        command: systemctl is-active -q calico-node.service
        register: status
        failed_when: status.rc !=0 and status.rc != 3 # 0=running, 3=stopped or doesnt exist
        when: upgrading is defined and upgrading|bool == true

      # always stop and disable if service exists
      - name: stop calico-node service if running
        service:
          name: calico-node.service
          state: stopped
          enabled: no
        when: upgrading is defined and upgrading|bool == true and status is defined and status.rc == 0

      - name: remove unnecessary calico-node unit file
        file:
          path: "{{ init_system_dir }}/calico-node.service"
          state: absent
        when: upgrading is defined and upgrading|bool == true

    roles:
      - calico

    post_tasks:
      - name: remove calico config job
        command: kubectl delete job configure-calico -n kube-system
        run_once: true
Remove calico config job after network came up successfully
Remove calico config job after network came up successfully
