This took me some time to get to work using Windows / Cygwin and my GNS3 VOSS simulated switch.
I struggled with getting Ansible + SSH to work from within Cygwin using key based authentication. But I wanted to test Ansible and Ansible-Playbook against VOSS so I persevered with simple user and password authentication which I got to work using a combination of files (ansible.cfg, inventory file = hosts and a test.yml file to run a single show command and display the output).
Hosts file
[voss]
192.168.211.10
ansible_ssh_common_args=’-o StrictHostKeyChecking=no’
ansible_network_os=voss
ansible_connection=network_cli
ansible_ssh_pass=rwa
[defaults]
inventory = inventory
host_key_checking = False
ansible_connection=network_cli
ssh_args = -o ControlMaster=no
First stage, was to get a ‘Pong’ response to my ‘Ping’:
-bash-4.4$ ansible -i ./inventory/hosts voss -u rwa -m ping -c network_cli -e ansible_network_os=voss
192.168.211.10 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“ping”: “pong”
Next, I wanted to run Ansible which would login to the switch and run a simple command:
-bash-4.4$ ansible -i ./inventory/hosts voss -u rwa -m voss_command -a “commands=’show clock'”
192.168.211.10 | SUCCESS => {
“ansible_facts”: {
“discovered_interpreter_python”: “/usr/bin/python”
},
“changed”: false,
“stdout”: [
“Sat Jun 13 13:02:15 2020 UTC”
],
“stdout_lines”: [
[
“Sat Jun 13 13:02:15 2020 UTC”
]
]
}
Now that Ansible could login and carry out a task I progressed to put a task into a Playbook:
—
– hosts: voss
– name: run show clock on remote devices
voss_command:
commands: show clock
debug:
var: output
ok: [192.168.211.10]
ok: [192.168.211.10]
ok: [192.168.211.10] => {
“output”: {
“changed”: false,
“failed”: false,
“stdout”: [
“Sat Jun 13 15:00:37 2020 UTC”
],
“stdout_lines”: [
[
“Sat Jun 13 15:00:37 2020 UTC”
]
]
}
}
192.168.211.10 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
– hosts: voss
gather_facts: false
– name: show run pipe prompt
voss_command:
commands:
– enable
– show run | i promptregister: output- name: show output
when: output.stdout is search(‘VOSS1’)
debug:
ok: [192.168.211.10] => {
“msg”: “prompt \”VOSS1\””
}
– hosts: voss
gather_facts: false
vlan_numbers: [100, 200]
– name: add vlans
voss_config:
commands:
– vlan create {{ item }} type port-mstprstp 0
with_items: “{{ vlan_numbers }}”
become: yes
changed: [192.168.211.10] => (item=100)
changed: [192.168.211.10] => (item=200)
192.168.211.10 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
100 VLAN-100 byPort 0 none N/A N/A 0
VOSS1:1#show vlan basic | i ‘200’
200 VLAN-200 byPort 0 none N/A N/A 0
– hosts: voss
gather_facts: falsevars:
vlans: {
“100”: {“description”: “floor1”, “ip”: “1.1.1.1”},
“200”: {“description”: “floor2”, “ip”: “1.1.2.1”}
}
– name: add vlans
voss_config:
commands:
– vlan create {{ item.key }} type port-mstprstp 0
with_dict: “{{ vlans }}”
become: yes- name: configure vlans
voss_config:
commands:
– ip address {{ item.value.ip }}/24
parents: interface vlan {{ item.key }}
with_dict: “{{ vlans }}”
become: yes- name: name vlans
voss_config:
commands:
– vlan name {{ item.key }} {{ item.value.description }}
with_dict: “{{ vlans }}”
become: yes
$ cat playbook1.yml
—
– name: “Play 1: Capture sys-info”
hosts: routers
connection: network_cli
tasks:
– name: “Task 1: Show sys-info”
voss_command:
commands: show sys-info
register: result
– name: “Task 2: Print output”
debug:
msg: “{{ result }}”
– name: “Task 3: Create files folder”
file:
path: “outputs”
state: directory
run_once: true
– name: “Task 4: Write stdout to file”
copy:
content: “{{ result.stdout[0] }}\n”
dest: “outputs/{{ inventory_hostname }}.txt”