Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions collection_cisco_ios.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
- name: "Demonstrate connecting to switches"
hosts: switches
gather_facts: no

vars:
ansible_network_os: "cisco.ios.ios"

tasks:
###
# Collect data
#
# - name: Gather facts (eos)
# arista.eos.eos_facts:
# when: ansible_network_os == 'arista.eos.eos'

- name: Gather facts (ios)
cisco.ios.ios_facts:
when: ansible_network_os == 'cisco.ios.ios'

# - name: Gather facts (vyos)
# vyos.vyos.vyos_facts:
# when: ansible_network_os == 'vyos.vyos.vyos'

###
# Demonstrate variables
#
- name: Display some facts
debug:
msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}"

- name: Facts from a specific host
debug:
var: hostvars['ios-01.example.net']

- name: Write facts to disk using a template
copy:
content: |
#jinja2: lstrip_blocks: True
IOS device info:
{% for host in groups['ios'] %}
Hostname: {{ hostvars[host].ansible_net_hostname }}
Version: {{ hostvars[host].ansible_net_version }}
Model: {{ hostvars[host].ansible_net_model }}
Serial: {{ hostvars[host].ansible_net_serialnum }}
{% endfor %}
dest: /tmp/switch-facts
run_once: yes

###
# Get running configuration
#

# - name: Backup switch (eos)
# arista.eos.eos_config:
# backup: yes
# register: backup_eos_location
# when: ansible_network_os == 'arista.eos.eos'
#
# - name: backup switch (vyos)
# vyos.vyos.vyos_config:
# backup: yes
# register: backup_vyos_location
# when: ansible_network_os == 'vyos.vyos.vyos'

- name: backup switch (ios)
cisco.ios.ios_config:
backup: yes
register: backup_ios_location
when: ansible_network_os == 'cisco.ios.ios'

- name: Create backup dir
file:
path: "/tmp/backups/{{ inventory_hostname }}"
state: directory
recurse: yes

# - name: Copy backup files into /tmp/backups/ (eos)
# copy:
# src: "{{ backup_eos_location.backup_path }}"
# dest: "/tmp/backups/{{ inventory_hostname }}/{{ inventory_hostname }}.bck"
# when: ansible_network_os == 'arista.eos.eos'
#
# - name: Copy backup files into /tmp/backups/ (vyos)
# copy:
# src: "{{ backup_vyos_location.backup_path }}"
# dest: "/tmp/backups/{{ inventory_hostname }}/{{ inventory_hostname }}.bck"
# when: ansible_network_os == 'vyos.vyos.vyos'

- name: Copy backup files into /tmp/backups/ (ios)
copy:
src: "{{ backup_ios_location.backup_path }}"
dest: "/tmp/backups/{{ inventory_hostname }}/{{ inventory_hostname }}.bck"
when: ansible_network_os == 'isco.ios.ios'

44 changes: 44 additions & 0 deletions inventories/network_inventory.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[all:vars]
# these defaults can be overridden for any group in the [group:vars] section
ansible_connection=ansible.netcommon.network_cli
ansible_user=ansible

[switches:children]
eos
ios
vyos

[eos]
veos01 ansible_host=veos-01.example.net
veos02 ansible_host=veos-02.example.net
veos03 ansible_host=veos-03.example.net
veos04 ansible_host=veos-04.example.net

[eos:vars]
ansible_become=yes
ansible_become_method=enable
ansible_network_os=arista.eos.eos
ansible_user=my_eos_user
ansible_password=my_eos_password

[ios]
ios01 ansible_host=ios-01.example.net
ios02 ansible_host=ios-02.example.net
ios03 ansible_host=ios-03.example.net

[ios:vars]
ansible_become=yes
ansible_become_method=enable
ansible_network_os=cisco.ios.ios
ansible_user=my_ios_user
ansible_password=my_ios_password

[vyos]
vyos01 ansible_host=vyos-01.example.net
vyos02 ansible_host=vyos-02.example.net
vyos03 ansible_host=vyos-03.example.net

[vyos:vars]
ansible_network_os=vyos.vyos.vyos
ansible_user=my_vyos_user
ansible_password=my_vyos_password