---
- hosts: [HOSTS]
gather_facts: yes
connection: local
vars:
compliant_ios_version: 15.2(4)E8
new_ios_bin: c3560cx-universalk9-mz.152-4.E8.bin
new_ios_md5: bf2f1960b1fe0a05bdc69e17d82fcfed
should_reboot: YES
vars_prompt:
- name: username
prompt: "Enter username: "
private: no
- name: password
prompt: "Enter password: "
private: yes
- name: auth_pass
prompt: "Enter enable secret: "
private: yes
tasks:
- name: gathering HOSTS facts
ios_facts:
username: "{{ username }}"
password: "{{ password }}"
authorize: yes
auth_pass: "{{ auth_pass }}"
timeout: 30
register: all_facts
- name: Verifying if file exists in flash
ios_command:
commands:
- command: dir | include "{{ new_ios_bin }}"
username: "{{ username }}"
password: "{{ password }}"
authorize: yes
auth_pass: "{{ auth_pass }}"
timeout: 30
register: new_ios_on_flash
- name: Debugging new_ios_on_flash
debug:
msg: new ios is flash is "{{ new_ios_on_flash }}"
- name: Starting IOS upgrade process
block:
- name: Upgrade IOS images if not compliant
block:
- name: Download new IOS image if it is not already on flash
ios_command:
commands:
prompt: 'c3560cx-universalk9-mz.152-4.E8.bin'
answer: "\r"
username: "{{ username }}"
password: "{{ password }}"
authorize: yes
auth_pass: "{{ auth_pass }}"
timeout: 600
when: new_ios_on_flash.stdout[0] == ""
- name: Checking MD5 hash
ios_command:
commands:
- command: "verify /md5 flash:/{{ new_ios_bin }}"
username: "{{ username }}"
password: "{{ password }}"
authorize: yes
auth_pass: "{{ auth_pass }}"
timeout: 3000
register: md5_result
- name: Set boot var if MD5 matches
block:
- name: Changing boot var
ios_config:
lines:
- default boot system
- boot system flash:/"{{ new_ios_bin }}"
username: "{{ username }}"
password: "{{ password }}"
authorize: yes
auth_pass: "{{ auth_pass }}"
save_when: always
timeout: 150
register: new_ios_on_flash
- name: Setting boot var
ios_command:
commands:
- command: "reload at 23:00\ny"
username: "{{ username }}"
password: "{{ password }}"
authorize: yes
auth_pass: "{{ auth_pass }}"
timeout: 30
when: md5_result.stdout[0][-32:] == new_ios_md5
when: ansible_net_version != compliant_ios_version