Skip to content

Commit

Permalink
Merge custom defaults with factory defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
anramgop committed Oct 8, 2024
1 parent ed875ed commit 20e51b6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
63 changes: 63 additions & 0 deletions plugins/action/common/merge_defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright (c) 2024 Cisco Systems, Inc. and its affiliates
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# SPDX-License-Identifier: MIT

from __future__ import absolute_import, division, print_function


__metaclass__ = type

from ansible.utils.display import Display
from ansible.plugins.action import ActionBase

import importlib
import os
import pathlib
import copy

display = Display()


def merge_dicts(d1, d2):
for k, v in d2.items():
if k in d1 and isinstance(d1[k], dict) and isinstance(v, dict):
d1[k] = merge_dicts(d1[k], v)
else:
d1[k] = v
return d1

class ActionModule(ActionBase):

def run(self, tmp=None, task_vars=None):
# self._supports_async = True
results = super(ActionModule, self).run(tmp, task_vars)
results['failed'] = False
results['msg'] = None
results['defaults'] = None

# Get Data from Ansible Task
fac_def = self._task.args['factory_defaults']
cus_def = self._task.args['custom_defaults']

results['defaults'] = merge_dicts(fac_def, cus_def)
#import epdb; epdb.set_trace()

return results

Empty file removed roles/validate/defaults/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# SPDX-License-Identifier: MIT

---
defaults:
factory_defaults:
vxlan:
global:
route_reflectors: 2
Expand Down
31 changes: 25 additions & 6 deletions roles/validate/tasks/sub_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,32 @@
ansible.builtin.include_tasks: manage_model_files_current.yml
when: check_roles['save_previous']

- name: Stat Host Specific Defaults
ansible.builtin.stat: path="{{ role_path }}/defaults/{{ inventory_hostname }}/defaults.yml"
register: host_defaults
- name: Stat Factory Defaults
ansible.builtin.stat: path="{{ role_path }}/files/defaults.yml"
register: factory_defaults_file
delegate_to: localhost

- name: Include Host Specific Defaults if Available
- name: Include Factory Defaults if Available
ansible.builtin.include_vars:
file: "{{ role_path }}/defaults/{{ inventory_hostname }}/defaults.yml"
when: host_defaults.stat.exists
file: "{{ role_path }}/files/defaults.yml"
when: factory_defaults_file.stat.exists
delegate_to: localhost

- name: Stat Custom Specific Defaults
ansible.builtin.stat: path="{{ custom_defaults_path }}"
register: custom_defaults_file
delegate_to: localhost

- name: Include Custom Defaults if Available
ansible.builtin.include_vars:
file: "{{ custom_defaults_path }}"
when: custom_defaults_file.stat.exists
delegate_to: localhost

- name: Merge factory and custom defaults
when: custom_defaults_file.stat.exists
cisco.nac_dc_vxlan.common.merge_defaults:
factory_defaults: "{{ factory_defaults }}"
custom_defaults: "{{ defaults }}"
register: defaults
delegate_to: localhost

0 comments on commit 20e51b6

Please sign in to comment.