USING THE COPY MODULE:
- Web Browser:
- "Ansible Copy" in google.com
- "Ansible.builtin.copy module"
- Scroll down and you get to see a lot of parameters that you can use
in Copy Module.
- Content
- Now, let us see some of the parameters that we're going to work on.
1. WRITING PLAYBOOK (Saving the Running configuration on startup_config directory):
- Now, in this playbook, we're going to take all those running_configs and
startup_configs and save it in the respective
directories.
[root@localhost My-Automation-Stuff2]# cat example15-run_config_backup.yml
---
- name: "Play-1: Testing Playbook"
hosts: localhost
tasks:
- name: "Play-1: Task1 - Collection facts from the localhost"
ansible.builtin.setup:
filter:
- "ansible_date_time"
register: output
- name: "Play-1: Task2 - Recording Variable"
set_fact:
TD: "{{ ansible_date_time.date }}"
- name: "Play-1: Task3 - Creating Directories }}"
file:
path: "Backups/{{ TD }}"
#or/home/trainonic/My-Automation-Stuff-2/Backups/{{ TD }}
state: directory
run_once: true
- name: "Play-2: - Backing up Configuration"
hosts: all
connection: network_cli
tasks:
- name: "Play-2: Task1 - Pull Configurations from Remote Device"
napalm_get_facts:
hostname: "{{ ansible_host }}"
username: "{{ ansible_user }}"
password: "{{ ansible_password }}"
dev_os: "{{ napalm_platform }}"
filter: ["config"]
register: result
- name: "Play-2 Task2 - Creating Sub-Directory for Running_Config"
file:
path: "Backups/{{ hostvars.localhost.TD }}/running-configs"
state: directory
run_once: true
- name: "Play-2 Task3 - Creating Sub-Directory for startup_Config"
file:
path: "Backups/{{ hostvars.localhost.TD }}/startup-configs"
state: directory
run_once: true
- name: "Play-2 Task4 - Copy Startup_Configs to Disk"
copy:
content: "{{ result.ansible_facts.napalm_config.startup }}"
dest: "Backups/{{ hostvars.localhost.TD
}}/startup-configs/{{ inventory_hostname }}-startup-configs.txt"
2. VERIFYING THE DIRECTORIES BEFORE RUNNING THE PLAYBOOK:
[root@localhost My-Automation-Stuff2]# ls -l Backups/2024-01-30/running-configs/
total 0
[root@localhost My-Automation-Stuff2]# ls -l Backups/2024-01-30/startup-configs/
total 0
- So, no files are here, in those running_config and startup_config
directories.
3. RUNNING THE PLAYBOOK:
[root@localhost My-Automation-Stuff2]# ansible-playbook example15-
run_config_backup.yml
PLAY [Play-1: Testing Playbook]
******************************************************************
TASK [Play-1: Task1 - Collection facts from the localhost]
***************************************
ok: [localhost]
TASK [Play-1: Task2 - Recording Variable]
********************************************************
ok: [localhost]
TASK [Play-1: Task3 - Creating Directories }}]
***************************************************
changed: [localhost]
PLAY [Play-2: - Backing up Configuration]
********************************************************
TASK [Play-2: Task1 - Pull Configurations from Remote Device]
***********************************
ok: [arista1]
ok: [ios1]
TASK [Play-2 Task2 - Creating Sub-Directory for Running_Config]
**********************************
changed: [ios1]
TASK [Play-2 Task3 - Creating Sub-Directory for startup_Config]
**********************************
changed: [ios1]
TASK [Play-2 Task4 - Copy Startup_Configs to Disk]
***********************************************
changed: [arista1]
changed: [ios1]
PLAY RECAP
***********************************************************************************
****
arista1 : ok=3 changed=2 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
ios1 : ok=5 changed=4 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
localhost : ok=3 changed=1 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
- So, we can see, the playbook ran successfuly.
- Let us now, verify the configurations.
4. VERIFYING TEH DIRECTORIES:
[root@localhost My-Automation-Stuff2]# ls -l Backups/2024-01-30/startup-configs/
total 8
-rw-r--r--. 1 root root 884 Jan 30 18:47 arista1-startup-configs.txt
-rw-r--r--. 1 root root 3168 Jan 30 18:47 ios1-startup-configs.txt
- So, now you can see, the running configurations of the targeted
devices of IOS and Arista are stored in the
running_configs directory. Similarly, the startup configurations of
the targeted devices are stored in the
startup_configs directory.
5. CHANGE THE HOSTNAME IN THE TARGET DEVICE:
#enable
#configure terminal
#hostname IOS101
6. WRITING PLAYBOOK (Saving the configuration on running_config directory):
- Now, in this playbook, we're going to take all those running_configs and
startup_configs and save it in the respective
directories.
[root@localhost My-Automation-Stuff2]# cat example15-run_config_backup.yml
---
- name: "Play-1: Testing Playbook"
hosts: localhost
tasks:
- name: "Play-1: Task1 - Collection facts from the localhost"
ansible.builtin.setup:
filter:
- "ansible_date_time"
register: output
- name: "Play-1: Task2 - Recording Variable"
set_fact:
TD: "{{ ansible_date_time.date }}"
- name: "Play-1: Task3 - Creating Directories }}"
file:
path: "Backups/{{ TD }}"
#or/home/trainonic/My-Automation-Stuff-2/Backups/{{ TD }}
state: directory
run_once: true
- name: "Play-2: - Backing up Configuration"
hosts: all
connection: network_cli
tasks:
- name: "Play-2: Task1 - Pull Configurations from Remote Device"
napalm_get_facts:
hostname: "{{ ansible_host }}"
username: "{{ ansible_user }}"
password: "{{ ansible_password }}"
dev_os: "{{ napalm_platform }}"
filter: ["config"]
register: result
- name: "Play-2 Task2 - Creating Sub-Directory for Running_Config"
file:
path: "Backups/{{ hostvars.localhost.TD }}/running-configs"
state: directory
run_once: true
- name: "Play-2 Task3 - Creating Sub-Directory for startup_Config"
file:
path: "Backups/{{ hostvars.localhost.TD }}/startup-configs"
state: directory
run_once: true
- name: "Play-2 Task4 - Copy Startup_Configs to Disk"
copy:
content: "{{ result.ansible_facts.napalm_config.startup }}"
dest: "Backups/{{ hostvars.localhost.TD
}}/startup-configs/{{ inventory_hostname }}-startup-configs.txt"
- name: "Play-2 Task5 - Copy Running_Configs to Disk"
copy:
content: "{{ result.ansible_facts.napalm_config.running }}"
dest: "Backups/{{ hostvars.localhost.TD
}}/running-configs/{{ inventory_hostname }}-running-config.txt"
7. VERIFYING TEH DIRECTORIES:
[root@localhost My-Automation-Stuff2]# ls -l Backups/2024-01-30/startup-configs/
total 8
-rw-r--r--. 1 root root 884 Jan 30 18:47 arista1-startup-configs.txt
-rw-r--r--. 1 root root 3168 Jan 30 18:47 ios1-startup-configs.txt
- So, now you can see, the running configurations of the targeted
devices of IOS and Arista are stored in the
running_configs directory. Similarly, the startup configurations of
the targeted devices are stored in the
startup_configs directory.
8. COMPARING FILES:
[root@localhost My-Automation-Stuff2]# diff
Backups/2024-01-30/running-configs/ios1-running-config.txt
Backups/2024-01-30/startup-configs/ios1-startup-configs.txt
0a1
> Using 1546 out of 262144 bytes, uncompressed size = 3160 bytes
- So, doing this, if you see anything mentioned over here, means that there's
something changed or modified:
- "<" signifies information in the running configuration.
- ">" signifies infomration in the startup configuration.
***********************************************************************************
****************************************************
X-X-X-X-X