- hosts:
    - master[0]
  name: build master0 image for ascend-operator
  tasks:
    - name: build master0 ascend-operator image
      install_ascend_operator:
        resources_dir: "{{ resource_path }}"
        step: 'build'
        is_first_group: "{{ sub_group_idx | default(1) | int == 1 }}"

    - name: set master0 arch
      set_fact:
        master0_arch: "{{ ansible_architecture }}"

- hosts:
    - other_build_image
  name: build image for ascend-operator
  tasks:
    - name: build ascend-operator image
      install_ascend_operator:
        resources_dir: "{{ resource_path }}"
        step: 'build'
        is_first_group: "{{ sub_group_idx | default(1) | int == 1 }}"

- hosts:
    - master[0]
  name: fetch master0 ascend-operator image
  tasks:
    - name: fetch master0 ascend-operator image
      scp:
        ip: "{{ inventory_hostname }}"
        port: "{{ansible_ssh_port|default('22')}}"
        remote_user: "{{ ansible_ssh_user }}"
        passwd: "{{ ansible_ssh_pass | default(omit) }}"
        src: "{{ ascend_operator_images }}"
        dest: "{{ resource_path }}/mindxdl/dlImages/{{ ansible_architecture }}/ascend-operator"
        fetch: 'true'
      delegate_to: localhost

- hosts:
    - other_build_image
  name: fetch ascend-operator image
  tasks:
    - name: fetch ascend-operator image
      scp:
        ip: "{{ inventory_hostname }}"
        port: "{{ansible_ssh_port|default('22')}}"
        remote_user: "{{ ansible_ssh_user }}"
        passwd: "{{ ansible_ssh_pass | default(omit) }}"
        src: "{{ ascend_operator_images }}"
        dest: "{{ resource_path }}/mindxdl/dlImages/{{ ansible_architecture }}/ascend-operator"
        fetch: 'true'
      delegate_to: localhost
      when: ansible_architecture != hostvars[groups['master'][0]].master0_arch

- hosts:
    - master
  name: install or upgrade ascend-operator
  tasks:
    - name: push images to remote
      scp:
        ip: "{{ inventory_hostname }}"
        port: "{{ansible_ssh_port|default('22')}}"
        remote_user: "{{ ansible_ssh_user }}"
        passwd: "{{ ansible_ssh_pass | default(omit) }}"
        src: "{{ resource_path }}/mindxdl/dlImages/{{ ansible_architecture }}/ascend-operator/*"
        dest: "{{ resource_path }}/mindxdl/dlImages/{{ ansible_architecture }}/ascend-operator/"
      delegate_to: localhost

    - name: install ascend-operator
      install_ascend_operator:
        resources_dir: "{{ resource_path }}"
        node_name: "{{ NODE_NAME }}"
        container_runtime_type: "{{ hostvars[groups['master'][0]].container_runtime_type }}"
        step: 'install'

    - name: apply ascend-operator
      run_once: yes
      install_ascend_operator:
        resources_dir: "{{ resource_path }}"
        step: 'apply'
      delegate_to: "{{ groups['master'][0] }}"
      when: sub_group_idx | default(1) | int == 1