- hosts:
    - master[0]
  name: build master0 image for ascend-device-plugin
  tasks:
    - name: build master0 ascend-device-plugin image
      install_device_plugin:
        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-device-plugin
  tasks:
    - name: build ascend-device-plugin image
      install_device_plugin:
        resources_dir: "{{ resource_path }}"
        step: 'build'
        is_first_group: "{{ sub_group_idx | default(1) | int == 1 }}"

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

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

- hosts:
    - npu_node
  tasks:
    - name: get ascend-device-plugin label
      label_node:
        step: 'get_label'
        ansible_run_tags: "ascend-device-plugin"
        node_name: "{{ NODE_NAME }}"
        master_node: "{{ inventory_hostname in groups['master'] }}"
        worker_node: "{{ inventory_hostname in groups['worker'] }}"

- hosts:
    - worker
  name: install or upgrade ascend-device-plugin
  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 }}/device-plugin/*"
        dest: "{{ resource_path }}/mindxdl/dlImages/{{ ansible_architecture }}/device-plugin/"
      delegate_to: localhost

    - name: install device-plugin
      install_device_plugin:
        resources_dir: "{{ resource_path }}"
        node_name: "{{ NODE_NAME }}"
        container_runtime_type: "{{ hostvars[groups['master'][0]].container_runtime_type }}"
        step: 'install'

    - name: apply device-plugin
      run_once: yes
      install_device_plugin:
        resources_dir: "{{ resource_path }}"
        step: 'apply'
        labels: "{{ groups.get('npu_node', []) | default([groups['worker'][0]], true) 
                | map('extract', hostvars, 'node_label') | map('default', {}) | list | combine(recursive=True)}}"
      delegate_to: "{{ groups['master'][0] }}"
      when: sub_group_idx | default(1) | int == 1