0% found this document useful (0 votes)
18 views

Creating directories for Running and Startup configuration

Uploaded by

comp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Creating directories for Running and Startup configuration

Uploaded by

comp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

CREATING DIRECTORIES FOR STARTUP AND RUNNING CONFIGURATION:

- So, here, we're going to take some backup of the targeted decvice
configuration.

- "Napalm getters" in google.

- Here, in this page, we've got multi-vendor devices that is supported by


Napalm.

- So, we're going to use "get_config".

1. WRITING A PLAYBOOK WITH MULTIPLE PLAY:


- With this playbook, we're going to get the RUNNING_CONFIG and
STARTUP_CONFIG output of the targeted devices:

[root@localhost My-Automation-Stuff2]# cat example13-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 - Print the Output"


debug:
msg: "{{ result }}"

2. RUNNING THE PLAYBOOK:


[root@localhost My-Automation-Stuff2]# ansible-playbook example13-
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 }}]


***************************************************
ok: [localhost]

PLAY [Play-2: - Backing up Configuration]


********************************************************

TASK [Play-2: Task1 - Pull Configurations from Remote Device]


***********************************
ok: [arista1]
ok: [ios1]

TASK [Play-2 Task2 - Print the Output]


***********************************************************
ok: [ios1] => {
"msg": {
"ansible_facts": {
"napalm_config": {
"candidate": "",
"running": "!\n\n!\nversion 15.2\nservice timestamps debug datetime
msec\nservice timestamps log datetime msec\nno service password-encryption\nservice
compress-config\n!\nhostname ios1\n!\nboot-start-marker\nboot-end-marker\n!\n!\n!\
nusername admin privilege 15 secret 5 $1$0KhI$NYp9R/UG.0Qb/SnBL8r780\nno aaa new-
model\n!\n!\n!\n!\n!\n!\n!\n!\nip domain-name imedita.com\nip cef\nno ipv6 cef\n!\
n!\n!\nspanning-tree mode rapid-pvst\nspanning-tree extend system-id\n!\nvlan
internal allocation policy ascending\n!\n! \n!\n!\n!\n!\n!\n!\n!\n!\n!\n!\n!\n!\
ninterface GigabitEthernet0/1\n media-type rj45\n negotiation auto\n!\ninterface
GigabitEthernet0/2\n media-type rj45\n negotiation auto\n!\ninterface
GigabitEthernet0/3\n media-type rj45\n negotiation auto\n!\ninterface
GigabitEthernet0/0\n no switchport\n ip address 10.10.10.61 255.255.255.0\n
negotiation auto\n!\ninterface GigabitEthernet1/0\n media-type rj45\n negotiation
auto\n!\ninterface GigabitEthernet1/1\n media-type rj45\n negotiation auto\n!\
ninterface GigabitEthernet1/2\n media-type rj45\n negotiation auto\n!\ninterface
GigabitEthernet1/3\n media-type rj45\n negotiation auto\n!\nip forward-protocol nd\
n!\nno ip http server\nno ip http secure-server\n!\nip ssh version 2\n!\n!\n!\n!\
n!\ncontrol-plane\n!\nbanner exec ^C\
n**************************************************************************\n* IOSv
is strictly limited to use for evaluation, demonstration and IOS *\n* education.
IOSv is provided as-is and is not supported by Cisco's *\n* Technical Advisory
Center. Any use or disclosure, in whole or in part, *\n* of the IOSv Software or
Documentation to any third party for any *\n* purposes is expressly
prohibited except as otherwise authorized by *\n* Cisco in writing.
*\n**************************************************************************^C\
nbanner incoming ^C\
n**************************************************************************\n* IOSv
is strictly limited to use for evaluation, demonstration and IOS *\n* education.
IOSv is provided as-is and is not supported by Cisco's *\n* Technical Advisory
Center. Any use or disclosure, in whole or in part, *\n* of the IOSv Software or
Documentation to any third party for any *\n* purposes is expressly
prohibited except as otherwise authorized by *\n* Cisco in writing.
*\n**************************************************************************^C\
nbanner login ^C\
n**************************************************************************\n* IOSv
is strictly limited to use for evaluation, demonstration and IOS *\n* education.
IOSv is provided as-is and is not supported by Cisco's *\n* Technical Advisory
Center. Any use or disclosure, in whole or in part, *\n* of the IOSv Software or
Documentation to any third party for any *\n* purposes is expressly
prohibited except as otherwise authorized by *\n* Cisco in writing.
*\n**************************************************************************^C\n!\
nline con 0\nline aux 0\nline vty 0 4\n login local\n transport input ssh\n!\n!\
nend",
"startup": "Using 1546 out of 262144 bytes, uncompressed size =
3160 bytes\n!\n\n!\nversion 15.2\nservice timestamps debug datetime msec\nservice
timestamps log datetime msec\nno service password-encryption\nservice compress-
config\n!\nhostname ios1\n!\nboot-start-marker\nboot-end-marker\n!\n!\n!\nusername
admin privilege 15 secret 5 $1$0KhI$NYp9R/UG.0Qb/SnBL8r780\nno aaa new-model\n!\n!\
n!\n!\n!\n!\n!\n!\nip domain-name imedita.com\nip cef\nno ipv6 cef\n!\n!\n!\
nspanning-tree mode rapid-pvst\nspanning-tree extend system-id\n!\nvlan internal
allocation policy ascending\n!\n! \n!\n!\n!\n!\n!\n!\n!\n!\n!\n!\n!\n!\ninterface
GigabitEthernet0/1\n media-type rj45\n negotiation auto\n!\ninterface
GigabitEthernet0/2\n media-type rj45\n negotiation auto\n!\ninterface
GigabitEthernet0/3\n media-type rj45\n negotiation auto\n!\ninterface
GigabitEthernet0/0\n no switchport\n ip address 10.10.10.61 255.255.255.0\n
negotiation auto\n!\ninterface GigabitEthernet1/0\n media-type rj45\n negotiation
auto\n!\ninterface GigabitEthernet1/1\n media-type rj45\n negotiation auto\n!\
ninterface GigabitEthernet1/2\n media-type rj45\n negotiation auto\n!\ninterface
GigabitEthernet1/3\n media-type rj45\n negotiation auto\n!\nip forward-protocol nd\
n!\nno ip http server\nno ip http secure-server\n!\nip ssh version 2\n!\n!\n!\n!\
n!\ncontrol-plane\n!\nbanner exec ^C\
n**************************************************************************\n* IOSv
is strictly limited to use for evaluation, demonstration and IOS *\n* education.
IOSv is provided as-is and is not supported by Cisco's *\n* Technical Advisory
Center. Any use or disclosure, in whole or in part, *\n* of the IOSv Software or
Documentation to any third party for any *\n* purposes is expressly
prohibited except as otherwise authorized by *\n* Cisco in writing.
*\n**************************************************************************^C\
nbanner incoming ^C\
n**************************************************************************\n* IOSv
is strictly limited to use for evaluation, demonstration and IOS *\n* education.
IOSv is provided as-is and is not supported by Cisco's *\n* Technical Advisory
Center. Any use or disclosure, in whole or in part, *\n* of the IOSv Software or
Documentation to any third party for any *\n* purposes is expressly
prohibited except as otherwise authorized by *\n* Cisco in writing.
*\n**************************************************************************^C\
nbanner login ^C\
n**************************************************************************\n* IOSv
is strictly limited to use for evaluation, demonstration and IOS *\n* education.
IOSv is provided as-is and is not supported by Cisco's *\n* Technical Advisory
Center. Any use or disclosure, in whole or in part, *\n* of the IOSv Software or
Documentation to any third party for any *\n* purposes is expressly
prohibited except as otherwise authorized by *\n* Cisco in writing.
*\n**************************************************************************^C\n!\
nline con 0\nline aux 0\nline vty 0 4\n login local\n transport input ssh\n!\n!\
nend"
}
},
"changed": false,
"failed": false
}
}
ok: [arista1] => {
"msg": {
"ansible_facts": {
"napalm_config": {
"candidate": "",
"running": "! Command: show running-config\n! device: arista1
(vEOS-lab, EOS-4.31.0F)\n!\n! boot system flash:/vEOS-lab.swi\n!\nno aaa root\n!\
nusername admin privilege 15 role network-admin secret sha512
$6$x6M69Sl8EhAn7RPm$2mRpCtj4I4OyHyt.tNvJPyqTiQtGy7JHpjQaSbx5zEGB1kxfpIUXk4ZiB4Ia7kp
kFlsb9ntpXqstSX8zEPz2u.\n!\ntransceiver qsfp default-mode 4x10G\n!\nservice routing
protocols model multi-agent\n!\nhostname arista1\ndns domain imedita.com\n!\
nspanning-tree mode mstp\n!\nsystem l1\n unsupported speed action error\n
unsupported error-correction action error\n!\nvlan 11\n name eleven\n!\nvlan 12\n
name twelve\n!\nmanagement api http-commands\n no shutdown\n!\ninterface
Ethernet1\n!\ninterface Ethernet2\n!\ninterface Ethernet3\n!\ninterface
Management1\n ip address 10.10.10.71/24\n!\nno ip routing\n!\nip ssh client
source-interface Ethernet1\n!\nend\n",
"startup": "! Command: show startup-config\n! Startup-config last
modified at Mon Jan 29 11:25:43 2024 by admin\n! device: arista1 (vEOS-lab, EOS-
4.31.0F)\n!\n! boot system flash:/vEOS-lab.swi\n!\nno aaa root\n!\nusername admin
privilege 15 role network-admin secret sha512
$6$x6M69Sl8EhAn7RPm$2mRpCtj4I4OyHyt.tNvJPyqTiQtGy7JHpjQaSbx5zEGB1kxfpIUXk4ZiB4Ia7kp
kFlsb9ntpXqstSX8zEPz2u.\n!\ntransceiver qsfp default-mode 4x10G\n!\nservice routing
protocols model multi-agent\n!\nhostname arista1\ndns domain imedita.com\n!\
nspanning-tree mode mstp\n!\nsystem l1\n unsupported speed action error\n
unsupported error-correction action error\n!\nvlan 11\n name eleven\n!\nvlan 12\n
name twelve\n!\nmanagement api http-commands\n no shutdown\n!\ninterface
Ethernet1\n!\ninterface Ethernet2\n!\ninterface Ethernet3\n!\ninterface
Management1\n ip address 10.10.10.71/24\n!\nno ip routing\n!\nip ssh client
source-interface Ethernet1\n!\nend\n"
}
},
"changed": false,
"failed": false
}
}
PLAY RECAP
***********************************************************************************
****
arista1 : ok=2 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
ios1 : ok=2 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
localhost : ok=3 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0

- So, it creates a directory.

- So, here, you can see that the RUNNING_CONFIG and the STARTUP_CONFIG output
of the targeted devices.

3. VERIFYING THE DIRECTORY


[root@localhost My-Automation-Stuff2]# ls -l Backups/2024-01-30/
total 0

- Though, no file is is created or saved in the directory, as shown above:

NOTE:
- So, we're now going to create two different directories:

- One directory to save the running-configuration

- Another directory to save the startup-configuration

4. MODIFYING THE PLAYBOOK (FOR CREATING TWO DIRECTORIES FOR - RUNNING_CONFIG &
STARTUP_CONFIG):

- Now, let us try to split up the backup and store the RUNNING_CONFIG file on
one directory and the STARTUP_CONFIG file on
another directory.
[root@localhost My-Automation-Stuff2]# cat example14-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

5. RUNNING THE PLAYBOOK:


[root@localhost My-Automation-Stuff2]# ansible-playbook example14-
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]

PLAY RECAP
***********************************************************************************
****
arista1 : ok=1 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
ios1 : ok=3 changed=2 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, now what it did is;


1. Created a Backup folder
2. Created a Running folder and saved all the running configuration on
that folder.
3. Created a Startup Folder and saved all the startup configuration on
that folder.
6. VERIFYING THE DIRECTORIES THAT ARE CREATED BY THE ABOVE PLAYBOOK:
[root@localhost My-Automation-Stuff2]# ls Backups/2024-01-30/
running-configs startup-configs

[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

- Now, the directories are successfully created but no configurations were


saved on those recently created directories.

- So, let us now save the configurations of the running_config and the
startup_config on their respective directories and that
is going to be covered in the very next topic using the COPY MODULE.
***********************************************************************************
****************************************************
X-X-X-X-X

You might also like