nbctcp
nbctcp

Reputation: 33

Ansible how to loop with_dict

I want to run these mikrotik commands using ansible

interface bridge port add bridge=bridge1 ingress-filtering=no interface=ether3
interface bridge port add bridge=bridge1 ingress-filtering=no interface=ether4

BUT NOT

interface bridge port add bridge=bridge1 ingress-filtering=no interface=ether3,ether4

my playbook run wrong command How to fix that tq

`# cat main.yml

- hosts: mikrotiks
  gather_facts: no
  connection: network_cli
  vars:
  ansible_network_os: routeros
  vlans:
  - 10:
    interfaces:
      - ether3
      - ether4
    untagged_interfaces: ether2
    tagged_interfaces: bridge1,ether3,ether4
    network: 10.0.10.0
    netmask: 24
    ip_address1: 10.0.10.1
    ip_address2: 10.0.10.2
    dns: 192.168.88.1
    dhcp_pool1: 10.0.10.101-10.0.10.150
    dhcp_pool2: 10.0.10.151-10.0.10.200
    leasetime: 1d
  - 20:
    interfaces:
      - ether3
      - ether4
    untagged_interfaces: ether2
    tagged_interfaces: bridge1,ether3,ether4
    network: 10.0.20.0
    netmask: 24
    ip_address1: 10.0.20.1
    ip_address2: 10.0.20.2
    dns: 192.168.88.1
    dhcp_pool1: 10.0.20.101-10.0.20.150
    dhcp_pool2: 10.0.20.151-10.0.20.200
    leasetime: 1d
    tasks:
  - import_tasks: vlan.yml

cat vlan.yml


- name: create vlans on R1
  routeros_command:
  commands:
    - "interface bridge port add bridge=bridge1 ingress-filtering=no interface={{ item.value.interfaces }}"
    - "interface bridge port add bridge=bridge1 frame-types=admit-only-untagged-and-priority-tagged pvid=10 interface={{ item.value.interfaces }}"
  with_dict: "{{ vlans }}"\`

EDIT: my mikrotik commands should be like this R1 /interface bridge port add bridge=bridge1 ingress-filtering=no interface=ether3 add bridge=bridge1 ingress-filtering=no interface=ether4 add bridge=bridge1 frame-types=admit-only-untagged-and-priority-tagged interface=ether2 pvid=10 /interface bridge vlan add bridge=bridge1 tagged=bridge1,ether3,ether4 untagged=ether2 vlan-ids=10 add bridge=bridge1 tagged=bridge1,ether3,ether4 vlan-ids=20

when I use these

\`routeros_command:
commands:
  - "/interface bridge port add bridge=bridge1 ingress-filtering=no interface={{ item.1 }}"
  - "/interface bridge port add bridge=bridge1 frame-types=admit-only-untagged-and-priority-tagged pvid=10 interface={{ item.1 }}"

with_subelements: - "{{ vlans }}" - interfaces`

I got this error `fatal: [R1]: FAILED! => { "msg": "could not find 'interfaces' key in iterated item '{10: {'interfaces': ['ether3', 'ether4'], 'untagged_interfaces': 'ether2', 'tagged_interfaces': 'bridge1,ether3,ether4', 'network': '10.0.10.0', 'netmask': 24, 'ip_address1': '10.0.10.1', 'ip_address2': '10.0.10.2', 'dns': '192.168.88.1', 'dhcp_pool1': '10.0.10.101-10.0.10.150', 'dhcp_pool2': '10.0.10.151-10.0.10.200', 'leasetime': '1d'}}'"`

Upvotes: 2

Views: 397

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68404

Iterate with_subelements, e.g.

    - debug:
        msg: "{{ item.0.keys()|first }} ... interface={{ item.1 }}"
      with_subelements:
        - "{{ vlans }}"
        - interfaces

gives

  msg: 10 ... interface=ether3
  msg: 10 ... interface=ether4
  msg: 20 ... interface=ether3
  msg: 20 ... interface=ether4

Upvotes: 1

Related Questions