How to create a task dict from a templated variable

Hello,

I have a variable file that defines the config state of my network hardware. As I would need to create lots of if and loops in loops I would like to be able to template the modules so that the amount of sessions I open to the device stays low.

I currently have this but this does not work as I get the error:
FAILED! => {“ansible_facts”: {“discovered_interpreter_python”: “/usr/bin/python”}, “changed”: false, “msg”: “Failure when processing no_log parameters. Module invocation will be hidden. dictionary requested, could not parse JSON or key=value”}

  • name: L2 - Config Arista
    block:

  • set_fact:
    physical_interface_config: |
    {% for L2_phys in L2.physical %}

  • name: {{ L2_phys.name }}
    {% if L2_phys.description is defined and L2_phys.description != “” %}
    description: “{{ L2_phys.description }}”
    {% endif %}
    {% if L2_phys.mtu is defined %}
    mtu: {{ L2_phys.mtu }}
    {% endif %}
    {% if L2_phys.autoneg is defined and L2_phys.autoneg %}
    duplex: auto
    {% endif %}
    {% if L2_phys.link_speed is defined %}
    speed: {{ L2_phys.link_speed }}
    {% endif %}
    {% if L2_phys.vid is not defined %}
    mode: layer3
    {% else %}
    mode: layer2
    {% endif %}
    {% endfor %}
    L2_config: |
    {% for L2_phys in L2.physical %}

  • name: {{ L2_phys.name }}
    {%- if L2_phys.vid is defined %}
    {%- if L2_phys.vid.untagged is defined %}
    untagged:
    vlan: {{ L2_phys.vid.untagged }}
    {%- endif %}
    {%- if L2_phys.vid.tagged is defined %}
    trunk:
    vlan: {{ L2_phys.vid.tagged }}
    {%- if L2_phys.vid.pvid is defined %}
    native_vlan: {{ L2_phys.vid.pvid }}
    {%- endif %}
    {%- endif %}
    {%- endif %}
    {%- endfor %}

  • name: L2 - Configure physical interfaces
    arista.eos.eos_interfaces:
    config: “{{ physical_interface_config }}”
    state: replaced

  • name: L2 - Configure L2 interfaces
    arista.eos.eos_l2_interfaces:
    config:
    “{{ L2_config }}”
    state: replaced

Hello,

I have a variable file that defines the config state of my network hardware. As I would need to create lots of if and
loops in loops I would like to be able to template the modules so that the amount of sessions I open to the device stays
low.

I currently have this but this does not work as I get the error:
FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "Failure
when processing no_log parameters. Module invocation will be hidden. dictionary requested, could not parse JSON or
key=value"}

The module expects a list in the config parameter, but you are passing a string with contains (hopefully valid) YAML.

Using

   config: "{{ physical_interface_config | from_yaml }}"

might bring you closer to the solution.

Regards
         Racke

- name: L2 - Config Arista
block:
- set_fact:
physical_interface_config: |
{% for L2_phys in L2.physical %}
- name: {{ L2_phys.name }}
{% if L2_phys.description is defined and L2_phys.description != "" %}
description: "{{ L2_phys.description }}"
{% endif %}
{% if L2_phys.mtu is defined %}
mtu:

{{ L2_phys.mtu }}

Thanks for your reply, that works perfectly!