Skip to content

Commit

Permalink
Fix(cv_deploy): Ignore missing structured config files (#4836)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClausHolbechArista authored Dec 20, 2024
1 parent 9e4bc28 commit 81f1902
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The `arista.avd.cv_workflow` module is an Ansible Action Plugin providing the fo
| <samp>cv_token</samp> | str | True | None | | Service account token. It is strongly recommended to use Vault for this. |
| <samp>cv_verify_certs</samp> | bool | optional | True | | Verifies CloudVison server certificates. |
| <samp>configuration_dir</samp> | str | True | None | | Path to directory containing .cfg files with EOS configurations. |
| <samp>structured_config_dir</samp> | str | True | None | | Path to directory containing files with AVD structured configurations.<br>If found, the `serial_number` or `system_mac_address` will be used to identify the Device on CloudVision.<br>Any tags found in the structured configuration metadata will be applied to the Device and/or Interfaces. |
| <samp>structured_config_dir</samp> | str | False | None | | Path to directory containing files with AVD structured configurations.<br>If found, the `serial_number` or `system_mac_address` will be used to identify the Device on CloudVision.<br>Any tags found in the structured configuration metadata will be applied to the Device and/or Interfaces. |
| <samp>structured_config_suffix</samp> | str | optional | yml | | File suffix for AVD structured configuration files. |
| <samp>device_list</samp> | list | True | None | | List of devices to deploy. The names are used to find AVD structured configuration and EOS configuration files. |
| <samp>strict_tags</samp> | bool | optional | False | | If `true` other tags associated with the devices will get removed. Otherwise other tags will be left as-is. |
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

46 changes: 25 additions & 21 deletions ansible_collections/arista/avd/plugins/action/cv_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 81f1902

Please sign in to comment.