Skip to content

Commit 4aafc2e

Browse files
committed
Added code for python lab and some fixes for the bash lab
1 parent 9e1b32e commit 4aafc2e

11 files changed

Lines changed: 133 additions & 36 deletions

File tree

01-iosxr-01-cli-automation-bash/ztp_bash/ansible/playbook.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
shell: source /pkg/bin/ztp_helper.sh && xrcmd "show configuration running-config interface loopback2"
1717
register: output
1818

19-
- debug: msg=output.stdout_lines
19+
- debug:
20+
var: output.stdout_lines
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
!
22
interface loopback2
3-
ipv4 address 100.100.100.100/24
3+
ipv4 address 100.100.100.100/32
44
!
55
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
!
22
interface loopback2
3-
ipv4 address 200.200.200.200/24
3+
ipv4 address 200.200.200.200/32
44
!
55
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[routers]
2+
r1 ansible_user="admin" ansible_password="admin" ansible_host=10.10.20.170 ansible_port=2222 hostname=r1
3+
r2 ansible_user="admin" ansible_password="admin" ansible_host=10.10.20.170 ansible_port=2232 hostname=r2
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
- hosts: routers
3+
strategy: debug
4+
become: yes
5+
gather_facts: no
6+
7+
tasks:
8+
- debug: msg="hostname={{hostname}}"
9+
- name: Copy and Execute the Python Configuration script on the router
10+
script: /root/{{ hostname }}/configure_loopback2.py
11+
register: output
12+
13+
- debug:
14+
var: output.stdout_lines
15+
16+
- name: Copy the show command script to the router
17+
copy: src=/root/{{ hostname }}/show_loopback_config.py dest=/home/admin/show_loopback_config.py owner=admin force=yes mode=u+x
18+
19+
- name: Execute the show command script
20+
shell: /home/admin/show_loopback_config.py
21+
register: output
22+
23+
- debug:
24+
var: output.stdout_lines
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
sys.path.append("/pkg/bin")
5+
from ztp_helper import ZtpHelpers
6+
from pprint import pprint
7+
8+
loopback2_config="""!
9+
interface loopback2
10+
ipv4 address 100.100.100.100/32
11+
!
12+
end
13+
"""
14+
15+
16+
ztp_obj = ZtpHelpers()
17+
18+
response = ztp_obj.xrapply_string(cmd=loopback2_config)
19+
20+
pprint(response)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
sys.path.append("/pkg/bin")
5+
from ztp_helper import ZtpHelpers
6+
from pprint import pprint
7+
8+
ztp_obj = ZtpHelpers()
9+
10+
response = ztp_obj.xrcmd({"exec_cmd" : "show running-config interface loopback2"})
11+
12+
pprint(response)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
sys.path.append("/pkg/bin")
5+
from ztp_helper import ZtpHelpers
6+
from pprint import pprint
7+
8+
loopback2_config="""!
9+
interface loopback2
10+
ipv4 address 200.200.200.200/32
11+
!
12+
end
13+
"""
14+
15+
16+
ztp_obj = ZtpHelpers()
17+
18+
response = ztp_obj.xrapply_string(cmd=loopback2_config)
19+
20+
pprint(response)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
sys.path.append("/pkg/bin")
5+
from ztp_helper import ZtpHelpers
6+
from pprint import pprint
7+
8+
ztp_obj = ZtpHelpers()
9+
10+
response = ztp_obj.xrcmd({"exec_cmd" : "show running-config interface loopback2"})
11+
12+
pprint(response)

01-iosxr-02-cli-automation-python/ztp_python/configure_ospf_bgp.py

100755100644
Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/usr/bin/env python
22

3-
import sys
3+
import sys,os
44
sys.path.append("/pkg/bin/")
55
from ztp_helper import ZtpHelpers
6+
from pprint import pprint
67

7-
ParameterMap = {
8+
ParameterMap = {
89
"r1" : {
910
"local_asn" : 65000,
1011
"remote_asn" : 65000,
@@ -26,19 +27,19 @@ class ZtpChildClass(ZtpHelpers):
2627
def get_hostname(self):
2728

2829
show_command= "show running-config hostname"
29-
30+
3031
response = self.xrcmd({"exec_cmd" : show_command})
3132

3233
if response["status"] == "success":
3334
try:
3435
output = response["output"]
3536
hostname_config = output[0]
36-
hostname = hostname_config.split()[1]
37+
hostname = hostname_config.split()[1]
3738
return hostname
3839
except Exception as e:
3940
print("Failed to extract hostname")
4041
print("Error: " + str(e))
41-
return ""
42+
return ""
4243
else:
4344
print("Failed to fetch hostname configuration")
4445
return ""
@@ -59,28 +60,28 @@ def configure_ospf(self):
5960
!
6061
end
6162
"""
62-
63+
6364
try:
6465
response = self.xrapply_string(cmd=ospf_config)
6566

6667
if response["status"] == "success":
67-
print("OSPF configuration successfully applied")
68-
print(response["output"])
68+
print("\nOSPF configuration successfully applied\n")
69+
pprint(response["output"])
6970
return True
7071
else:
71-
print("Failed to apply OSPF configuration")
72-
print(response["output"])
72+
print("\nFailed to apply OSPF configuration\n")
73+
pprint(response["output"])
7374
return False
7475
except Exception as e:
75-
print("Failed to apply OSPF configuration")
76+
print("\nFailed to apply OSPF configuration\n")
7677
print("Error : "+str(e))
7778
return False
7879

7980

80-
def configure_bgp(asn=None, hostname=None):
81+
def configure_bgp(self):
82+
83+
hostname = self.get_hostname()
8184

82-
hostname = self.get_hostname()
83-
8485
if hostname == "":
8586
print("Require hostname to determine bgp config, aborting")
8687
return False
@@ -96,27 +97,28 @@ def configure_bgp(asn=None, hostname=None):
9697
!
9798
!
9899
end
99-
""".format(local_asn = ParameterMap[hostname][local_asn],
100-
neighbor_ip = ParameterMap[hostname][bgp_neighbor_ip],
101-
remote_asn = ParameterMap[hostname][remote_asn])
100+
""".format(local_asn = ParameterMap[hostname]["local_asn"],
101+
neighbor_ip = ParameterMap[hostname]["bgp_neighbor_ip"],
102+
remote_asn = ParameterMap[hostname]["remote_asn"])
102103

103104
with open("/tmp/bgp_config", 'w') as fd:
104105
fd.write(bgp_config)
105106

106107
try:
107-
response = self.xrapply(filename="/tmp/bgp_config")
108+
response = self.xrapply(filename="/tmp/bgp_config",
109+
reason="iBGP config using xrapply_with_reason")
108110
os.remove("/tmp/bgp_config")
109111

110112
if response["status"] == "success":
111-
print("BGP configuration successfully applied")
112-
print(response["output"])
113+
print("\nBGP configuration successfully applied\n")
114+
pprint(response["output"])
113115
return True
114116
else:
115-
print("Failed to apply BGP configuration")
116-
print(response["output"])
117+
print("\nFailed to apply BGP configuration\n")
118+
pprint(response["output"])
117119
return False
118120
except Exception as e:
119-
print("Failed to apply BGP configuration")
121+
print("\nFailed to apply BGP configuration\n")
120122
print("Error : "+str(e))
121123
os.remove("/tmp/bgp_config")
122124
return False
@@ -135,24 +137,24 @@ def configure_loopback(self):
135137
ipv4 address {loopback0_ip}
136138
!
137139
end
138-
""".format(loopback0_ip = ParameterMap[hostname][loopback0_ip])
140+
""".format(loopback0_ip = ParameterMap[hostname]["loopback0_ip"])
139141

140142
try:
141143
response = self.xrapply_string(cmd=loopback0_config)
142144

143145
if response["status"] == "success":
144-
print("Loopback0 configuration successfully applied")
145-
print(response["output"])
146+
print("\nLoopback0 configuration successfully applied\n")
147+
pprint(response["output"])
146148
return True
147149
else:
148-
print("Failed to apply Loopback0 configuration")
149-
print(response["output"])
150+
print("\nFailed to apply Loopback0 configuration\n")
151+
pprint(response["output"])
150152
return False
151153
except Exception as e:
152-
print("Failed to apply Loopback0 configuration")
154+
print("\nFailed to apply Loopback0 configuration\n")
153155
print("Error : "+str(e))
154156
return False
155-
157+
156158

157159
if __name__ == "__main__":
158160

@@ -161,7 +163,7 @@ def configure_loopback(self):
161163
if not config_obj.configure_loopback():
162164
print("Couldn't apply loopback0 configuration, aborting")
163165
sys.exit(1)
164-
166+
165167
if not config_obj.configure_ospf():
166168
print("Couldn't apply ospf configuration, aborting")
167169
sys.exit(1)

0 commit comments

Comments
 (0)