- Host File
The hosts file leveraged for this specific use-case:
cisco@linux-dev:~/nxos-ansible$ more hosts
[all:vars]
ansible_connection = local
[spine]
dean
cisco@linux-dev:~/nxos-ansible$
The playbook used for this particular use-case
cisco@linux-dev:~/nxos-ansible/ansible_playbooks$ more vlans.yml
# vlans.yml
---
- name: VLANs
hosts: all
connection: local
gather_facts: no
tasks:
- name: ensure VLANs 2-20 and 99 exist on all switches
nxos_vlan: vlan_id="2-20,99" state=present host={{ inventory_hostname }}
- name: config VLANs names for a few VLANs
nxos_vlan: vlan_id={{ item.vid }} name={{ item.name }} host={{ inventory_hostname }} state=present
with_items:
- { vid: 2, name: web }
- { vid: 3, name: app }
- { vid: 4, name: db }
- { vid: 20, name: server }
- { vid: 99, name: native }
- Checking VLAN
In this step we are verifying the VLANS are not configured in the switch
n9k-sw-1# sh vlan brief
VLAN | Name | Status | Ports |
---|---|---|---|
1 | default | active | Eth1/1, Eth1/2, Eth1/3, Eth1/4 |
Eth1/5, Eth1/6, Eth1/7, Eth1/8 | |||
Eth1/9, Eth1/10, Eth1/11 | |||
Eth1/12, Eth1/13, Eth1/14 | |||
Eth1/15, Eth1/16, Eth1/17 | |||
Eth1/18, Eth1/19, Eth1/20 | |||
Eth1/21, Eth1/22, Eth1/23 | |||
Eth1/24, Eth1/25, Eth1/26 | |||
Eth1/27, Eth1/28, Eth1/29 | |||
Eth1/30, Eth1/31, Eth1/32 | |||
Eth1/33, Eth1/34, Eth1/35 | |||
Eth1/36, Eth1/37, Eth1/38 | |||
Eth1/39, Eth1/40, Eth1/41 | |||
Eth1/42, Eth1/43, Eth1/44 | |||
Eth1/45, Eth1/46, Eth1/47 | |||
Eth1/48, Eth1/49, Eth1/50 | |||
Eth1/51, Eth1/52, Eth1/53 | |||
Eth1/54 |
n9k-sw-1#
- Execute Playbook
Now run the playbook by executing ansible-playbook vlans.yml
cisco@linux-dev:~/nxos-ansible/ansible_playbooks$ ansible-playbook vlans.yml
PLAY [VLANs] ******************************************************************
TASK: [ensure VLANs 2-20 and 99 exist on all switches] ************************
changed: [dean]
TASK: [config VLANs names for a few VLANs] ************************************
changed: [dean] => (item={'name': 'web', 'vid': 2})
changed: [dean] => (item={'name': 'app', 'vid': 3})
changed: [dean] => (item={'name': 'db', 'vid': 4})
changed: [dean] => (item={'name': 'server', 'vid': 20})
changed: [dean] => (item={'name': 'native', 'vid': 99})
PLAY RECAP ********************************************************************
dean : ok=2 changed=2 unreachable=0 failed=0
cisco@linux-dev:~/nxos-ansible/ansible_playbooks$
- Verify VLAN Creation
Check the switch to verify the VLANs have been created
n9k-sw-1# sh vlan brief
VLAN | Name | Status | Ports |
---|---|---|---|
1 | default | active | Eth1/1, Eth1/2, Eth1/3, Eth1/4 |
Eth1/5, Eth1/6, Eth1/7, Eth1/8 | |||
Eth1/9, Eth1/10, Eth1/11 | |||
Eth1/12, Eth1/13, Eth1/14 | |||
Eth1/15, Eth1/16, Eth1/17 | |||
Eth1/18, Eth1/19, Eth1/20 | |||
Eth1/21, Eth1/22, Eth1/23 | |||
Eth1/24, Eth1/25, Eth1/26 | |||
Eth1/27, Eth1/28, Eth1/29 | |||
Eth1/30, Eth1/31, Eth1/32 | |||
Eth1/33, Eth1/34, Eth1/35 | |||
Eth1/36, Eth1/37, Eth1/38 | |||
Eth1/39, Eth1/40, Eth1/41 | |||
Eth1/42, Eth1/43, Eth1/44 | |||
Eth1/45, Eth1/46, Eth1/47 | |||
Eth1/48, Eth1/49, Eth1/50 | |||
Eth1/51, Eth1/52, Eth1/53 | |||
Eth1/54 | |||
2 | web | active | |
3 | app | active | |
4 | db | active | |
5 | VLAN0005 | active | |
6 | VLAN0006 | active | |
7 | VLAN0007 | active | |
8 | VLAN0008 | active | |
9 | VLAN0009 | active | |
10 | VLAN0010 | active | |
11 | VLAN0011 | active | |
12 | VLAN0012 | active | |
13 | VLAN0013 | active | |
14 | VLAN0014 | active | |
15 | VLAN0015 | active | |
16 | VLAN0016 | active | |
17 | VLAN0017 | active | |
18 | VLAN0018 | active | |
19 | VLAN0019 | active | |
20 | server | active | |
99 | native | active |