Skip to content

Commit 478765f

Browse files
Changes to rest module to choose from the OS using utils and action plugin
1 parent 73df401 commit 478765f

26 files changed

+128
-48
lines changed

plugins/action/dtc/fabric_check_sync.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from ansible.utils.display import Display
2828
from ansible.plugins.action import ActionBase
29+
from .rest_module_utils import get_rest_module
2930

3031

3132
display = Display()
@@ -40,8 +41,15 @@ def run(self, tmp=None, task_vars=None):
4041

4142
fabric = self._task.args["fabric"]
4243

44+
network_os = task_vars['ansible_network_os']
45+
rest_module = get_rest_module(network_os)
46+
if not rest_module:
47+
results['failed'] = True
48+
results['msg'] = f"Unsupported network_os: {network_os}"
49+
return results
50+
4351
ndfc_response = self._execute_module(
44-
module_name=task_vars['ansible_network_os_rest'],
52+
module_name=rest_module,
4553
module_args={
4654
"method": "GET",
4755
"path": f"/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{fabric}/inventory/switchesByFabric",

plugins/action/dtc/fabrics_config_save.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424

2525
__metaclass__ = type
2626

27+
2728
from ansible.utils.display import Display
2829
from ansible.plugins.action import ActionBase
30+
from .rest_module_utils import get_rest_module
2931

3032

3133
display = Display()
@@ -42,8 +44,14 @@ def run(self, tmp=None, task_vars=None):
4244

4345
for fabric in fabrics:
4446
display.display(f"Executing config-save on Fabric: {fabric}")
47+
network_os = task_vars['ansible_network_os']
48+
rest_module = get_rest_module(network_os)
49+
if not rest_module:
50+
results['failed'] = True
51+
results['msg'] = f"Unsupported network_os: {network_os}"
52+
return results
4553
ndfc_config_save = self._execute_module(
46-
module_name=task_vars['ansible_network_os_rest'],
54+
module_name=rest_module,
4755
module_args={
4856
"method": "POST",
4957
"path": f"/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{fabric}/config-save",

plugins/action/dtc/fabrics_deploy.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424

2525
__metaclass__ = type
2626

27+
2728
from ansible.utils.display import Display
2829
from ansible.plugins.action import ActionBase
30+
from .rest_module_utils import get_rest_module
2931

3032

3133
display = Display()
@@ -39,11 +41,17 @@ def run(self, tmp=None, task_vars=None):
3941
results['failed'] = False
4042

4143
fabrics = self._task.args["fabrics"]
42-
4344
for fabric in fabrics:
4445
display.display(f"Executing config-deploy on Fabric: {fabric}")
46+
network_os = task_vars['ansible_network_os']
47+
rest_module = get_rest_module(network_os)
48+
if not rest_module:
49+
results['failed'] = True
50+
results['msg'] = f"Unsupported network_os: {network_os}"
51+
return results
52+
4553
ndfc_deploy = self._execute_module(
46-
module_name=task_vars['ansible_network_os_rest'],
54+
module_name=rest_module,
4755
module_args={
4856
"method": "POST",
4957
"path": f"/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{fabric}/config-deploy?forceShowRun=false",

plugins/action/dtc/get_poap_data.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,23 @@ def _get_discovered(self, ip, role, hostname):
139139

140140
return discovered
141141

142+
142143
def refresh(self) -> None:
143144
"""
144145
### Summary
145146
Refresh POAP data from NDFC
146-
147147
"""
148148
self.refresh_succeeded = False
149149
self.refresh_message = None
150150

151+
from .rest_module_utils import get_rest_module
152+
network_os = self.task_vars['ansible_network_os']
153+
rest_module = get_rest_module(network_os)
154+
if not rest_module:
155+
self.refresh_message = f"Unsupported network_os: {network_os}"
156+
return
151157
data = self.execute_module(
152-
module_name=task_vars['ansible_network_os_rest'],
158+
module_name=rest_module,
153159
module_args={
154160
"method": self.poap_get_method,
155161
"path": self.poap_get_path

plugins/action/dtc/manage_child_fabric_networks.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424

2525
__metaclass__ = type
2626

27+
2728
from ansible.utils.display import Display
2829
from ansible.plugins.action import ActionBase
2930
from ansible.template import Templar
3031
from ansible.errors import AnsibleFileNotFound
32+
from .rest_module_utils import get_rest_module
3133

3234

3335
import json
@@ -53,6 +55,13 @@ def run(self, tmp=None, task_vars=None):
5355

5456
child_fabrics = msite_data['child_fabrics_data']
5557

58+
network_os = task_vars['ansible_network_os']
59+
rest_module = get_rest_module(network_os)
60+
if not rest_module:
61+
results['failed'] = True
62+
results['msg'] = f"Unsupported network_os: {network_os}"
63+
return results
64+
5665
for network in networks:
5766
network_attach_group_switches = [
5867
network_attach_groups_dict['switches']
@@ -130,7 +139,7 @@ def run(self, tmp=None, task_vars=None):
130139
# return results
131140

132141
ndfc_net = self._execute_module(
133-
module_name=task_vars['ansible_network_os_rest'],
142+
module_name=rest_module,
134143
module_args={
135144
"method": "GET",
136145
"path": f"/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/top-down/fabrics/{child_fabric}/networks/{network['name']}",
@@ -184,7 +193,7 @@ def run(self, tmp=None, task_vars=None):
184193
rendered_to_nice_json = templar.environment.filters['to_nice_json'](rendered_content)
185194

186195
ndfc_net_update = self._execute_module(
187-
module_name=task_vars['ansible_network_os_rest'],
196+
module_name=rest_module,
188197
module_args={
189198
"method": "PUT",
190199
"path": f"/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/top-down/fabrics/{child_fabric}/networks/{network['name']}",

plugins/action/dtc/manage_child_fabric_vrfs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424

2525
__metaclass__ = type
2626

27+
2728
from ansible.utils.display import Display
2829
from ansible.plugins.action import ActionBase
2930
from ansible.template import Templar
3031
from ansible.errors import AnsibleFileNotFound
32+
from .rest_module_utils import get_rest_module
3133

3234

3335
import json

plugins/action/dtc/manage_child_fabrics.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424

2525
__metaclass__ = type
2626

27+
2728
from ansible.utils.display import Display
2829
from ansible.plugins.action import ActionBase
30+
from .rest_module_utils import get_rest_module
2931

3032
display = Display()
3133

plugins/action/dtc/prepare_msite_child_fabrics_data.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424

2525
__metaclass__ = type
2626

27+
2728
from ansible.utils.display import Display
2829
from ansible.plugins.action import ActionBase
30+
from .rest_module_utils import get_rest_module
2931

3032
display = Display()
3133

plugins/action/dtc/prepare_msite_data.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
from ansible.utils.display import Display
2828
from ansible.plugins.action import ActionBase
29-
from ...plugin_utils.helper_functions import ndfc_get_fabric_attributes
30-
from ...plugin_utils.helper_functions import ndfc_get_fabric_switches
29+
from .rest_module_utils import get_rest_module
30+
from ...plugin_utils/helper_functions import ndfc_get_fabric_attributes
31+
from ...plugin_utils/helper_functions import ndfc_get_fabric_switches
3132
import re
3233

3334
display = Display()
Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,21 @@
3535

3636
class ActionModule(ActionBase):
3737
"""
38-
Action plugin to dynamically select between cisco.nac_dc_vxlan.dtc.rest_selector and cisco.dcnm.dcnm_rest
38+
Action plugin to dynamically select between cisco.nd.nd_rest and cisco.dcnm.dcnm_rest
3939
based on the ansible_network_os variable.
4040
"""
41+
REST_MODULES = {
42+
'cisco.nd.nd': 'cisco.nd.nd_rest',
43+
'cisco.dcnm.dcnm': 'cisco.dcnm.dcnm_rest'
44+
}
45+
46+
def get_rest_module(self, network_os):
47+
if not network_os:
48+
results['failed'] = True
49+
results['msg'] = "ansible_network_os is not defined in task_vars"
50+
else:
51+
results = self.REST_MODULES.get(network_os)
52+
return results
4153

4254
def run(self, tmp=None, task_vars=None):
4355
"""
@@ -59,14 +71,12 @@ def run(self, tmp=None, task_vars=None):
5971
module_args = self._task.args.copy()
6072

6173
# Get the network OS from task variables or module arguments
62-
network_os = task_vars.get('ansible_network_os') \
63-
or module_args.pop('ansible_network_os', None)
74+
network_os = task_vars.get('ansible_network_os')
75+
display.vvvv(f"Using OS module: {network_os}")
6476

6577
# Determine which module to use based on network_os
66-
if network_os == 'cisco.nd.nd':
67-
rest_module = 'cisco.nac_dc_vxlan.dtc.rest_selector'
68-
elif network_os == 'cisco.dcnm.dcnm':
69-
rest_module = 'cisco.dcnm.dcnm_rest'
78+
if network_os in self.REST_MODULES:
79+
rest_module = self.get_rest_module(network_os)
7080
else:
7181
results['failed'] = True
7282
results['msg'] = (
@@ -75,27 +85,40 @@ def run(self, tmp=None, task_vars=None):
7585
)
7686
return results
7787

78-
display.vvvv(f"Using REST module: {rest_module}")
88+
display.vvvv(f"Using ----------REST------------- module: {rest_module}")
7989

8090
try:
91+
display.vvvv(f"Executing module: {rest_module} with args: {module_args}")
92+
93+
self._task.vars['ansible_rest_os_module'] = rest_module
94+
95+
# Set the connection to httpapi
96+
if 'connection' not in task_vars:
97+
task_vars['connection'] = 'httpapi'
98+
8199
# Execute the appropriate REST module
82100
result = self._execute_module(
83101
module_name=rest_module,
84102
module_args=module_args,
85103
task_vars=task_vars,
86-
tmp=tmp
104+
tmp=tmp,
105+
wrap_async=False
87106
)
88107

89108
# Update results with the module's results
90109
if result.get('failed'):
91110
results.update(result)
111+
display.error(f"Module execution failed: {result.get('msg')}")
92112
else:
93113
results['changed'] = result.get('changed', False)
94114
results.update(result)
115+
display.vvvv(f"Module execution successful: {result}")
95116

96117
except Exception as e:
118+
import traceback
119+
error_trace = traceback.format_exc()
97120
results['failed'] = True
98-
results['msg'] = f"Failed to execute {rest_module}: {str(e)}"
121+
results['msg'] = f"Failed to execute {rest_module}: {str(e)}\n{error_trace}"
99122
display.error(results['msg'], wrap_text=False)
100123

101124
return results

0 commit comments

Comments
 (0)