Skip to content

pyavd

Warning

pyavd is still in the development phase. Please do not use.

Installation

pip3 install pyavd

Reference

validate_inputs(all_hostvars, eos_designs=True, eos_cli_config_gen=True)

Validate input variables according to AVD the eos_designs schema and/or the eos_cli_config_gen schema. as documented on avd.arista.com.

Where supported by the schema, types will be auto type-converted like from “int” to “str”.

Parameters:

Name Type Description Default
all_hostvars dict[str, dict]

A dictionary where keys are hostnames and values are dictionaries of all variables per devices.

{
    "<hostname1>": dict,
    "<hostname2>": dict,
    ...
}

required
eos_designs bool

Validate variables according to the eos_designs schema.

True
eos_cli_config_gen bool

Validate variables according to the eos_cli_config_gen schema.

True

Raises:

Type Description
AristaAvdError

List of validation errors across all devices.

Source code in python-avd/pyavd/validate_inputs.py
def validate_inputs(
    all_hostvars: dict[str, dict],
    eos_designs: bool = True,
    eos_cli_config_gen: bool = True,
) -> None:
    """
    Validate input variables according to AVD the `eos_designs` schema and/or the `eos_cli_config_gen` schema.
    as documented on avd.arista.com.

    Where supported by the schema, types will be auto type-converted like from "int" to "str".

    Args:
        all_hostvars: A dictionary where keys are hostnames and values are dictionaries of all variables per devices.
            ```python
            {
                "<hostname1>": dict,
                "<hostname2>": dict,
                ...
            }
            ```
        eos_designs: Validate variables according to the `eos_designs` schema.
        eos_cli_config_gen: Validate variables according to the `eos_cli_config_gen` schema.

    Raises:
        AristaAvdError: List of validation errors across all devices.
    """
    if eos_designs:
        eos_designs_schema_tools = AvdSchemaTools(schema_id=EOS_DESIGNS_SCHEMA_ID)

    if eos_cli_config_gen:
        eos_cli_config_gen_schema_tools = AvdSchemaTools(schema_id=EOS_CLI_CONFIG_GEN_SCHEMA_ID)

    error_messages = []
    for hostname, hostvars in all_hostvars.items():
        if eos_designs:
            # Initialize SharedUtils class to fetch default variables below.
            shared_utils = SharedUtils(hostvars=hostvars, templar=None)

            # Insert dynamic keys into the input data if not set.
            # These keys are required by the schema, but the default values are set inside shared_utils.
            hostvars.setdefault("node_type_keys", shared_utils.node_type_keys)
            hostvars.setdefault("connected_endpoints_keys", shared_utils.connected_endpoints_keys)
            hostvars.setdefault("network_services_keys", shared_utils.network_services_keys)

            # Validate input data
            result = eos_designs_schema_tools.convert_and_validate_data(hostvars)
            if result.get("failed"):
                error_messages.extend([f"[{hostname}]: {str(error)}" for error in result["errors"]])

        if eos_cli_config_gen:
            # Validate input data
            result = eos_cli_config_gen_schema_tools.convert_and_validate_data(hostvars)
            if result.get("failed"):
                error_messages.extend([f"[{hostname}]: {str(error)}" for error in result["errors"]])

    if error_messages:
        raise AristaAvdError(f"{error_messages}")

    return

get_avd_facts(all_hostvars)

Build avd_facts using the AVD eos_designs_facts logic.

Variables should be converted and validated according to AVD eos_designs schema first using pyavd.validate_inputs.

Note! No support for inline templating or jinja templates for descriptions or ip addressing

Parameters:

Name Type Description Default
all_hostvars dict[str, dict]

A dictionary where keys are hostnames and values are dictionaries of all variables per devices.

{
    "<hostname1>": dict,
    "<hostname2>": dict,
    ...
}

required

Returns:

Type Description
dict[str, dict]

Nested dictionary with various internal “facts”. The full dict must be given as argument to pyavd.get_device_structured_config:

{
    "avd_switch_facts": dict,
    "avd_overlay_peers": dict,
    "avd_topology_peers" : dict
}

Source code in python-avd/pyavd/get_avd_facts.py
def get_avd_facts(all_hostvars: dict[str, dict]) -> dict[str, dict]:
    """
    Build avd_facts using the AVD eos_designs_facts logic.

    Variables should be converted and validated according to AVD `eos_designs` schema first using `pyavd.validate_inputs`.

    Note! No support for inline templating or jinja templates for descriptions or ip addressing

    Args:
        all_hostvars: A dictionary where keys are hostnames and values are dictionaries of all variables per devices.
            ```python
            {
                "<hostname1>": dict,
                "<hostname2>": dict,
                ...
            }
            ```

    Returns:
        Nested dictionary with various internal "facts". The full dict must be given as argument to `pyavd.get_device_structured_config`:
            ```python
            {
                "avd_switch_facts": dict,
                "avd_overlay_peers": dict,
                "avd_topology_peers" : dict
            }
            ```
    """

    avd_switch_facts_instances = _create_avd_switch_facts_instances(all_hostvars)
    avd_switch_facts = _render_avd_switch_facts(avd_switch_facts_instances)
    avd_overlay_peers, avd_topology_peers = _render_peer_facts(avd_switch_facts)

    return {
        "avd_switch_facts": avd_switch_facts,
        "avd_overlay_peers": avd_overlay_peers,
        "avd_topology_peers": avd_topology_peers,
    }

get_device_structured_config(hostname, hostvars, avd_facts)

Build and return the AVD structured configuration for one device.

Parameters:

Name Type Description Default
hostname str

Hostname of device.

required
hostvars dict

Dictionary of variables passed to AVD eos_designs modules. Variables should be converted and validated according to AVD eos_designs schema first using pyavd.validate_inputs.

required
avd_facts dict

Dictionary of avd_facts as returned from pyavd.get_avd_facts.

required

Returns:

Type Description
dict

Device Structured Configuration as a dictionary

Source code in python-avd/pyavd/get_device_structured_config.py
def get_device_structured_config(hostname: str, hostvars: dict, avd_facts: dict) -> dict:
    """
    Build and return the AVD structured configuration for one device.

    Args:
        hostname: Hostname of device.
        hostvars: Dictionary of variables passed to AVD `eos_designs` modules.
            Variables should be converted and validated according to AVD `eos_designs` schema first using `pyavd.validate_inputs`.
        avd_facts: Dictionary of avd_facts as returned from `pyavd.get_avd_facts`.

    Returns:
        Device Structured Configuration as a dictionary
    """

    # Set 'inventory_hostname' on the input hostvars, to keep compatability with Ansible focused code.
    # Also map in avd_facts without touching the hostvars
    mapped_hostvars = ChainMap(
        {
            "inventory_hostname": hostname,
            "switch": avd_facts["avd_switch_facts"][hostname]["switch"],
        },
        avd_facts,
        hostvars,
    )

    # We do not validate input variables in this stage (done in "validate_inputs")
    # So we feed the vendored code an empty schema to avoid failures.
    input_schema_tools = AvdSchemaTools(schema={})
    output_schema_tools = AvdSchemaTools(schema_id=EOS_CLI_CONFIG_GEN_SCHEMA_ID)
    result = {}
    structured_config = get_structured_config(
        vars=mapped_hostvars,
        input_schema_tools=input_schema_tools,
        output_schema_tools=output_schema_tools,
        result=result,
        templar=None,
    )
    if result.get("failed"):
        raise AristaAvdError(f"{[str(error) for error in result['errors']]}")

    return structured_config

get_device_config(hostname, hostvars)

Render and return the device configuration using AVD eos_cli_config_gen templates.

Parameters:

Name Type Description Default
hostname str

Hostname of device.

required
hostvars dict

Dictionary of variables applied to template. Variables should be converted and validated according to AVD eos_cli_config_gen schema first using pyavd.validate_inputs.

required

Returns:

Type Description
str

Device configuration in EOS CLI format.

Source code in python-avd/pyavd/get_device_config.py
def get_device_config(hostname: str, hostvars: dict) -> str:
    """
    Render and return the device configuration using AVD eos_cli_config_gen templates.

    Args:
        hostname: Hostname of device.
        hostvars: Dictionary of variables applied to template.
            Variables should be converted and validated according to AVD `eos_cli_config_gen` schema first using `pyavd.validate_inputs`.

    Returns:
        Device configuration in EOS CLI format.
    """

    # Set 'inventory_hostname' on the input hostvars, to keep compatability with Ansible focused code.
    mapped_vars = ChainMap({"inventory_hostname": hostname}, hostvars)

    templar = Templar()
    return templar.render_template_from_file(JINJA2_CONFIG_TEMPLATE, mapped_vars)

get_device_doc(hostname, hostvars)

Render and return the device documentation using AVD eos_cli_config_gen templates.

Parameters:

Name Type Description Default
hostname str

Hostname of device.

required
hostvars dict

Dictionary of variables applied to template. Variables should be converted and validated according to AVD eos_cli_config_gen schema first using pyavd.validate_inputs.

required

Returns:

Type Description
str

Device documentation in Markdown format.

Source code in python-avd/pyavd/get_device_doc.py
def get_device_doc(
    hostname: str,
    hostvars: dict,
) -> str:
    """
    Render and return the device documentation using AVD eos_cli_config_gen templates.

    Args:
        hostname: Hostname of device.
        hostvars: Dictionary of variables applied to template.
            Variables should be converted and validated according to AVD `eos_cli_config_gen` schema first using `pyavd.validate_inputs`.

    Returns:
        Device documentation in Markdown format.
    """

    # Set 'inventory_hostname' on the input hostvars, to keep compatability with Ansible focused code.
    mapped_hostvars = ChainMap({"inventory_hostname": hostname}, hostvars)

    templar = Templar()
    return templar.render_template_from_file(JINJA2_DOCUMENTAITON_TEMPLATE, mapped_hostvars)

Last update: June 16, 2023