How to create a task dict from a templated variable

631 views
Skip to first unread message

Gianni Stubbe

unread,
Apr 13, 2021, 10:52:06 AM4/13/21
to Ansible Project

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

Stefan Hornburg (Racke)

unread,
Apr 13, 2021, 12:34:04 PM4/13/21
to ansible...@googlegroups.com
On 4/13/21 4:52 PM, Gianni Stubbe wrote:
>
> 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
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/18445f1f-330d-42a4-9861-e79ca8fc856cn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/18445f1f-330d-42a4-9861-e79ca8fc856cn%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Gianni Stubbe

unread,
Apr 27, 2021, 5:31:27 AM4/27/21
to Ansible Project
Thanks for your reply, that works perfectly!
Reply all
Reply to author
Forward
0 new messages