大网数据广告活动
服务器运维问答

How to select network interface given ip address in ansible across Debian and FreeBSD?

· 52CE 我爱测

I'm looking for an expression to fetch the interface name given an ip address assigned to that iface, across Linux and FreeBSD. This question is based on this answer: https://serverfault.com/a/948288/416946 This jinja2 expression…

问题原文

原帖链接 · Casey · CC BY-SA 4.0

I'm looking for an expression to fetch the interface name given an ip address assigned to that iface, across Linux and FreeBSD.

This question is based on this answer: https://serverfault.com/a/948288/416946

This jinja2 expression will, on Debian, return the interface object (from ansible facts) for the given_ip

iface_for_ip: >-
  {{ ansible_facts
  | dict2items
  | selectattr('value.ipv4', 'defined')
  | selectattr('value.ipv4.address', 'equalto', given_ip)
  | first  }}

However this does not work on FreeBSD because the ipv4 structure is an array, not an object.

If you run just this snippet:

iface_for_ip: >-
  {{ ansible_facts
  | dict2items
  | selectattr('value.ipv4', 'defined') }}

You will get an output like this:

on Debian
  - key: eth0
    value:
      active: true
      device: eth0
      ipv4:
        address: 10.8.20.206
        broadcast: 10.8.20.255
        netmask: 255.255.255.0
        network: 10.8.20.0
      ipv6:
      - address: fe80::84ee:35ff:fed4:a23c
        prefix: '64'
        scope: link
      macaddress: 00:ee:35:00:00:00
      mtu: 1500
      promisc: false
      speed: 10000
      type: ether
on FreeBSD
  - key: epair0b
    value:
      device: epair0b
      flags:
      - UP
      - BROADCAST
      - RUNNING
      - SIMPLEX
      - MULTICAST
      ipv4:
      - address: 10.8.20.207
        broadcast: 10.8.20.255
        netmask: 255.255.255.0
        network: 10.8.20.0
      ipv6: []
      macaddress: 00:ee:23:00:00:00
      media: Ethernet
      media_options:
      - full-duplex
      media_select: 10Gbase-T
      media_type: 10Gbase-T
      metric: '0'
      mtu: '1500'
      options:
      - PERFORMNUD
      status: active
      type: ether

How can I use a jinja2 ansible expression to fetch the interface given just the ip address cross platform? json_query could be useful here, but the method eludes me.

已采纳解答

原帖链接 · Vladimir Botka · CC BY-SA 4.0

There is a difference in data collected by setup on Debian and FreeBSD.


In Ubuntu (Debian derivative) the attribute ipv4 is a dictionary. Secondary addresses are stored in the list ipv4_secondaries. As a first step create a list of devices and ipv4 addresses. For example

    - debug:
        var: ansible_facts.distribution
    - set_fact:
        ifc_list: "{{ ansible_facts|
                      dict2items|
                      json_query(query)|
                      selectattr('ipv4')|list }}"
      vars:
        query: "[?value.type == 'ether'].{device: value.device,
                                          ipv4: value.ipv4.address}"
    - debug:
        var: ifc_list

give (abridged)

  ansible_facts.distribution: Ubuntu

  ifc_list:
  - device: eth0
    ipv4: 10.1.0.27

Then "select network interface for given ip address"

    - set_fact:
        iface_for_ip: "{{ ifc_list|
                          selectattr('ipv4', 'eq', ip_address)|
                          map(attribute='device')|list }}"
      vars:
        ip_address: "10.1.0.27"
    - debug:
        var: iface_for_ip

give (abridged)

  iface_for_ip:
  - eth0

In FreeBSD the attribute ipv4 is a list. Create a list of devices and ipv4

    - debug:
        var: ansible_facts.distribution
    - set_fact:
        ifc_list: "{{ ansible_facts|
                      dict2items|
                      json_query(query)|
                      selectattr('ipv4')|list }}"
      vars:
        query: "[?value.type == 'ether'].{device: value.device,
                                          ipv4: value.ipv4[].address}"
    - debug:
        var: ifc_list

give (abridged)

  ansible_facts.distribution: FreeBSD

  ifc_list:
  - device: wlan0
    ipv4:
    - 10.1.0.51

Then "select network interface for given ip address"

    - set_fact:
        iface_for_ip: "{{ iface_for_ip|default([]) + [item.device] }}"
      loop: "{{ ifc_list }}"
      when: ip_address in item.ipv4
      vars:
        ip_address: "10.1.0.51"
    - debug:
        var: iface_for_ip

give (abridged)

  iface_for_ip:
  - wlan0

Since Ansible 2.8 you can use test contains. The task below gives the same results

    - set_fact:
        iface_for_ip: "{{ ifc_list|
                          selectattr('ipv4', 'contains', ip_address)|
                          map(attribute='device')|
                          unique }}"
      vars:
        ip_address: "10.1.0.51"

Q: "How to remove the final set_fact + loop so it can be defined purely in a vars file?"

A: The attribute ipv4 is a list. To use selectattr, instead of a loop, you'll need a test contains(seq, value). There is no such test in Ansible version 2.7 and lower. Only in(value, seq) test with reversed order of parameters is available. If you don't have Ansible version 2.8 or higher you'll have to write your own test. For example

shell> cat test_plugins/contains.py 

def contains(l, value):
    return value in l


class TestModule:
    """Main test class from Ansible."""

    def tests(self):
        """Add these tests to the list of tests available to Ansible."""
        return {
            'contains': contains
        }

Then the set_fact gives the same result. The expression can be also used in vars

    - set_fact:
        iface_for_ip: "{{ ifc_list|
                          selectattr('ipv4', 'contains', ip_address)|
                          map(attribute='device')|list }}"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-10-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。