This Article will contain Ansible playbook example for Device configuration comparison.
Prerequsites (assumptions):
- Ansible installed on Linux
- Devise Configuration happaning daily and stored in /home/administrator/network-programmability/backups/SWITCH/
Ansible notes will be posted here and continuesly updated on the fly when required.
Ansible environment as always devided for 2 servers: PRODuction and DEVelopment.
Tested scrypts will be moved from DEV into PROD server once fully tested and ready.
Manual execution (for test):
ansible-playbook diff.yml
Output files location:
ls home/administrator/network-programmability/
Scheduling done over crontub:
# Execute against config DIFF
5 6 * * * if ! out=`ansible-playbook /etc/ansible/diff.yml
`; then echo $out; fi
Playbook:
--
## Playbook to get system time and append it to backup files ## Made by DM ## Last change 09/06/2021 - hosts: localhost tasks: - name: Get ansible date/time facts setup: filter: "ansible_date_time" gather_subset: "!all" - name: Store DTG as fact set_fact: DTG: "{{ ansible_date_time.date }}" DTGY: "{{ '%Y-%m-%d'|strftime(ansible_date_time.epoch|int - 86400*1) }}" - hosts: SWITCH connection: network_cli gather_facts: no tasks: - name: DIFF shell: "diff /home/administrator/network-programmability/backups/SWITCH/{{hostvars.localhost.DTGY}}/{{inventory_hostname}}-{{hostvars.localhost.DTGY}}-config.txt /home/administrator/network-programmability/backups/SWITCH/{{hostvars.localhost.DTG}}/{{inventory_hostname}}-{{hostvars.localhost.DTG}}-config.txt " register: diff_output failed_when: diff_output.rc >= 2 delegate_to: localhost - name: COPY copy: content: "{{ diff_output.stdout }}" dest: "/home/administrator/network-programmability/backups/SWITCH/DIFF/{{inventory_hostname}}-{{hostvars.localhost.DTGY}}-{{hostvars.localhost.DTG}}.txt" when: diff_output.rc == 1 failed_when: diff_output.rc >= 2 delegate_to: localhost - name: Send a success email mail: host: smtp.mydomain.com.au port: 25 from: This email address is being protected from spambots. You need JavaScript enabled to view it. (Ansible Automation) to: - USER01 <This email address is being protected from spambots. You need JavaScript enabled to view it.> - USER02 <This email address is being protected from spambots. You need JavaScript enabled to view it.> subject: "Compare {{ inventory_hostname }} config {{ hostvars.localhost.DTG }} against {{ hostvars.localhost.DTGY }}" body: "This email include changes for {{inventory_hostname}} compare to config day before. \n {{ diff_output.stdout }} \n \n DO NOT REPLY TO THIS EMAIL" attach: "/home/administrator/network-programmability/backups/SWITCH/DIFF/{{inventory_hostname}}-{{hostvars.localhost.DTGY}}-{{hostvars.localhost.DTG}}.txt" when: diff_output.rc == 1 failed_when: diff_output.rc >= 2 delegate_to: localhost