From 628fe4cf97ce36abba103c8159f237ea4f107c74 Mon Sep 17 00:00:00 2001 From: Matt Tarkington Date: Mon, 1 Apr 2024 22:01:38 -0400 Subject: [PATCH 1/4] initial commit of draf iac-test module --- plugins/modules/nac_iac_test.py | 63 +++++++++++++++++++++++ roles/test_ndfc_deploy/README.md | 38 ++++++++++++++ roles/test_ndfc_deploy/defaults/main.yml | 2 + roles/test_ndfc_deploy/files/.gitkeep | 0 roles/test_ndfc_deploy/handlers/main.yml | 2 + roles/test_ndfc_deploy/meta/main.yml | 52 +++++++++++++++++++ roles/test_ndfc_deploy/tasks/main.yml | 14 +++++ roles/test_ndfc_deploy/templates/.gitkeep | 0 roles/test_ndfc_deploy/tests/inventory | 2 + roles/test_ndfc_deploy/tests/test.yml | 5 ++ roles/test_ndfc_deploy/vars/main.yml | 2 + 11 files changed, 180 insertions(+) create mode 100644 plugins/modules/nac_iac_test.py create mode 100644 roles/test_ndfc_deploy/README.md create mode 100644 roles/test_ndfc_deploy/defaults/main.yml create mode 100644 roles/test_ndfc_deploy/files/.gitkeep create mode 100644 roles/test_ndfc_deploy/handlers/main.yml create mode 100644 roles/test_ndfc_deploy/meta/main.yml create mode 100644 roles/test_ndfc_deploy/tasks/main.yml create mode 100644 roles/test_ndfc_deploy/templates/.gitkeep create mode 100644 roles/test_ndfc_deploy/tests/inventory create mode 100644 roles/test_ndfc_deploy/tests/test.yml create mode 100644 roles/test_ndfc_deploy/vars/main.yml diff --git a/plugins/modules/nac_iac_test.py b/plugins/modules/nac_iac_test.py new file mode 100644 index 00000000..5e6aac9a --- /dev/null +++ b/plugins/modules/nac_iac_test.py @@ -0,0 +1,63 @@ +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +ANSIBLE_METADATA = {"metadata_version": "0.1", "status": ["preview"]} + +import os + +from ansible.module_utils.basic import AnsibleModule + + +def run_module(): + module_args = dict( + data_dir=dict(type="str", required=False), + template_dir=dict(type="str", required=False), + output_dir=dict(type="str", required=False), + ) + + result = dict(changed=False) + + module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) + + data_dir = module.params["data_dir"] + template_dir = module.params["template_dir"] + output_dir = module.params["output_dir"] + + if output_dir and not os.path.exists(output_dir): + module.fail_json( + msg="The provided directory (results_dir) does not appear to exist. Is it a directory?" + ) + + options = [] + + if data_dir: + options.append("--data") + options.append(data_dir) + if template_dir: + options.append("--templates") + options.append(template_dir) + if output_dir: + options.append("--output") + options.append(output_dir) + + command = ["iac-test"] + command.extend(options) + + rc, _, _ = module.run_command(command) + + if rc > 0 and rc < 251: + result["failed_tests"] = rc + + result["rc"] = rc + result["changed"] = True + + module.exit_json(**result) + + +def main(): + run_module() + + +if __name__ == "__main__": + main() diff --git a/roles/test_ndfc_deploy/README.md b/roles/test_ndfc_deploy/README.md new file mode 100644 index 00000000..225dd44b --- /dev/null +++ b/roles/test_ndfc_deploy/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/roles/test_ndfc_deploy/defaults/main.yml b/roles/test_ndfc_deploy/defaults/main.yml new file mode 100644 index 00000000..e33d54d3 --- /dev/null +++ b/roles/test_ndfc_deploy/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# defaults file for test_ndfc_deploy diff --git a/roles/test_ndfc_deploy/files/.gitkeep b/roles/test_ndfc_deploy/files/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/roles/test_ndfc_deploy/handlers/main.yml b/roles/test_ndfc_deploy/handlers/main.yml new file mode 100644 index 00000000..27e18c48 --- /dev/null +++ b/roles/test_ndfc_deploy/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for test_ndfc_deploy diff --git a/roles/test_ndfc_deploy/meta/main.yml b/roles/test_ndfc_deploy/meta/main.yml new file mode 100644 index 00000000..c572acc9 --- /dev/null +++ b/roles/test_ndfc_deploy/meta/main.yml @@ -0,0 +1,52 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/roles/test_ndfc_deploy/tasks/main.yml b/roles/test_ndfc_deploy/tasks/main.yml new file mode 100644 index 00000000..7e8368ec --- /dev/null +++ b/roles/test_ndfc_deploy/tasks/main.yml @@ -0,0 +1,14 @@ +--- +# tasks file for test_ndfc_deploy + +# - name: Render NDFC Robot Resource File +# cisco.nac_dc_vxlan.template_local: +# src: 'ndfc_common.robot' +# dest: "" +# lstrip_blocks: yes + +- name: Run Tests + cisco.nac_dc_vxlan.nac_iac_test: + data_dir: "{{ playbook_dir }}/host_vars/{{ MD.fabric.global.name }}" + template_dir: "{{ role_path }}/templates" + output_dir: "{{ playbook_dir }}/tests" diff --git a/roles/test_ndfc_deploy/templates/.gitkeep b/roles/test_ndfc_deploy/templates/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/roles/test_ndfc_deploy/tests/inventory b/roles/test_ndfc_deploy/tests/inventory new file mode 100644 index 00000000..878877b0 --- /dev/null +++ b/roles/test_ndfc_deploy/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/roles/test_ndfc_deploy/tests/test.yml b/roles/test_ndfc_deploy/tests/test.yml new file mode 100644 index 00000000..5a59bf74 --- /dev/null +++ b/roles/test_ndfc_deploy/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - test_ndfc_deploy diff --git a/roles/test_ndfc_deploy/vars/main.yml b/roles/test_ndfc_deploy/vars/main.yml new file mode 100644 index 00000000..fafd100a --- /dev/null +++ b/roles/test_ndfc_deploy/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for test_ndfc_deploy From 26376d51dc8348b8d5ef04991ab135fd76ea9201 Mon Sep 17 00:00:00 2001 From: Matt Tarkington Date: Sun, 14 Apr 2024 21:22:59 -0400 Subject: [PATCH 2/4] stash different implementations --- plugins/action/common/nac_iac_test.py | 43 ++++++++++++++ plugins/modules/iac_test.py | 86 +++++++++++++++++++++++++++ plugins/modules/nac_iac_test.py | 63 -------------------- roles/test_ndfc_deploy/tasks/main.yml | 16 +++-- 4 files changed, 141 insertions(+), 67 deletions(-) create mode 100644 plugins/action/common/nac_iac_test.py create mode 100644 plugins/modules/iac_test.py delete mode 100644 plugins/modules/nac_iac_test.py diff --git a/plugins/action/common/nac_iac_test.py b/plugins/action/common/nac_iac_test.py new file mode 100644 index 00000000..21b2e076 --- /dev/null +++ b/plugins/action/common/nac_iac_test.py @@ -0,0 +1,43 @@ +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +from ansible import constants as C +from ansible.utils.display import Display +from ansible.plugins.action import ActionBase + +import os +import iac_test.robot_writer +import iac_test.pabot + +display = Display() + +class ActionModule(ActionBase): + + def run(self, tmp=None, task_vars=None): + results = super(ActionModule, self).run(tmp, task_vars) + results['failed'] = False + results['msg'] = None + + data = self._task.args.get('data') + templates = self._task.args.get('templates') + filters = self._task.args.get('filters', "") + tests = self._task.args.get('tests', "") + output = self._task.args.get('output') + include = self._task.args.get('include', []) + exclude = self._task.args.get('exclude', []) + render_only = self._task.args.get('render_only', "") + dry_run = self._task.args.get('dry_run', "") + + if output and not os.path.exists(output): + display.warning(f"The provided directory {output} does not appear to exist. Is it a directory?") + + writer = iac_test.robot_writer.RobotWriter(data, filters, tests, include, exclude) + writer.write(templates, output) + if not render_only: + iac_test.pabot.run_pabot(output, include, exclude, dry_run) + + results["changed"] = True + + return results diff --git a/plugins/modules/iac_test.py b/plugins/modules/iac_test.py new file mode 100644 index 00000000..16baf3e6 --- /dev/null +++ b/plugins/modules/iac_test.py @@ -0,0 +1,86 @@ +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +ANSIBLE_METADATA = {"metadata_version": "0.1", "status": ["preview"]} + +import os + +from ansible.module_utils.basic import AnsibleModule + +import iac_test.robot_writer +import iac_test.pabot + + +def run_module(): + module_args = dict( + data=dict(type="list", required=True), + templates=dict(type="str", required=True), + filters=dict(type="str", default="", required=False), + tests=dict(type="str", default="", required=False), + output=dict(type="str", required=True), + include=dict(type="list", default=[], required=False), + exclude=dict(type="list", default=[], required=False), + render_only=dict(type="bool", default=False, required=False), + dry_run=dict(type="bool", default=False, required=False) + ) + + result = dict(changed=False) + + module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) + + # import epdb; epdb.set_trace() + + data = module.params["data"] + templates = module.params["templates"] + filters = module.params["filters"] + tests = module.params["tests"] + output = module.params["output"] + include = module.params["include"] + exclude = module.params["exclude"] + render_only = module.params["render_only"] + dry_run = module.params["dry_run"] + + if output and not os.path.exists(output): + module.fail_json( + msg=f"The provided directory {output} does not appear to exist. Is it a directory?" + ) + + # writer = iac_test.robot_writer.RobotWriter(data, filters, tests, include, exclude) + # writer.write(templates, output) + # # iac_test.pabot.run_pabot(output, include, exclude, dry_run) + # if not render_only: + # iac_test.pabot.run_pabot(output, include, exclude, dry_run) + + options = [] + + if data: + options.append("--data") + options.append(data[0]) + if templates: + options.append("--templates") + options.append(templates) + if output: + options.append("--output") + options.append(output) + + command = ["iac-test"] + command.extend(options) + + rc, _, _ = module.run_command(command) + + if rc > 0 and rc < 251: + result["failed_tests"] = rc + + result["rc"] = rc + result["changed"] = True + + module.exit_json(**result) + + +def main(): + run_module() + + +if __name__ == "__main__": + main() diff --git a/plugins/modules/nac_iac_test.py b/plugins/modules/nac_iac_test.py deleted file mode 100644 index 5e6aac9a..00000000 --- a/plugins/modules/nac_iac_test.py +++ /dev/null @@ -1,63 +0,0 @@ -from __future__ import absolute_import, division, print_function - -__metaclass__ = type - -ANSIBLE_METADATA = {"metadata_version": "0.1", "status": ["preview"]} - -import os - -from ansible.module_utils.basic import AnsibleModule - - -def run_module(): - module_args = dict( - data_dir=dict(type="str", required=False), - template_dir=dict(type="str", required=False), - output_dir=dict(type="str", required=False), - ) - - result = dict(changed=False) - - module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) - - data_dir = module.params["data_dir"] - template_dir = module.params["template_dir"] - output_dir = module.params["output_dir"] - - if output_dir and not os.path.exists(output_dir): - module.fail_json( - msg="The provided directory (results_dir) does not appear to exist. Is it a directory?" - ) - - options = [] - - if data_dir: - options.append("--data") - options.append(data_dir) - if template_dir: - options.append("--templates") - options.append(template_dir) - if output_dir: - options.append("--output") - options.append(output_dir) - - command = ["iac-test"] - command.extend(options) - - rc, _, _ = module.run_command(command) - - if rc > 0 and rc < 251: - result["failed_tests"] = rc - - result["rc"] = rc - result["changed"] = True - - module.exit_json(**result) - - -def main(): - run_module() - - -if __name__ == "__main__": - main() diff --git a/roles/test_ndfc_deploy/tasks/main.yml b/roles/test_ndfc_deploy/tasks/main.yml index 7e8368ec..72762b48 100644 --- a/roles/test_ndfc_deploy/tasks/main.yml +++ b/roles/test_ndfc_deploy/tasks/main.yml @@ -7,8 +7,16 @@ # dest: "" # lstrip_blocks: yes +- name: Create Output Directory + ansible.builtin.file: + path: "{{ playbook_dir }}/output/" + state: directory + mode: '0755' + - name: Run Tests - cisco.nac_dc_vxlan.nac_iac_test: - data_dir: "{{ playbook_dir }}/host_vars/{{ MD.fabric.global.name }}" - template_dir: "{{ role_path }}/templates" - output_dir: "{{ playbook_dir }}/tests" + # cisco.nac_dc_vxlan.common.nac_iac_test: + cisco.nac_dc_vxlan.iac_test: + data: + - "{{ playbook_dir }}/host_vars/{{ MD.fabric.global.name }}" + templates: "{{ role_path }}/templates" + output: "{{ playbook_dir }}/output" From 172e1a7ae0902b072914efe472f188500d359cc7 Mon Sep 17 00:00:00 2001 From: Matt Tarkington Date: Tue, 16 Apr 2024 21:01:24 -0400 Subject: [PATCH 3/4] update module with dir checks --- plugins/modules/iac_test.py | 63 +++++++++++++++++++++------ roles/test_ndfc_deploy/tasks/main.yml | 1 + 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/plugins/modules/iac_test.py b/plugins/modules/iac_test.py index 16baf3e6..033330a9 100644 --- a/plugins/modules/iac_test.py +++ b/plugins/modules/iac_test.py @@ -11,6 +11,9 @@ import iac_test.robot_writer import iac_test.pabot +import sys +from io import StringIO + def run_module(): module_args = dict( @@ -41,31 +44,63 @@ def run_module(): render_only = module.params["render_only"] dry_run = module.params["dry_run"] - if output and not os.path.exists(output): - module.fail_json( - msg=f"The provided directory {output} does not appear to exist. Is it a directory?" - ) + paths = data + paths.append(templates) + if filters: + paths.append(filters) + if tests: + paths.append(tests) + + for path in paths: + if not os.path.exists(path): + module.fail_json(msg=f"The provided directory {path} does not appear to exist!") + + for path in paths: + if not os.listdir(path): + module.fail_json(msg=f"The provided directory {path} exists but appears to be empty!") + + #### Option #1 #### + #### iac-test class object & methods #### # writer = iac_test.robot_writer.RobotWriter(data, filters, tests, include, exclude) # writer.write(templates, output) - # # iac_test.pabot.run_pabot(output, include, exclude, dry_run) # if not render_only: - # iac_test.pabot.run_pabot(output, include, exclude, dry_run) + # iac_test.pabot.run_pabot(outpiac_testut, include, exclude, dry_run) + + #### Option #2 #### + #### iac-test cmd line wrapper #### options = [] - if data: + for item in data: options.append("--data") - options.append(data[0]) - if templates: - options.append("--templates") - options.append(templates) - if output: - options.append("--output") - options.append(output) + options.append(item) + options.append("--templates") + options.append(templates) + if filters: + options.append("--filters") + options.append(filters) + if tests: + options.append("--tests") + options.append(tests) + options.append("--output") + options.append(output) + if include: + for item in include: + options.append("--include") + options.append(item) + if exclude: + for item in exclude: + options.append("--exclude") + options.append(item) + if render_only: + options.append("--render-only") + if dry_run: + options.append("--dry-run") command = ["iac-test"] command.extend(options) + result["command"] = command rc, _, _ = module.run_command(command) diff --git a/roles/test_ndfc_deploy/tasks/main.yml b/roles/test_ndfc_deploy/tasks/main.yml index 72762b48..196cd080 100644 --- a/roles/test_ndfc_deploy/tasks/main.yml +++ b/roles/test_ndfc_deploy/tasks/main.yml @@ -20,3 +20,4 @@ - "{{ playbook_dir }}/host_vars/{{ MD.fabric.global.name }}" templates: "{{ role_path }}/templates" output: "{{ playbook_dir }}/output" + delegate_to: localhost From 49d5415ea8b5c6115408d4d84885e4b223d43400 Mon Sep 17 00:00:00 2001 From: Mike Wiebe Date: Wed, 17 Apr 2024 12:05:46 -0400 Subject: [PATCH 4/4] Update main.yml Force new actions to run --- roles/test_ndfc_deploy/tasks/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/roles/test_ndfc_deploy/tasks/main.yml b/roles/test_ndfc_deploy/tasks/main.yml index 196cd080..c2f5074a 100644 --- a/roles/test_ndfc_deploy/tasks/main.yml +++ b/roles/test_ndfc_deploy/tasks/main.yml @@ -1,6 +1,8 @@ --- # tasks file for test_ndfc_deploy +# Force Actions To Run + # - name: Render NDFC Robot Resource File # cisco.nac_dc_vxlan.template_local: # src: 'ndfc_common.robot'