how to connect to a Cisco Catalyst Switch from Ansible and retrieve the current configuration:
- Install Ansible. Linux | Mac | Windows
- Create an inventory file that lists the Cisco Catalyst Switch that you want to connect to.
- Create a playbook that contains the following tasks:
- A task to connect to the Cisco Catalyst Switch.
- A task to retrieve the current configuration.
- 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