# How to create a task dict from a templated variable

**URL:** <https://forum.ansible.com/t/how-to-create-a-task-dict-from-a-templated-variable/34973>\
**Category:** Ansible Project\
**Created:** [April 13, 2021, 2:52pm UTC](https://forum.ansible.com/t/how-to-create-a-task-dict-from-a-templated-variable/34973 "2021-04-13T14:52:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gianni\_Stubbe](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Gianni\_Stubbe](https://forum.ansible.com/u/Gianni_Stubbe)\
**Post date:** [April 13, 2021, 2:52pm UTC](https://forum.ansible.com/t/how-to-create-a-task-dict-from-a-templated-variable/34973/1 "2021-04-13T14:52:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [April 13, 2021, 4:33pm UTC](https://forum.ansible.com/t/how-to-create-a-task-dict-from-a-templated-variable/34973/2 "2021-04-13T16:33:33Z")

</div>

> 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

&nbsp;&nbsp;&nbsp;config: "{{ physical\_interface\_config | from\_yaml }}"

might bring you closer to the solution.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 }}

---

<div class="post-metadata">

**Author:** ![Gianni\_Stubbe](https://avatars.discourse-cdn.com/v4/letter/g/5daacb/32.png) [@Gianni\_Stubbe](https://forum.ansible.com/u/Gianni_Stubbe)\
**Post date:** [April 27, 2021, 9:31am UTC](https://forum.ansible.com/t/how-to-create-a-task-dict-from-a-templated-variable/34973/3 "2021-04-27T09:31:27Z")

</div>

Thanks for your reply, that works perfectly!
