cd/etc/ansible/roles/saveconfig/tasks/
vi main.yml
---
# tasks file
for saveconfig
# get the time for the current save file
- name: get time
local_action: command date +%Y%m%d%H%M%S
register: time
# get the current running config form the destination
device
- name: run 'show run'
ios_command:
provider: "{{provider}}"
authorize: yes
commands:
- show run
register: config
# if there is not an existing folder for the
destination device, create one
- name: ensure the directory exists
file:
path:
"{{backupdir}}/{{inventory_hostname}}"
recurse: yes
state: directory
# get the file list from the destination device folder
- name: Get files in a folder
find:
paths:
"{{backupdir}}/{{inventory_hostname}}"
register: found_files
# get the latest file attributes if the folder is not
empty
- name: get the latest old file
set_fact:
latest_file: "{{ found_files.files |
sort(attribute='mtime',reverse=true) | first }}"
when: found_files.matched != 0
# get the stat of the latest old config
- name: get the stat of the old config
stat:
path: "{{latest_file.path}}"
register: oldconfig_stat
when: found_files.matched != 0
# save the new config file to folder
- name: save output to {{backupdir}}
copy:
content: "{{config.stdout[0]}}"
dest:
"{{backupdir}}/{{inventory_hostname}}/config_{{inventory_hostname}}_{{time.stdout}}.txt"
# get the stat of the new config
- name: get the stat of the new config
stat:
path:
"{{backupdir}}/{{inventory_hostname}}/config_{{inventory_hostname}}_{{time.stdout}}.txt"
register: newconfig_stat
when: found_files.matched != 0
# if the new config is the same as the latest old
config, delete the new config file, delete the new file if it's the same
- name: compare the new config and the latest
old config
file:
state: absent
path:
"{{backupdir}}/{{inventory_hostname}}/config_{{inventory_hostname}}_{{time.stdout}}.txt"
when:
- found_files.matched != 0
- newconfig_stat.stat.checksum ==
oldconfig_stat.stat.checksum
# if the new config is different than the latest old
config, do nothing
|