1

I'm writing an Ansible role that sets up the network. For each type of interface (ethernet, bond, bridge and vlan) I made a variable that contains the relevant data. The idea is that I have to make a loop that runs the number of times that there are elements in a list variable ('bridge_ports') and for each pass a configuration file is created via a template.

Parts of the variable for bridge interfaces look like this:

my_network__bridge_interface:
    address: "192.168.1.48",
    bootproto: "static",
    bridge_ports:
        - eth0
        - eth1
    device: "br-mgmt",
    ...

To make the pass, I have tried with a with_subelements loop - but this does not go well.

- name: Create the network configuration file for the port on the bridge devices
  template:
    src: "{{ ansible_os_family }}.bridge_port.j2"
    dest: "{{ my_network__ifconf_path }}/ifcfg-{{ item.1 }}"
  with_subelements
    - "{{ my_network__bridge_interface }}"
    - bridge_ports
  when: device_conf.type == 'bridge'
  register: my_network__bridge_port_result

When I run the code, the error message comes: "could not find 'bridge_ports' key in iterated item '{}'".

I can see that I use with_subelements in the wrong way, but I don't really know what type of loop I would otherwise need.

1

1 Answer 1

1

The issue is with the yml definition. The below yml works:

my_network__bridge_interface:

  - address: "192.168.1.48"
    bootproto: static
    bridge_ports:
      - eth0
      - eth1
    device: br-mgmt   

playbook -->

    ---
    - hosts: localhost
      tasks:
        - include_vars: vars.yml
        - debug:
            msg: "{{ item.1 }}"
          with_subelements:
            - "{{ my_network__bridge_interface }}"
            - bridge_ports

output -->

TASK [debug] ****************************************************************************************************************************
ok: [localhost] => (item=[{u'device': u'br-mgmt', u'bootproto': u'static', u'address': u'192.168.1.48'}, u'eth0']) => {
    "msg": "eth0"
}
ok: [localhost] => (item=[{u'device': u'br-mgmt', u'bootproto': u'static', u'address': u'192.168.1.48'}, u'eth1']) => {
    "msg": "eth1"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.