import micropip
import asyncio
async def instal_yaml():
await micropip.install("pyyaml")
# Check if there's an existing event loop and run the async function accordingly
try:
asyncio.get_event_loop().run_until_complete(instal_yaml())
except RuntimeError:
asyncio.run(instal_yaml())def convert_yaml_to_commands(config):
"""Convert YAML config to list of commands"""
commands = []
for line in config.split('\n'):
line = line.strip()
commands.append(line.lstrip())
return commandsimport yaml
file = """
R1:
netmiko:
host: clab-ospf_foundations__ospf_broadcast-r1
device_type: cisco_ios
username: admin
password: autonetops
port: 22
config:
interface Ethernet0/1
ip address 100.100.100.1 255.255.255.0
!
interface Tunnel1234
ip address 10.1.1.1 255.255.255.0
no ip redirects
ip nhrp map 10.1.1.2 100.100.100.2
ip nhrp map 10.1.1.3 100.100.100.3
ip nhrp map 10.1.1.4 100.100.100.4
ip nhrp network-id 111
ip ospf network broadcast
ip ospf priority 100
tunnel source Ethernet0/1
tunnel mode gre multipoint
!
interface Loopback0
ip address 1.1.1.1 255.255.255.255
ip ospf network point-to-point
!
router ospf 1
network 1.1.1.1 0.0.0.0 area 0
network 10.1.1.0 0.0.0.255 area 0
"""
data = yaml.safe_load(file)
commands = convert_yaml_to_commands(data["R1"]["config"])
print(data)
print(commands){'R1': {'netmiko': {'host': 'clab-ospf_foundations__ospf_broadcast-r1', 'device_type': 'cisco_ios', 'username': 'admin', 'password': 'autonetops', 'port': 22}, 'config': 'interface Ethernet0/1 ip address 100.100.100.1 255.255.255.0 interface Tunnel1234 ip address 10.1.1.1 255.255.255.0 no ip redirects ip nhrp map 10.1.1.2 100.100.100.2 ip nhrp map 10.1.1.3 100.100.100.3 ip nhrp map 10.1.1.4 100.100.100.4 ip nhrp network-id 111 ip ospf network broadcast ip ospf priority 100 tunnel source Ethernet0/1 tunnel mode gre multipoint interface Loopback0 ip address 1.1.1.1 255.255.255.255 ip ospf network point-to-point router ospf 1 network 1.1.1.1 0.0.0.0 area 0 network 10.1.1.0 0.0.0.255 area 0'}}
['interface Ethernet0/1 ip address 100.100.100.1 255.255.255.0 interface Tunnel1234 ip address 10.1.1.1 255.255.255.0 no ip redirects ip nhrp map 10.1.1.2 100.100.100.2 ip nhrp map 10.1.1.3 100.100.100.3 ip nhrp map 10.1.1.4 100.100.100.4 ip nhrp network-id 111 ip ospf network broadcast ip ospf priority 100 tunnel source Ethernet0/1 tunnel mode gre multipoint interface Loopback0 ip address 1.1.1.1 255.255.255.255 ip ospf network point-to-point router ospf 1 network 1.1.1.1 0.0.0.0 area 0 network 10.1.1.0 0.0.0.255 area 0']
file = """
R1:
netmiko:
host: clab-ospf_foundations__ospf_broadcast-r1
device_type: cisco_ios
username: admin
password: autonetops
port: 22
config: |
interface Ethernet0/1
ip address 100.100.100.1 255.255.255.0
!
interface Tunnel1234
ip address 10.1.1.1 255.255.255.0
no ip redirects
ip nhrp map 10.1.1.2 100.100.100.2
ip nhrp map 10.1.1.3 100.100.100.3
ip nhrp map 10.1.1.4 100.100.100.4
ip nhrp network-id 111
ip ospf network broadcast
ip ospf priority 100
tunnel source Ethernet0/1
tunnel mode gre multipoint
!
interface Loopback0
ip address 1.1.1.1 255.255.255.255
ip ospf network point-to-point
!
router ospf 1
network 1.1.1.1 0.0.0.0 area 0
network 10.1.1.0 0.0.0.255 area 0
"""
data = yaml.safe_load(file)
commands = convert_yaml_to_commands(data["R1"]["config"])
print(data)
print(commands){'R1': {'netmiko': {'host': 'clab-ospf_foundations__ospf_broadcast-r1', 'device_type': 'cisco_ios', 'username': 'admin', 'password': 'autonetops', 'port': 22}, 'config': 'interface Ethernet0/1\n ip address 100.100.100.1 255.255.255.0\ninterface Tunnel1234\n ip address 10.1.1.1 255.255.255.0\n no ip redirects\n ip nhrp map 10.1.1.2 100.100.100.2\n ip nhrp map 10.1.1.3 100.100.100.3\n ip nhrp map 10.1.1.4 100.100.100.4\n ip nhrp network-id 111\n ip ospf network broadcast\n ip ospf priority 100\n tunnel source Ethernet0/1\n tunnel mode gre multipoint\ninterface Loopback0\n ip address 1.1.1.1 255.255.255.255\n ip ospf network point-to-point\nrouter ospf 1\n network 1.1.1.1 0.0.0.0 area 0\n network 10.1.1.0 0.0.0.255 area 0\n'}}
['interface Ethernet0/1', 'ip address 100.100.100.1 255.255.255.0', 'interface Tunnel1234', 'ip address 10.1.1.1 255.255.255.0', 'no ip redirects', 'ip nhrp map 10.1.1.2 100.100.100.2', 'ip nhrp map 10.1.1.3 100.100.100.3', 'ip nhrp map 10.1.1.4 100.100.100.4', 'ip nhrp network-id 111', 'ip ospf network broadcast', 'ip ospf priority 100', 'tunnel source Ethernet0/1', 'tunnel mode gre multipoint', 'interface Loopback0', 'ip address 1.1.1.1 255.255.255.255', 'ip ospf network point-to-point', 'router ospf 1', 'network 1.1.1.1 0.0.0.0 area 0', 'network 10.1.1.0 0.0.0.255 area 0', '']