Skip to content

Commit

Permalink
Ansible Sanity Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewiebe committed Apr 16, 2024
1 parent 97ce6b9 commit 66857f7
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 154 deletions.
6 changes: 1 addition & 5 deletions plugins/action/common/check_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@

__metaclass__ = type

from ansible import constants as C
from ansible.utils.display import Display
from ansible.plugins.action import ActionBase

from ..helper_functions import do_something

from pprint import pprint

display = Display()


class ActionModule(ActionBase):

def run(self, tmp=None, task_vars=None):
Expand Down
6 changes: 2 additions & 4 deletions plugins/action/common/nac_dc_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@

__metaclass__ = type

from ansible import constants as C
from ansible.utils.display import Display
from ansible.plugins.action import ActionBase

from ..helper_functions import do_something

import iac_validate.validator
from iac_validate.yaml import load_yaml_files
import os

display = Display()


class ActionModule(ActionBase):

def run(self, tmp=None, task_vars=None):
Expand Down Expand Up @@ -68,4 +66,4 @@ def run(self, tmp=None, task_vars=None):
# Return Schema Validated Model Data
results['data'] = load_yaml_files([mdata])

return results
return results
44 changes: 21 additions & 23 deletions plugins/action/common/prepare_plugins/001_prep_list_defaults.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

from ...helper_functions import data_model_key_check
from pprint import pprint


def update_nested_dict(nested_dict, keys, new_value):
if len(keys) == 1:
nested_dict[keys[0]] = new_value
else:
key = keys[0]
if key in nested_dict:
update_nested_dict(nested_dict[key], keys[1:], new_value)
if len(keys) == 1:
nested_dict[keys[0]] = new_value
else:
key = keys[0]
if key in nested_dict:
update_nested_dict(nested_dict[key], keys[1:], new_value)


class PreparePlugin:
Expand All @@ -21,7 +21,7 @@ def set_list_default(self, parent_keys, target_key):
dm_check = data_model_key_check(self.model_data, keys)
if target_key in dm_check['keys_not_found'] or \
target_key in dm_check['keys_no_data']:
update_nested_dict(self.model_data, keys, [])
update_nested_dict(self.model_data, keys, [])

# The prepare method is used to default each list or nested list
# in the model data to an empty list if the key for the list does
Expand All @@ -32,7 +32,6 @@ def set_list_default(self, parent_keys, target_key):
def prepare(self):
self.model_data = self.kwargs['results']['model_extended']


# --------------------------------------------------------------------
# Fabric Global List Defaults
# --------------------------------------------------------------------
Expand All @@ -55,10 +54,10 @@ def prepare(self):
parent_keys = ['fabric', 'topology']
dm_check = data_model_key_check(self.model_data, parent_keys)
if 'topology' in dm_check['keys_no_data']:
self.model_data['fabric']['topology'] = { 'edge_connections': []}
self.model_data['fabric']['topology'] = { 'fabric_links': []}
self.model_data['fabric']['topology'] = { 'switches': []}
self.model_data['fabric']['topology'] = { 'vpc_peers': []}
self.model_data['fabric']['topology'] = {'edge_connections': []}
self.model_data['fabric']['topology'] = {'fabric_links': []}
self.model_data['fabric']['topology'] = {'switches': []}
self.model_data['fabric']['topology'] = {'vpc_peers': []}

# Check fabric.topology.fabric_links list element
target_key = 'fabric_links'
Expand Down Expand Up @@ -86,7 +85,7 @@ def prepare(self):
dm_check = data_model_key_check(switch, ['freeforms'])
if 'freeforms' in dm_check['keys_not_found'] or \
'freeforms' in dm_check['keys_no_data']:
self.model_data['fabric']['topology']['switches'][list_index]['freeforms'] = []
self.model_data['fabric']['topology']['switches'][list_index]['freeforms'] = []

list_index += 1

Expand All @@ -99,7 +98,7 @@ def prepare(self):
for switch in self.model_data['fabric']['topology']['switches']:
dm_check = data_model_key_check(switch, ['interfaces'])
if 'interfaces' in dm_check['keys_not_found'] or 'interfaces' in dm_check['keys_no_data']:
self.model_data['fabric']['topology']['switches'][list_index]['interfaces'] = []
self.model_data['fabric']['topology']['switches'][list_index]['interfaces'] = []

list_index += 1

Expand All @@ -111,11 +110,10 @@ def prepare(self):
parent_keys = ['fabric', 'overlay_services']
dm_check = data_model_key_check(self.model_data, parent_keys)
if 'overlay_services' in dm_check['keys_no_data']:
self.model_data['fabric']['overlay_services'] = { 'vrfs': []}
self.model_data['fabric']['overlay_services'] = { 'vrf_attach_groups': []}
self.model_data['fabric']['overlay_services'] = { 'networks': []}
self.model_data['fabric']['overlay_services'] = { 'network_attach_groups': []}

self.model_data['fabric']['overlay_services'] = {'vrfs': []}
self.model_data['fabric']['overlay_services'] = {'vrf_attach_groups': []}
self.model_data['fabric']['overlay_services'] = {'networks': []}
self.model_data['fabric']['overlay_services'] = {'network_attach_groups': []}

# Check fabric.overlay_services.vrfs list element
target_key = 'vrfs'
Expand All @@ -131,7 +129,7 @@ def prepare(self):
dm_check = data_model_key_check(group, ['switches'])
if 'switches' in dm_check['keys_not_found'] or \
'switches' in dm_check['keys_no_data']:
self.model_data['fabric']['overlay_services']['vrf_attach_groups'][list_index]['switches'] = []
self.model_data['fabric']['overlay_services']['vrf_attach_groups'][list_index]['switches'] = []

list_index += 1

Expand All @@ -149,9 +147,9 @@ def prepare(self):
dm_check = data_model_key_check(group, ['switches'])
if 'switches' in dm_check['keys_not_found'] or \
'switches' in dm_check['keys_no_data']:
self.model_data['fabric']['overlay_services']['network_attach_groups'][list_index]['switches'] = []
self.model_data['fabric']['overlay_services']['network_attach_groups'][list_index]['switches'] = []

list_index += 1

self.kwargs['results']['model_extended'] = self.model_data
return self.kwargs['results']
return self.kwargs['results']
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from ...helper_functions import data_model_key_check


class PreparePlugin:
def __init__(self, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from ...helper_functions import data_model_key_check


class PreparePlugin:
def __init__(self, **kwargs):
Expand All @@ -9,7 +7,6 @@ def __init__(self, **kwargs):
def prepare(self):
model_data = self.kwargs['results']['model_extended']


# Rebuild sm_data['fabric']['overlay_services']['vrf_attach_groups'] into
# a structure that is easier to use.
vrf_grp_name_list = []
Expand All @@ -20,7 +17,6 @@ def prepare(self):
for switch in grp['switches']:
model_data['fabric']['overlay_services']['vrf_attach_groups_dict'][grp['name']].append(switch)


# Remove attach_group from vrf if the group_name is not defined
for vrf in model_data['fabric']['overlay_services']['vrfs']:
if 'attach_group' in vrf:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from ...helper_functions import data_model_key_check


## Count interfaces of different types and expose in extended service model for controls within playbooks
##
# Count interfaces of different types and expose in extended service model for controls within playbooks
#
class PreparePlugin:
def __init__(self, **kwargs):
self.kwargs = kwargs
Expand Down Expand Up @@ -33,35 +31,35 @@ def prepare(self):
for interface_mode in self.mode_direct:
# if interface mode is a direct match, then increment the count for that mode
if interface_mode == interface.get('mode'):
model_data['fabric']['topology']['interfaces']['modes'][interface_mode]['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes'][interface_mode]['count'] += 1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] += 1
# loop through interface modes indirect along with additional validation and count
if interface.get('mode') == 'access':
# if interface name starts with 'po' and has vpc_id, then it is a vpc access interface
if interface.get('name').lower().startswith('po') and interface.get('vpc_id'):
model_data['fabric']['topology']['interfaces']['modes']['access_vpc']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['access_vpc']['count'] += 1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] += 1
# if interface name starts with 'po', then it is a port-channel access interface
elif interface.get('name').lower().startswith('po'):
model_data['fabric']['topology']['interfaces']['modes']['access_po']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['access_po']['count'] += 1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] += 1
# else it is a regular access interface
else:
model_data['fabric']['topology']['interfaces']['modes']['access']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['access']['count'] += 1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] += 1
if interface.get('mode') == 'trunk':
# if interface name starts with 'po' and has vpc_id, then it is a vpc trunk interface
if interface.get('name').lower().startswith('po') and interface.get('vpc_id'):
model_data['fabric']['topology']['interfaces']['modes']['trunk_vpc']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['trunk_vpc']['count'] += 1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] += 1
# if interface name starts with 'po', then it is a port-channel trunk interface
elif interface.get('name').lower().startswith('po'):
model_data['fabric']['topology']['interfaces']['modes']['trunk_po']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['trunk_po']['count'] += 1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] += 1
# else it is a regular trunk interface
else:
model_data['fabric']['topology']['interfaces']['modes']['trunk']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] +=1
model_data['fabric']['topology']['interfaces']['modes']['trunk']['count'] += 1
model_data['fabric']['topology']['interfaces']['modes']['all']['count'] += 1

self.kwargs['results']['model_extended'] = model_data
return self.kwargs['results']
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Group vPC interfaces by vpc_peers, vpc_id and switch_name
## This helps in identifying vPC interfaces for a given vpc_peer, vpc_id and switch_name
## Reduces the need to loop through all interfaces to find vPC interfaces in Jinja2 templates
# Group vPC interfaces by vpc_peers, vpc_id and switch_name
# This helps in identifying vPC interfaces for a given vpc_peer, vpc_id and switch_name
# Reduces the need to loop through all interfaces to find vPC interfaces in Jinja2 templates
class PreparePlugin:
def __init__(self, **kwargs):
self.kwargs = kwargs
Expand All @@ -15,7 +15,8 @@ def prepare(self):
if model_data.get('fabric').get('topology').get('switches') is not None:
# Initialize fabric.topology.interfaces.vpc_interfaces
model_data['fabric']['topology']['interfaces'] = model_data.get('fabric').get('topology').get('interfaces', {})
model_data['fabric']['topology']['interfaces']['vpc_interfaces'] = model_data.get('fabric').get('topology').get('interfaces').get('vpc_interfaces', {})
model_data['fabric']['topology']['interfaces']['vpc_interfaces'] = \
model_data.get('fabric').get('topology').get('interfaces').get('vpc_interfaces', {})
# if fabric.topology.vpc_peers is defined
if model_data.get('fabric').get('topology').get('vpc_peers') is not None:
# Loop through each vpc_peers
Expand All @@ -31,15 +32,15 @@ def prepare(self):
# Check if interface has vpc_id
if interface.get('vpc_id') is not None:
# Initialize fabric.topology.interfaces.vpc_interfaces.<peer1>___<peer2>.<vpc_id>.<switch_name>
model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1')+"___"+vpc_peer.get('peer2')] = model_data['fabric']['topology']['interfaces']['vpc_interfaces'].get(vpc_peer.get('peer1')+"___"+vpc_peer.get('peer2'), {})
model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1')+"___"+vpc_peer.get('peer2')][interface.get('vpc_id')] = model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1')+"___"+vpc_peer.get('peer2')].get(interface.get('vpc_id'), {})
model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1')+"___"+vpc_peer.get('peer2')][interface.get('vpc_id')][switch.get('name')] = model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1')+"___"+vpc_peer.get('peer2')][interface.get('vpc_id')].get(switch.get('name'), {})
model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')] = model_data['fabric']['topology']['interfaces']['vpc_interfaces'].get(vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2'), {}) # noqa: E501
model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')][interface.get('vpc_id')] = model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')].get(interface.get('vpc_id'), {}) # noqa: E501
model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')][interface.get('vpc_id')][switch.get('name')] = model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')][interface.get('vpc_id')].get(switch.get('name'), {}) # noqa: E501
# Assign interface to fabric.topology.interfaces.vpc_interfaces.<peer1>___<peer2>.<vpc_id>.<switch_name>
model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1')+"___"+vpc_peer.get('peer2')][interface.get('vpc_id')][switch.get('name')] = interface
# Update model_extended with updated model_data
model_data['fabric']['topology']['interfaces']['vpc_interfaces'][vpc_peer.get('peer1') + "___" + vpc_peer.get('peer2')][interface.get('vpc_id')][switch.get('name')] = interface # noqa: E501
# Update model_extended with updated model_data
self.kwargs['results']['model_extended'] = model_data
return self.kwargs['results']

# ========================================================
# Sample Input
# ========================================================
Expand Down
7 changes: 2 additions & 5 deletions plugins/action/common/prepare_service_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@

__metaclass__ = type

from ansible import constants as C
from ansible.utils.display import Display
from ansible.plugins.action import ActionBase

from ..helper_functions import do_something

import importlib
import os
import pathlib
import copy
from pprint import pprint

display = Display()


class ActionModule(ActionBase):

def run(self, tmp=None, task_vars=None):
Expand All @@ -39,7 +36,7 @@ def run(self, tmp=None, task_vars=None):
glob_plugin_path = os.path.dirname(__file__) + "/prepare_plugins"
plugin_prefix = "*_prep*.py"

prepare_libs = set(_.stem for _ in pathlib.Path.glob(pathlib.Path(glob_plugin_path), plugin_prefix))
prepare_libs = set(x.stem for x in pathlib.Path.glob(pathlib.Path(glob_plugin_path), plugin_prefix))
dict_of_plugins = {}
for lib in prepare_libs:
plugin_name = f"{full_plugin_path}.{lib}"
Expand Down
10 changes: 3 additions & 7 deletions plugins/action/dtc/add_device_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@

__metaclass__ = type

from ansible import constants as C
from ansible.utils.display import Display
from ansible.plugins.action import ActionBase

from ..helper_functions import do_something

from pprint import pprint

display = Display()


class ActionModule(ActionBase):

def run(self, tmp=None, task_vars=None):
Expand All @@ -32,7 +28,7 @@ def run(self, tmp=None, task_vars=None):
for key in ['management', 'role']:
if switch.get(key) is None:
results['failed'] = True
results['msg'] = "Data model path 'fabric.topology.switches.{0}.{1}' must be defined!".format(switch['name'],key)
results['msg'] = "Data model path 'fabric.topology.switches.{0}.{1}' must be defined!".format(switch['name'], key)
return results

return results
return results
Loading

0 comments on commit 66857f7

Please sign in to comment.