Project 1: Overview of connecting to a Cisco Switch


how to connect to a Cisco Catalyst Switch from Ansible and retrieve the current configuration:

  1. Install Ansible. Linux | Mac | Windows
  2. Create an inventory file that lists the Cisco Catalyst Switch that you want to connect to.
  3. Create a playbook that contains the following tasks:
    • A task to connect to the Cisco Catalyst Switch.
    • A task to retrieve the current configuration.
  4. Run the playbook.

Here is an example of an inventory file:

Code snippet

[switches]
switch1 ansible_host=192.168.1.100

Here is an example of a playbook:

Code snippet

---
- hosts: switches
  tasks:
  - name: Connect to switch
    ansible.netcommon.network_cli:
      host: "{{ item }}"
      connection: network_cli
      become: yes
      become_method: enable
      user: ansible
      password: ansible

  - name: Retrieve current configuration
    ansible.netcommon.network_cli:
      host: "{{ item }}"
      connection: network_cli
      become: yes
      become_method: enable
      command: show running-config
      register: running_config

  - debug:
      var: running_config.stdout

To run the playbook, use the following command:

Code snippet

ansible-playbook playbook.yml

This will connect to the Cisco Catalyst Switch, retrieve the current configuration, and store it in the running_config.stdout variable. You can then view the configuration by running the following command:

Code snippet

cat running_config.stdout

David Prestidge

IT Industry Professional for over 20 years, with experiance from Helpdesk, Systems Administration and Network Administration. I now run Bonded Stream, a MSP that focuses on Network Management at scale.

Recent Posts