Skip to content

Instantly share code, notes, and snippets.

@awilki01
Last active May 13, 2020 16:09
Show Gist options
  • Save awilki01/54d459cadfdecd72a42a67393570e8fe to your computer and use it in GitHub Desktop.
Save awilki01/54d459cadfdecd72a42a67393570e8fe to your computer and use it in GitHub Desktop.
Cisco Switch IOS Upgrade Ansible Playbook
---
- name: Upgrade a Cisco IOS Device
hosts: ios_switch
tasks:
- name: Gathering Facts
ios_facts:
gather_subset: hardware
provider: "{{ creds_ssh }}"
tags: always
- name: Copying image to device flash
ntc_file_copy:
platform: cisco_ios_ssh
local_file: images/{{ image }}
host: "{{ inventory_hostname }}"
username: "{{ username }}"
password: "{{ password }}"
when: ansible_net_version != "{{ standard_version }}"
tags: copy
- name: Setting boot image
ios_config:
lines:
- no boot system
- boot system flash:{{ image }}
provider: "{{ creds_ssh }}"
host: "{{ inventory_hostname }}"
when: ansible_net_version != "{{ standard_version }}"
tags: set_boot
notify: save config
- name: Backup config
ntc_save_config:
platform: cisco_ios_ssh
host: "{{ inventory_hostname }}"
username: "{{ username }}"
password: "{{ password }}"
local_file: backup/{{ inventory_hostname }}.cfg
when: ansible_net_version != "{{ standard_version }}"
tags: backup
- name: Reloading the device
ntc_reboot:
platform: cisco_ios_ssh
confirm: true
timer: 2
host: "{{ inventory_hostname }}"
username: "{{ username }}"
password: "{{ password }}"
when: ansible_net_version != "{{ standard_version }}"
tags: reload
- name: Verifying connectivity
wait_for:
port: 22
host: "{{ inventory_hostname }}"
timeout: 600
- ios_command:
commands: ping 8.8.8.8
provider: "{{ creds_ssh }}"
wait_for:
- result[0] contains "!!!"
register: result
failed_when: "not '!!!' in result.stdout[0]"
tags: verify
handlers:
- name: save config
ios_config:
provider: "{{ creds_ssh }}"
save_when: always
@254In61
Copy link

254In61 commented Apr 27, 2020

Interesting stuff.
Gives me an idea or two on how to improve on my script too :).
Check it out here:

https://github.com/254In61/ciscoIosUpgrade

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment