---
- name: initialize function
  include_tasks: includes/init.yaml

- name: validate ios_config_includes is defined
  fail:
    msg: "missing required arg: ios_config_includes"
  when: ios_config_includes is undefined

- name: set ios checkpoint filename
  set_fact:
    ios_checkpoint_filename: "chk_ansible"

# initiate creating a checkpoint of the existing running-config
- name: create checkpoint of current configuration
  include_tasks: "{{ role_path }}/includes/checkpoint/create.yaml"

- name: configure the target device
  block:
    # iterate over the set of includes to configure the device
    - name: iterate over configuration tasks
      include_tasks: "{{ task }}"
      loop: "{{ ios_config_includes }}"
      loop_control:
        loop_var: task

  rescue:
    # since the host has failed during the configuration load, the role by
    # default will initiate a restore sequence.  the restore sequence will
    # load the previous running-config with the replace option enabled.
    - name: display message
      debug:
        msg: "error configuring device, starting rollback"
      when: ios_config_rollback_enabled

    - name: initiate configuration rollback
      include_tasks: "{{ role_path }}/includes/checkpoint/restore.yaml"

    - name: display message
      debug:
        msg: "successfully completed configuration rollback"
      when: ios_config_rollback_enabled

    - name: fail host due to config load error
      fail:
        msg: "error loading configuration onto target device"

- name: set the ios_active_config fact
  set_fact:
    ios_active_config: "cfg_ansible"

# check if any reminents are left over from a previous run and remove them
# prior to starting the configuration tasks.
- name: check if stale temporarary files exist on target device
  cli:
    command: dir
  register: ios_dir_listing

- name: remove temporary files from target device
  cli:
    command: "delete /force flash:/{{ ios_active_config }}"
  when: ios_active_config in ios_dir_listing.stdout

# copy the updated running-config to the local flash device to be used to
# generate a configuration diff between the before and after
# running-configurations.
- name: copy running-config to active config
  ios_command:
    commands:
      - command: "copy running-config flash:{{ ios_active_config }}"
        prompt: ["\\? "]
        answer: "{{ ios_active_config }}"

# generate the configuration diff and display the diff to stdout.  only set
# changed if there are lines in the diff that have changed
- name: generate ios diff
  cli:
    command: "show archive config differences flash:{{ ios_checkpoint_filename }} flash:{{ ios_active_config }}"
  register: ios_config_diff
  changed_when: "'No changes were found' not in ios_config_diff.stdout"

- name: display config diff
  debug:
    msg: "{{ ios_config_diff.stdout.splitlines() }}"
  when: not ansible_check_mode

# refresh the list of files currently on the target network device flash
# drive and remote all temp files
- name: update local directory listing
  cli:
    command: dir
  register: ios_dir_listing

- name: remove remote temp files from flash
  cli:
    command: "delete /force flash:/{{ filename }}"
  loop:
    - "{{ ios_active_config }}"
    - "{{ ios_checkpoint_filename }}"
  loop_control:
    loop_var: filename
  when: filename in ios_dir_listing.stdout
