Introduction:
Ansible is a powerful automation tool that allows network administrators to efficiently manage and configure network devices, including Cisco network switches. With Ansible, you can automate repetitive tasks, ensure consistency across your network infrastructure, and greatly simplify the management process. In this blog post, we will walk you through the process of writing an Ansible playbook specifically designed to manage Cisco network switches.

Prerequisites:
Before diving into writing an Ansible playbook for Cisco switches, make sure you have the following prerequisites in place:

  1. Ansible Installation: Ensure that you have Ansible installed on your system. You can refer to the official Ansible documentation for installation instructions specific to your operating system.
  2. Access to Cisco Network Switches: Make sure you have SSH access to the Cisco network switches you intend to manage using Ansible. Also, ensure that you have the necessary administrative privileges to execute the required commands.
  3. Inventory File: Create an inventory file that lists the IP addresses or hostnames of your Cisco switches. This file will be used by Ansible to identify the devices it needs to manage.

Let’s now proceed with writing the Ansible playbook:

Step 1: Create the Playbook File
Start by creating a new file with a “.yaml” extension (e.g., cisco_switch_playbook.yaml). This file will contain the playbook’s instructions.

Step 2: Define the Hosts
In the playbook file, define the hosts section to specify the target hosts for the playbook. You can reference the inventory file or directly provide the IP addresses or hostnames of the Cisco switches. For example:

---
- name: Manage Cisco Switches
  hosts: cisco_switches
  gather_facts: false

Step 3: Establish SSH Connection
Next, define the connection details for SSH in the playbook file. This ensures Ansible can establish a secure connection with the Cisco switches. Add the following lines to the playbook:

  vars:
    ansible_connection: network_cli
    ansible_network_os: ios
    ansible_user: <your_ssh_username>
    ansible_password: <your_ssh_password>
    ansible_become: true
    ansible_become_method: enable

Replace <your_ssh_username> and <your_ssh_password> with the appropriate SSH credentials for your Cisco switches.

Step 4: Write Tasks
Within the playbook, specify the tasks you want Ansible to perform on the Cisco switches. For example, you might want to configure VLANs or enable specific protocols. Here’s an example task to configure a VLAN:

  tasks:
    - name: Configure VLAN
      ios_vlan:
        vlan_id: 100
        name: Production
        state: present

Step 5: Run the Playbook
Save the playbook file and execute it using the ansible-playbook command, providing the path to your playbook file:

$ ansible-playbook cisco_switch_playbook.yaml

Ansible will connect to the Cisco switches, execute the tasks defined in the playbook, and provide detailed output about the changes made.

Conclusion:
By leveraging Ansible’s powerful automation capabilities, you can simplify the management of your Cisco network switches. With the step-by-step guide provided in this blog post, you should now be equipped to write your own Ansible playbooks to configure and manage your Cisco switches effectively. Empower yourself with automation, save time, and ensure consistency across your network infrastructure. Happy automating!