Create Some Configuration using Excel and Subtasks¶
This task uses an excel file as input to generate some cisco ios configuration using subtasks.
vlan_id |
vlan_name |
vrf |
ip_address |
netmask |
description |
remove |
|---|---|---|---|---|---|---|
10 |
VLAN_10 |
|||||
11 |
VLAN_11 |
1.1.1.1 |
255.255.255.0 |
desc 11 |
||
12 |
VLAN_12 |
VRF_B |
2.2.2.2 |
255.255.255.0 |
desc 12 |
|
13 |
VLAN_13 |
VRF_C |
3.3.3.3 |
255.255.255.0 |
desc 13 |
y |
The task just loads the excel data, calls the subtask subtasks/create_vlan_ios/create_vlan.ios.j2 and returns the collected results
run.py¶
retval=""
# load excel from path defined in defaults.yaml (excel_data_path) and iterate over each line
for line_no,cfg in file.excel.load(excel_data_path).items():
# run subtask/subtasklet without an output plugin and inject excel data into self.configuration of the subtask
if cfg.get("remove"):
cfg["undo"]=True
result=task.run(".subtasks/create_vlan_ios",{**self.configuration,**cfg}, output_plugin="null")[0]
# as we have just a single tasklet so the result is always in [0]
if result["status"] == "ok":
retval += "\n" + result["result"]
else:
log.error(result["error"])
return retval
In the main task defaults just the path to the excel data is configured
defaults.yaml¶
excel_data_path: data.xlsx
The subtask is written as jinja2 task and has it’s own defaults.yaml
.subtasks/create_vlan_ios/create_vlan.ios.j2¶
{%if undo %}no {% endif %}vlan {{vlan_id}}
name {{vlan_name}}
{%if undo %}no interface vlan {{vlan_id}}{% endif %}
{% if ip_address and netmask and not undo %}
interface vlan {{vlan_id}}
{% if description %}
description {{description}}
{% endif %}
{% if vrf %}
ip vrf forwarding {{vrf}}
{% endif %}
ip address {{ip_address}} {{netmask}}
no shutdown
{% endif %}
.subtasks/create_vlan_ios/defaults.yaml¶
ip_address: False
netmask: False
description: False
vrf: False
The result of this task is a valid cisco ios vlan configuration.
result.ios¶
vlan 10
name VLAN_10
vlan 11
name VLAN_11
interface vlan 11
description desc 11
ip address 1.1.1.1 255.255.255.0
no shutdown
vlan 12
name VLAN_12
interface vlan 12
description desc 12
ip vrf forwarding VRF_B
ip address 2.2.2.2 255.255.255.0
no shutdown
no vlan 13
name VLAN_13
no interface vlan 13