Automating VLAN Creation on Cisco Devices with Ansible
Automating VLAN Creation on Cisco Devices with Ansible
Ansible is a powerful automation tool that simplifies network management tasks, including creating VLANs on Cisco devices. For beginners, this guide will walk you through automating VLAN creation step-by-step, from setting up Ansible to deploying VLAN configurations.
What is a VLAN?
A VLAN (Virtual Local Area Network) is a logical group of devices within a network that can communicate as if they were on the same physical network, regardless of their physical location. VLANs improve network efficiency and security by segmenting traffic.
Why Use Ansible for VLAN Automation?
- Consistency: Avoid manual configuration errors.
- Efficiency: Configure multiple devices in seconds.
- Scalability: Manage large-scale networks easily.
- Flexibility: Supports various Cisco devices and integrates with other tools.
Prerequisites
-
Cisco Device Configuration:
- Ensure your Cisco devices support SSH and are configured to allow Ansible to connect.
- Enable SSH and set up a user with necessary privileges:
conf t ip domain-name yourdomain.com crypto key generate rsa modulus 2048 ip ssh version 2 username ansible_user privilege 15 secret your_password line vty 0 4 transport input ssh login local exit
-
Ansible Installed on Your System:
- Install Ansible on your Linux/Windows machine:
sudo apt update && sudo apt install ansible -y # Ubuntu
- Confirm installation:
ansible --version
- Install Ansible on your Linux/Windows machine:
-
Python Libraries: Install
paramiko
andnetmiko
for network automation:pip install paramiko netmiko
Step 1: Set Up Ansible Inventory
The inventory file specifies the devices Ansible will manage. Create an inventory file (e.g., inventory.yml
):
all:
hosts:
cisco_switch:
ansible_host: 192.168.1.1 # Replace with your device IP
ansible_user: ansible_user # SSH username
ansible_password: your_password # SSH password
ansible_network_os: ios
Save the file as inventory.yml
.
Step 2: Create the Ansible Playbook
An Ansible playbook defines the tasks to be executed. Create a playbook named create_vlan.yml
:
- name: Automate VLAN Creation on Cisco Devices
hosts: cisco_switch
gather_facts: no
tasks:
- name: Create VLAN
cisco.ios.ios_config:
lines:
- vlan 10
- name Marketing
save_when: changed
- name: Verify VLAN
cisco.ios.ios_command:
commands:
- show vlan brief
Explanation of the Playbook
-
Module:
cisco.ios.ios_config
: Configures the Cisco device.cisco.ios.ios_command
: Runs commands on the Cisco device.
-
Tasks:
- Create VLAN: Adds VLAN 10 with the name "Marketing."
- Verify VLAN: Executes the
show vlan brief
command to confirm VLAN creation.
-
save_when: changed
: Ensures the configuration is saved only if there are changes.
Step 3: Install the Cisco Ansible Collection
Ansible uses collections for specific device types. Install the Cisco collection:
ansible-galaxy collection install cisco.ios
Step 4: Run the Playbook
Execute the playbook to create the VLAN:
ansible-playbook -i inventory.yml create_vlan.yml
Step 5: Verify the Results
Check the output to ensure the VLAN was successfully created. You can also manually verify by logging into the switch and running:
show vlan brief
Extending the Playbook
You can extend the playbook to create multiple VLANs. Modify the playbook as follows:
- name: Automate VLAN Creation on Cisco Devices
hosts: cisco_switch
gather_facts: no
tasks:
- name: Create Multiple VLANs
cisco.ios.ios_config:
lines:
- vlan 10
- name Marketing
- vlan 20
- name Sales
- vlan 30
- name IT
save_when: changed
Common Troubleshooting Tips
-
SSH Connection Issues:
- Ensure the switch allows SSH and the Ansible user has the correct credentials.
- Test SSH connectivity using:
ssh ansible_user@192.168.1.1
-
Unsupported Modules:
- Ensure the Cisco device runs a compatible IOS version.
- Verify the
cisco.ios
collection is installed.
-
Playbook Errors:
- Use the
-vvv
flag for detailed logs:ansible-playbook -i inventory.yml create_vlan.yml -vvv
- Use the
Conclusion
Using Ansible to automate VLAN creation on Cisco devices saves time and ensures consistency across your network. With a basic understanding of Ansible and some practice, you'll be ready to tackle more complex network automation tasks.
Ansible is an excellent tool for both beginners and experienced network engineers. Start small, and soon you'll be automating all aspects of your network!
Comments