1. Instalacion de ansible
sudo apt update
sudo apt install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
2. Crear el archivo de configuracion de ansible
tutorial@tutorial:~/myproject$ cat ansible.cfg[defaults]
inventory = ./hosts.cfg
3. Creacion del archivo de inventario (hosts.cfg)
tutorial@tutorial:~/myproject$ cat hosts.cfg
[all:vars]
ansible user=tutorial
ansible_ssh_pass=tutorial
ansible_network_os=ios
ansible_connection=network_cli
[Routers]
R1 ansible_host=1.1.1.1
R2 ansible_host=2.2.2.2
R3 ansible_host=3.3.3.3
4 Creacion de playbook
tutorial@tutorial:~/myproject$ cat backup.yml
---
- name: Backup
hosts: Routers
gather_facts: False
connection: local
tasks:
- name: Show run
ios_command:
commands:
- show run
register: config
- name: save output
copy:
content: "{{config.stdout[0]}}"
dest: "./backups/{{inventory_hostname}}-config.txt"
5. Ejecucion del playbook
tutorial@tutorial:~/myproject$ ansible-playbook backup.yml
PLAY [Backup] ******************************************************************
TASK [Show run] ****************************************************************
TASK [save output] *************************************************************
ok: [R3]
ok: [R2]
ok: [R1]
PLAY RECAP *********************************************************************
R1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
R2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
R3 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
6. Mostrar backups de configuraciones
tutorial@tutorial:~/myproject$ ls -al backups/
total 20
drwxrwxr-x 2 tutorial tutorial 4096 Mar 25 03:30 .
drwxrwxr-x 3 tutorial tutorial 4096 Mar 25 03:31 ..
-rw-rw-r-- 1 tutorial tutorial 1351 Mar 25 03:30 R1-config.txt
-rw-rw-r-- 1 tutorial tutorial 1319 Mar 25 03:30 R2-config.txt
-rw-rw-r-- 1 tutorial tutorial 1320 Mar 25 03:30 R3-config.txt