From 81f190236b5153ccf9b731cb7647c8268d9bd426 Mon Sep 17 00:00:00 2001 From: Claus Holbech Date: Fri, 20 Dec 2024 16:43:26 +0100 Subject: [PATCH] Fix(cv_deploy): Ignore missing structured config files (#4836) --- .../Modules_and_action_plugins/cv_workflow.md | 2 +- .../base_configs/avd-ci-core1.yml | 2 - .../base_configs/avd-ci-leaf2.yml | 2 - .../base_configs/avd-ci-leaf3.yml | 2 - .../base_configs/avd-ci-leaf4.yml | 2 - .../base_configs/avd-ci-spine1.yml | 2 - .../base_configs/avd-ci-spine2.yml | 2 - .../arista/avd/plugins/action/cv_workflow.py | 46 ++++++++++--------- .../arista/avd/plugins/modules/cv_workflow.py | 2 +- 9 files changed, 27 insertions(+), 35 deletions(-) delete mode 100644 ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-core1.yml delete mode 100644 ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf2.yml delete mode 100644 ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf3.yml delete mode 100644 ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf4.yml delete mode 100644 ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-spine1.yml delete mode 100644 ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-spine2.yml diff --git a/ansible_collections/arista/avd/docs/plugins/Modules_and_action_plugins/cv_workflow.md b/ansible_collections/arista/avd/docs/plugins/Modules_and_action_plugins/cv_workflow.md index 54f86983848..49b5cd2493e 100644 --- a/ansible_collections/arista/avd/docs/plugins/Modules_and_action_plugins/cv_workflow.md +++ b/ansible_collections/arista/avd/docs/plugins/Modules_and_action_plugins/cv_workflow.md @@ -35,7 +35,7 @@ The `arista.avd.cv_workflow` module is an Ansible Action Plugin providing the fo | cv_token | str | True | None | | Service account token. It is strongly recommended to use Vault for this. | | cv_verify_certs | bool | optional | True | | Verifies CloudVison server certificates. | | configuration_dir | str | True | None | | Path to directory containing .cfg files with EOS configurations. | -| structured_config_dir | str | True | None | | Path to directory containing files with AVD structured configurations.
If found, the `serial_number` or `system_mac_address` will be used to identify the Device on CloudVision.
Any tags found in the structured configuration metadata will be applied to the Device and/or Interfaces. | +| structured_config_dir | str | False | None | | Path to directory containing files with AVD structured configurations.
If found, the `serial_number` or `system_mac_address` will be used to identify the Device on CloudVision.
Any tags found in the structured configuration metadata will be applied to the Device and/or Interfaces. | | structured_config_suffix | str | optional | yml | | File suffix for AVD structured configuration files. | | device_list | list | True | None | | List of devices to deploy. The names are used to find AVD structured configuration and EOS configuration files. | | strict_tags | bool | optional | False | | If `true` other tags associated with the devices will get removed. Otherwise other tags will be left as-is. | diff --git a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-core1.yml b/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-core1.yml deleted file mode 100644 index eab10aaf23a..00000000000 --- a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-core1.yml +++ /dev/null @@ -1,2 +0,0 @@ -hostname: avd-ci-core1 -is_deployed: true diff --git a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf2.yml b/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf2.yml deleted file mode 100644 index 9aab991be40..00000000000 --- a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf2.yml +++ /dev/null @@ -1,2 +0,0 @@ -hostname: avd-ci-leaf2 -is_deployed: true diff --git a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf3.yml b/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf3.yml deleted file mode 100644 index c3c2ad034ae..00000000000 --- a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf3.yml +++ /dev/null @@ -1,2 +0,0 @@ -hostname: avd-ci-leaf3 -is_deployed: true diff --git a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf4.yml b/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf4.yml deleted file mode 100644 index 80bb7b4a1f2..00000000000 --- a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-leaf4.yml +++ /dev/null @@ -1,2 +0,0 @@ -hostname: avd-ci-leaf4 -is_deployed: true diff --git a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-spine1.yml b/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-spine1.yml deleted file mode 100644 index 7e046c53734..00000000000 --- a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-spine1.yml +++ /dev/null @@ -1,2 +0,0 @@ -hostname: avd-ci-spine1 -is_deployed: true diff --git a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-spine2.yml b/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-spine2.yml deleted file mode 100644 index df9afdae83a..00000000000 --- a/ansible_collections/arista/avd/molecule/cv_deploy/intended/structured_configs/base_configs/avd-ci-spine2.yml +++ /dev/null @@ -1,2 +0,0 @@ -hostname: avd-ci-spine2 -is_deployed: true diff --git a/ansible_collections/arista/avd/plugins/action/cv_workflow.py b/ansible_collections/arista/avd/plugins/action/cv_workflow.py index ed4b37ccb74..410e2fe9ce6 100644 --- a/ansible_collections/arista/avd/plugins/action/cv_workflow.py +++ b/ansible_collections/arista/avd/plugins/action/cv_workflow.py @@ -44,7 +44,7 @@ ARGUMENT_SPEC = { "configuration_dir": {"type": "str", "required": True}, - "structured_config_dir": {"type": "str", "required": True}, + "structured_config_dir": {"type": "str", "required": False}, "structured_config_suffix": {"type": "str", "default": "yml"}, "device_list": {"type": "list", "elements": "str", "required": True}, "strict_tags": {"type": "bool", "required": False, "default": False}, @@ -184,7 +184,7 @@ async def deploy(self, validated_args: dict, result: dict) -> dict: async def build_objects( self, device_list: list[str], - structured_config_dir: str, + structured_config_dir: str | None, structured_config_suffix: str, configuration_dir: str, configlet_name_template: str, @@ -228,7 +228,7 @@ async def build_objects( async def build_object_for_device( self, hostname: str, - structured_config_dir: str, + structured_config_dir: str | None, structured_config_suffix: str, configuration_dir: str, configlet_name_template: str, @@ -255,24 +255,28 @@ async def build_object_for_device( TODO: Refactor into smaller functions. """ LOGGER.info("build_object_for_device: %s", hostname) - with Path(structured_config_dir, f"{hostname}.{structured_config_suffix}").open( # noqa: ASYNC230 - mode="r", encoding="UTF-8" - ) as structured_config_stream: - if structured_config_suffix in ["yml", "yaml"]: - interesting_keys = ("is_deployed", "serial_number", "metadata") - in_interesting_context = False - structured_config_lines = [] - for line in structured_config_stream: - if line.startswith(interesting_keys) or (in_interesting_context and line.startswith(" ")): - structured_config_lines.append(line) - in_interesting_context = True - else: - in_interesting_context = False - - structured_config = load("".join(structured_config_lines), Loader=YamlLoader) # noqa: S506 TODO: Consider safeload - else: - # Load as JSON - structured_config = json.load(structured_config_stream) + if structured_config_dir and (file_path := Path(structured_config_dir, f"{hostname}.{structured_config_suffix}")).exists(): + with file_path.open( # noqa: ASYNC230 + mode="r", encoding="UTF-8" + ) as structured_config_stream: + if structured_config_suffix in ["yml", "yaml"]: + interesting_keys = ("is_deployed", "serial_number", "metadata") + in_interesting_context = False + structured_config_lines = [] + for line in structured_config_stream: + if line.startswith(interesting_keys) or (in_interesting_context and line.startswith(" ")): + structured_config_lines.append(line) + in_interesting_context = True + else: + in_interesting_context = False + + structured_config = load("".join(structured_config_lines), Loader=YamlLoader) # noqa: S506 TODO: Consider safeload + else: + # Load as JSON + structured_config = json.load(structured_config_stream) + else: + # No structured config file. + structured_config = {} if not get(structured_config, "is_deployed", default=True): del structured_config diff --git a/ansible_collections/arista/avd/plugins/modules/cv_workflow.py b/ansible_collections/arista/avd/plugins/modules/cv_workflow.py index 37a8165ef72..15078799b85 100644 --- a/ansible_collections/arista/avd/plugins/modules/cv_workflow.py +++ b/ansible_collections/arista/avd/plugins/modules/cv_workflow.py @@ -41,7 +41,7 @@ Path to directory containing files with AVD structured configurations. If found, the `serial_number` or `system_mac_address` will be used to identify the Device on CloudVision. Any tags found in the structured configuration metadata will be applied to the Device and/or Interfaces. - required: true + required: false type: str structured_config_suffix: description: File suffix for AVD structured configuration files.