6

I'm attempting to configure a bridge interface on Ubuntu 16.04, templating the /etc/network/interfaces file in order to automatically insert the local NIC, like so:

auto br0
iface br0 inet dhcp
    bridge_ports <interface>

I do not have biosdevname installed, so the interface is showing as ens33 (on my test system), and changes on other hosts; instead of the historic eth0.

I can't use {{ ansible_default_ipv4.interface }} to complete the file, as this will only populate the file correctly on first run - after a reboot, running the playbook completes the file with the bridge br0 port instead.

I've tried a number of different iterations of using the {{ ansible_interfaces }} variable and attempted to match based on the regex en.* (all of these ports will be fixed Ethernet network cards) but none of them correctly populate the file.

The nearest I have come so far is:

- debug: msg={{ ansible_interfaces | map('match','ens.*') | list }}

which outputs

TASK [openvpn : debug] ****************************************************
ok: [192.168.0.134] => {
    "msg": [
        false,
        false,
        true
    ]
}

But I need to be able extract the actual adapter name that matches, not just whether there was a match or not.

Alternatively,

- debug: msg="{{ item }}"
  when: "{{ item }} | map('match','ens.*')"
  with_items: "{{ ansible_interfaces }}"

gives this promising output, but for all adapters, not just the one I'm attempting to match:

TASK [openvpn : debug] *****************************************************
ok: [192.168.0.134] => (item=lo) => {
    "item": "lo",
    "msg": "lo"
}
ok: [192.168.0.134] => (item=br0) => {
    "item": "br0",
    "msg": "br0"
}
ok: [192.168.0.134] => (item=ens33) => {
    "item": "ens33",
    "msg": "ens33"
}
10

Hmm, if you need to get interface name, try:

- debug:
    msg: "{{ ansible_interfaces | select('match','ens.*') | list | first }}"
| improve this answer | |
  • 1
    I would go with en.* rather than ens.* as otherwise you will miss interface names like enp2s1. – Michael Hampton Feb 1 '17 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.