---
- name: "Install OS image {{ si }}"
  check_mode: "{{ checkmode }}"
  nxos_install_os:
    system_image_file: "{{ si }}"
    issu: "{{ issu }}"
    provider: "{{ connection }}"
  register: result
  when: not force

- name: "Remove old boot pointers if any"
  nxos_config:
    lines:
      - no boot nxos
      - no boot kickstart
      - no boot system
    match: line
    provider: "{{ connection }}"
  ignore_errors: yes
  when: force

- name: "Set OS image {{ si }} boot pointers"
  nxos_config:
    lines:
      - "boot nxos bootflash:{{ si }}"
      - copy run start
    match: line
    provider: "{{ connection }}"
  when: force

- name: "Boot image {{ si }} using reload"
  nxos_config:
    lines:
      - 'terminal dont-ask'
      - 'reload'
    provider: "{{ connection }}"
  ignore_errors: yes
  when: force

- debug: msg=" {{ result['install_state'] }}"
  when: not force

- name: Wait for device to come back up with new image
  wait_for:
    port: 22
    state: started
    timeout: 500
    delay: 60
    host: "{{ inventory_hostname }}"
  when: result.changed and not checkmode

- debug: msg='Wait 5 mins to allow system to stabilize'
  when: result.changed and not checkmode
- pause:
    seconds: 300
  when: result.changed and not checkmode
