Skip to content

Commit 73df401

Browse files
plugin introduced to select dcnm_rest or nd_rest
1 parent 772b281 commit 73df401

File tree

16 files changed

+154
-314
lines changed

16 files changed

+154
-314
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Copyright (c) 2024 Cisco Systems, Inc. and its affiliates
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
# this software and associated documentation files (the "Software"), to deal in
8+
# the Software without restriction, including without limitation the rights to
9+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
# the Software, and to permit persons to whom the Software is furnished to do so,
11+
# subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
#
23+
# SPDX-License-Identifier: MIT
24+
25+
from __future__ import absolute_import, division, print_function
26+
27+
__metaclass__ = type
28+
29+
from ansible.utils.display import Display
30+
from ansible.plugins.action import ActionBase
31+
32+
33+
display = Display()
34+
35+
36+
class ActionModule(ActionBase):
37+
"""
38+
Action plugin to dynamically select between cisco.nac_dc_vxlan.dtc.rest_selector and cisco.dcnm.dcnm_rest
39+
based on the ansible_network_os variable.
40+
"""
41+
42+
def run(self, tmp=None, task_vars=None):
43+
"""
44+
Execute the action plugin.
45+
46+
Args:
47+
tmp (str, optional): Temporary directory. Defaults to None.
48+
task_vars (dict, optional): Dictionary of task variables. Defaults to None.
49+
50+
Returns:
51+
dict: Results of the REST API call
52+
"""
53+
# Initialize results dictionary
54+
results = super(ActionModule, self).run(tmp, task_vars)
55+
results['changed'] = False
56+
results['failed'] = False
57+
58+
# Get task arguments
59+
module_args = self._task.args.copy()
60+
61+
# 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)
64+
65+
# 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'
70+
else:
71+
results['failed'] = True
72+
results['msg'] = (
73+
f"Unsupported network_os: {network_os}. "
74+
"Must be 'cisco.nd.nd' or 'cisco.dcnm.dcnm'"
75+
)
76+
return results
77+
78+
display.vvvv(f"Using REST module: {rest_module}")
79+
80+
try:
81+
# Execute the appropriate REST module
82+
result = self._execute_module(
83+
module_name=rest_module,
84+
module_args=module_args,
85+
task_vars=task_vars,
86+
tmp=tmp
87+
)
88+
89+
# Update results with the module's results
90+
if result.get('failed'):
91+
results.update(result)
92+
else:
93+
results['changed'] = result.get('changed', False)
94+
results.update(result)
95+
96+
except Exception as e:
97+
results['failed'] = True
98+
results['msg'] = f"Failed to execute {rest_module}: {str(e)}"
99+
display.error(results['msg'], wrap_text=False)
100+
101+
return results

roles/dtc/connectivity_check/tasks/main.yml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,12 @@
4646
nd_version: "{{ nd_version_response.json.major }}.{{ nd_version_response.json.minor }}.{{ nd_version_response.json.maintenance }}{{ nd_version_response.json.patch }}"
4747
tags: "{{ nac_tags.connectivity_check }}" # Tags defined in roles/common_global/vars/main.yml
4848

49-
- name: Get Cisco NDFC Version for ND
50-
cisco.nd.nd_rest:
49+
- name: Get Cisco NDFC Version
50+
cisco.nac_dc_vxlan.dtc.rest_selector:
5151
method: GET
5252
path: /appcenter/cisco/ndfc/api/about/version
5353
register: ndfc_version
5454
tags: "{{ nac_tags.connectivity_check }}" # Tags defined in roles/common_global/vars/main.yml
55-
when: ansible_network_os_rest == "cisco.nd.nd_rest"
56-
57-
- name: Get Cisco NDFC Version for DCNM
58-
cisco.dcnm.dcnm_rest:
59-
method: GET
60-
path: /appcenter/cisco/ndfc/api/about/version
61-
register: ndfc_version
62-
tags: "{{ nac_tags.connectivity_check }}" # Tags defined in roles/common_global/vars/main.yml
63-
when: ansible_network_os_rest == "cisco.dcnm.dcnm_rest"
6455

6556
- name: Set Cisco NDFC Version Var
6657
ansible.builtin.set_fact:

roles/dtc/create/tasks/common/devices_discovery.yml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,9 @@
9090
when: results.policy_update | length > 0
9191
delegate_to: localhost
9292

93-
- name: Update Switch Hostname Policy in NDFC
94-
cisco.nd.nd_rest:
93+
- name: Update Switch Hostname Policy
94+
cisco.nac_dc_vxlan.dtc.rest_selector:
9595
method: PUT
9696
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies/{{ policy_ids }}/bulk"
9797
json_data: "{{ results.policy_update.values() | list | to_json }}"
98-
when: results.policy_update | length > 0 and ansible_network_os_rest == "cisco.nd.nd_rest"
99-
100-
- name: Update Switch Hostname Policy in DCNM
101-
cisco.dcnm.dcnm_rest:
102-
method: PUT
103-
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies/{{ policy_ids }}/bulk"
104-
json_data: "{{ results.policy_update.values() | list | to_json }}"
105-
when: results.policy_update | length > 0 and ansible_network_os_rest == "cisco.dcnm.dcnm_rest"
98+
when: results.policy_update | length > 0

roles/dtc/create/tasks/external/devices_discovery.yml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,9 @@
5252
when: results.policy_update | length > 0
5353
delegate_to: localhost
5454

55-
- name: Update Switch Hostname Policy in NDFC
56-
cisco.nd.nd_rest:
55+
- name: Update Switch Hostname Policy
56+
cisco.nac_dc_vxlan.dtc.rest_selector:
5757
method: PUT
5858
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies/{{ policy_ids }}/bulk"
5959
json_data: "{{ results.policy_update.values() | list | to_json }}"
60-
when: results.policy_update | length > 0 and ansible_network_os_rest == "cisco.nd.nd_rest"
61-
62-
- name: Update Switch Hostname Policy in DCNM
63-
cisco.dcnm.dcnm_rest:
64-
method: PUT
65-
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/policies/{{ policy_ids }}/bulk"
66-
json_data: "{{ results.policy_update.values() | list | to_json }}"
67-
when: results.policy_update | length > 0 and ansible_network_os_rest == "cisco.dcnm.dcnm_rest"
60+
when: results.policy_update | length > 1

roles/dtc/create/tasks/external/fabric.yml

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,62 +28,29 @@
2828
- "+ Manage Fabric External {{ MD_Extended.vxlan.fabric.name }}"
2929
- "----------------------------------------------------------------"
3030

31-
- name: Check if fabric External {{ MD_Extended.vxlan.fabric.name }} exists in NDFC
32-
cisco.nd.nd_rest:
31+
- name: Check if fabric External {{ MD_Extended.vxlan.fabric.name }} exists
32+
cisco.nac_dc_vxlan.dtc.rest_selector:
3333
method: GET
3434
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}"
35-
when: ansible_network_os_rest == "cisco.nd.nd_rest"
3635
register: get_result
3736
failed_when: false
3837

39-
- name: Check if fabric External {{ MD_Extended.vxlan.fabric.name }} exists in DCNM
40-
cisco.dcnm.dcnm_rest:
41-
method: GET
42-
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}"
43-
when: ansible_network_os_rest == "cisco.dcnm.dcnm_rest"
44-
register: get_result
45-
failed_when: false
46-
47-
- name: Manage fabric External {{ MD_Extended.vxlan.fabric.name }} in ND (PUT)
48-
cisco.nd.nd_rest:
38+
- name: Manage fabric External {{ MD_Extended.vxlan.fabric.name }} (PUT)
39+
cisco.nac_dc_vxlan.dtc.rest_selector:
4940
method: PUT
5041
path: '/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}/External_Fabric'
5142
payload: '{{ fabric_config }}'
5243
when:
53-
- ansible_network_os_rest == "cisco.nd.nd_rest"
5444
- get_result.response.DATA is defined
5545
- get_result.response.DATA
5646
register: put_result
5747

58-
- name: Manage fabric External {{ MD_Extended.vxlan.fabric.name }} in NDFC (PUT)
59-
cisco.dcnm.dcnm_rest:
60-
method: PUT
61-
path: '/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}/External_Fabric'
62-
json_data: '{{ fabric_config | to_json }}'
63-
when:
64-
- ansible_network_os_rest == "cisco.dcnm.dcnm_rest"
65-
- get_result.response.DATA is defined
66-
- get_result.response.DATA
67-
register: put_result
68-
69-
- name: Manage fabric External {{ MD_Extended.vxlan.fabric.name }} in ND (POST)
70-
cisco.nd.nd_rest:
48+
- name: Manage fabric External {{ MD_Extended.vxlan.fabric.name }} (POST)
49+
cisco.nac_dc_vxlan.dtc.rest_selector:
7150
method: POST
7251
path: '/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}/External_Fabric'
7352
payload: '{{ fabric_config }}'
7453
when: >
7554
(get_result.response.DATA is not defined) or
7655
(get_result.response.DATA is defined and not get_result.get('response', false) and not get_result.get('response').get('DATA'))
77-
and ansible_network_os_rest == "cisco.nd.nd_rest"
78-
register: post_result
79-
80-
- name: Manage fabric External {{ MD_Extended.vxlan.fabric.name }} in NDFC (POST)
81-
cisco.dcnm.dcnm_rest:
82-
method: POST
83-
path: '/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}/External_Fabric'
84-
json_data: '{{ fabric_config | to_json }}'
85-
when: >
86-
(get_result.response.DATA is not defined) or
87-
(get_result.response.DATA is defined and not get_result.get('response', false) and not get_result.get('response').get('DATA'))
88-
and ansible_network_os_rest == "cisco.dcnm.dcnm_rest"
8956
register: post_result

roles/dtc/create/tasks/sub_main_vxlan.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,13 @@
5858
- name: Config-Save block to propagate vPC changes to the fabric
5959
block:
6060
- name: Config-Save for Fabric {{ MD_Extended.vxlan.fabric.name }}
61-
cisco.nd.nd_rest:
61+
cisco.nac_dc_vxlan.dtc.rest_selector:
6262
method: POST
6363
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}/config-save"
64-
when: MD_Extended.vxlan.topology.switches | length > 0 and ansible_network_os_rest == "cisco.nd.nd_rest"
64+
when: MD_Extended.vxlan.topology.switches | length > 0
6565
register: config_save
6666
# TODO: Need to add logic to only save if changes are made
6767

68-
- name: Config-Save for Fabric {{ MD_Extended.vxlan.fabric.name }}
69-
cisco.dcnm.dcnm_rest:
70-
method: POST
71-
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}/config-save"
72-
when: MD_Extended.vxlan.topology.switches | length > 0 and ansible_network_os_rest == "cisco.dcnm.dcnm_rest"
73-
register: config_save
74-
7568
rescue:
7669
- name: Config-Save for Fabric {{ MD_Extended.vxlan.fabric.name }} - Failed
7770
ansible.builtin.debug:

roles/dtc/create/tasks/vxlan/vrfs_networks.yml

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,10 @@
3030

3131
- name: Check If Current Fabric Is An Active Multisite Fabric
3232
block:
33-
- name: Get Multisite Fabric Associations for nd
34-
cisco.nd.nd_rest:
33+
- name: Get Multisite Fabric Associations
34+
cisco.nac_dc_vxlan.dtc.rest_selector:
3535
method: GET
3636
path: /appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/msd/fabric-associations
37-
when: ansible_network_os_rest == "cisco.nd.nd_rest"
38-
register: ndfc_msd_fabric_associations
39-
40-
- name: Get Multisite Fabric Associations for dcnm
41-
cisco.dcnm.dcnm_rest:
42-
method: GET
43-
path: /appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/msd/fabric-associations
44-
when: ansible_network_os_rest == "cisco.dcnm.dcnm_rest"
4537
register: ndfc_msd_fabric_associations
4638

4739
- name: Find Current Fabric by Name In Multisite Associations
@@ -81,19 +73,7 @@
8173
# Manage Loopback VRF attachments on NDFC
8274
# --------------------------------------------------------------------
8375
- name: Attach Loopbacks to VRFs for nd
84-
cisco.nd.nd_rest:
85-
method: POST
86-
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/top-down/v2/fabrics/{{ MD_Extended.vxlan.fabric.name }}/vrfs/attachments"
87-
json_data: "{{ vars_common_vxlan.vrf_attach_config | to_json}}"
88-
when:
89-
- MD_Extended.vxlan.overlay.vrfs is defined
90-
- MD_Extended.vxlan.overlay.vrfs
91-
- vars_common_vxlan.changes_detected_vrfs
92-
- not is_active_child_fabric
93-
- ansible_network_os_rest == "cisco.nd.nd_rest"
94-
95-
- name: Attach Loopbacks to VRFs for dcnm
96-
cisco.dcnm.dcnm_rest:
76+
cisco.nac_dc_vxlan.dtc.rest_selector:
9777
method: POST
9878
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/top-down/v2/fabrics/{{ MD_Extended.vxlan.fabric.name }}/vrfs/attachments"
9979
json_data: "{{ vars_common_vxlan.vrf_attach_config | to_json}}"
@@ -102,7 +82,6 @@
10282
- MD_Extended.vxlan.overlay.vrfs
10383
- vars_common_vxlan.changes_detected_vrfs
10484
- not is_active_child_fabric
105-
- ansible_network_os_rest == "cisco.dcnm.dcnm_rest"
10685

10786
- name: Fail If Current Fabric is Part of Multisite And Attempting to Manage Networks
10887
ansible.builtin.fail:

roles/dtc/deploy/tasks/sub_main_external.yml

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,60 +30,29 @@
3030

3131
- ansible.builtin.debug: msg="Configuring NXOS Devices using NDFC (Direct to Controller)"
3232

33-
- name: Config-Save block for nd
33+
- name: Config-Save block
3434
block:
3535
- name: Config-Save for Fabric {{ MD_Extended.vxlan.fabric.name }}
36-
cisco.nd.nd_rest:
36+
cisco.nac_dc_vxlan.dtc.rest_selector:
3737
method: POST
3838
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}/config-save"
3939
register: config_save
4040
when: >
41-
(MD_Extended.vxlan.topology.switches is defined and MD_Extended.vxlan.topology.switches | length > 0) and
42-
ansible_network_os_rest == "cisco.nd.nd_rest"
41+
(MD_Extended.vxlan.topology.switches is defined and MD_Extended.vxlan.topology.switches | length > 0)
4342
# TODO: Need to add logic to only save if changes are made
4443

4544
rescue:
4645
- name: Config-Save for Fabric {{ MD_Extended.vxlan.fabric.name }} - Failed
4746
ansible.builtin.debug:
4847
msg: "{{ config_save.msg.DATA }}"
4948

50-
- name: Config-Save block for dcnm
51-
block:
52-
- name: Config-Save for Fabric {{ MD_Extended.vxlan.fabric.name }}
53-
cisco.dcnm.dcnm_rest:
54-
method: POST
55-
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}/config-save"
56-
register: config_save
57-
when: >
58-
(MD_Extended.vxlan.topology.switches is defined and MD_Extended.vxlan.topology.switches | length > 0) and
59-
ansible_network_os_rest == "cisco.dcnm.dcnm_rest"
60-
# TODO: Need to add logic to only save if changes are made
61-
62-
rescue:
63-
- name: Config-Save for Fabric {{ MD_Extended.vxlan.fabric.name }} - Failed
64-
ansible.builtin.debug:
65-
msg: "{{ config_save.msg.DATA }}"
66-
67-
- name: Deploy for Fabric {{ MD_Extended.vxlan.fabric.name }} for nd
68-
cisco.nd.nd_rest:
69-
method: POST
70-
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}/config-deploy?forceShowRun=false"
71-
vars:
72-
ansible_command_timeout: 3000
73-
ansible_connect_timeout: 3000
74-
when: >
75-
(MD_Extended.vxlan.topology.switches is defined and MD_Extended.vxlan.topology.switches | length > 0) and
76-
ansible_network_os_rest == "cisco.nd.nd_rest"
77-
# TODO: Need to add logic to only deploy if changes are made
78-
79-
- name: Deploy for Fabric {{ MD_Extended.vxlan.fabric.name }} for dcnm
80-
cisco.dcnm.dcnm_rest:
49+
- name: Deploy for Fabric {{ MD_Extended.vxlan.fabric.name }}
50+
cisco.nac_dc_vxlan.dtc.rest_selector:
8151
method: POST
8252
path: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ MD_Extended.vxlan.fabric.name }}/config-deploy?forceShowRun=false"
8353
vars:
8454
ansible_command_timeout: 3000
8555
ansible_connect_timeout: 3000
8656
when: >
87-
(MD_Extended.vxlan.topology.switches is defined and MD_Extended.vxlan.topology.switches | length > 1) and
88-
ansible_network_os_rest == "cisco.dcnm.dcnm_rest"
57+
(MD_Extended.vxlan.topology.switches is defined and MD_Extended.vxlan.topology.switches | length > 0)
8958
# TODO: Need to add logic to only deploy if changes are made

0 commit comments

Comments
 (0)