AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1040246
Accepted
Casey
Casey
Asked: 2020-10-28 06:41:03 +0800 CST2020-10-28 06:41:03 +0800 CST 2020-10-28 06:41:03 +0800 CST

如何在 Debian 和 FreeBSD 的 ansible 中选择给定 IP 地址的网络接口?

  • 772

我正在寻找一个表达式来获取接口名称,给定分配给该 iface 的 IP 地址,跨 Linux 和 FreeBSD。

这个问题基于这个答案:https ://serverfault.com/a/948288/416946

这个 jinja2 表达式将在 Debian 上返回接口对象(来自 ansible 事实)given_ip

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

然而,这在 FreeBSD 上不起作用,因为该ipv4结构是一个数组,而不是一个对象。

如果你只运行这个片段:

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

你会得到这样的输出:

在 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
在 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

如何使用 jinja2 ansible 表达式来获取仅给出 ip 地址跨平台的接口? json_query在这里可能很有用,但该方法使我无法理解。

freebsd ansible jinja jinja2
  • 2 2 个回答
  • 1386 Views

2 个回答

  • Voted
  1. Best Answer
    Vladimir Botka
    2020-10-28T10:41:54+08:002020-10-28T10:41:54+08:00

    在 Debian 和 FreeBSD 上通过setup收集的数据有所不同。


    在 Ubuntu(Debian 衍生产品)中,属性ipv4是一个字典。辅助地址存储在ipv4_secondaries列表中。作为第一步,创建设备列表和 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
    

    给(删减)

      ansible_facts.distribution: Ubuntu
    
      ifc_list:
      - device: eth0
        ipv4: 10.1.0.27
    

    然后“为给定的IP地址选择网络接口”

        - 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
    

    给(删减)

      iface_for_ip:
      - eth0
    

    在 FreeBSD 中,属性ipv4是一个列表。创建设备列表和 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
    

    给(删减)

      ansible_facts.distribution: FreeBSD
    
      ifc_list:
      - device: wlan0
        ipv4:
        - 10.1.0.51
    

    然后“为给定的IP地址选择网络接口”

        - 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
    

    给(删减)

      iface_for_ip:
      - wlan0
    

    从 Ansible 2.8 开始,您可以使用 test contains。下面的任务给出了相同的结果

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

    问:“如何删除最终的 set_fact + 循环,以便可以在 vars 文件中纯粹定义它? ”

    A:属性ipv4是一个列表。要使用selectattr而不是循环,您需要一个测试 contains(seq, value)。Ansible 2.7 及更低版本中没有这样的测试。只有in(value, seq)参数顺序相反的测试可用。如果您没有 Ansible 2.8 或更高版本,则必须编写自己的测试。例如

    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
            }
    

    然后set_fact给出相同的结果。表达式也可以用在vars中

        - set_fact:
            iface_for_ip: "{{ ifc_list|
                              selectattr('ipv4', 'contains', ip_address)|
                              map(attribute='device')|list }}"
    
    • 4
  2. simohe
    2020-10-29T07:56:29+08:002020-10-29T07:56:29+08:00

    灵感来自其他答案和链接的要点

    vars:
        freebsd_query: "[*].{device: device, active: active, ipv6: ipv6, ipv4: ipv4[? address == '{{ ip_find_iface }}']}[?ipv4])" # string must be in ' # sorry, only partial interface info, did not find out how to return all info directly
        linux_query: "[?ipv4.address == '{{ ip_find_iface }}']" # string must be in '
        ipv6_query: "[*].{device: device, active: active, ipv4: ipv4, ipv6: ipv6[? address == '{{ ip_find_iface }}']}[?ipv6]" # string must be in ' # sorry, only partial info, did not find how to return the full one
        ip_query: "{{ ip_find_iface | ipv6 | ternary(
            ipv6_query,
            ansible_facts[ansible_facts.interfaces | first].ipv4 is mapping | ternary(linux_query, freebsd_query)
        ) }}.device"
        all_interfaces: "{{ ansible_facts.interfaces | map('extract', ansible_facts) }}"
        iface_for_ip: "{{ all_interfaces | json_query(ip_query) }}"
    

    ip_query首先检查 ip 是否为 ipv6 格式。如果不是,它检查是否{anyNetwork}.ipv4是一个字典。根据此选择查询json_query()。

    (可能有错别字,因为我不能(简单地)测试所有。已经纠正了大约 5 次......)
    (这是如何发展的:https ://gist.github.com/Ramblurr/5d8324e0154ea6be52407618222fcaf7 )

    • 1

相关问题

  • Mono 2.4 Ahead-Of-Time (AOT) 可以在 FreeBSD x86(或 x64)上编译吗?

  • FreeBSD 和 Linux 有什么区别?[关闭]

  • 在 FreeBSD 上安装和运行 MySql

  • 在 freeBSD 上安装 netbeans 的问题

  • 用于 Web 应用服务器的 FreeBSD 磁盘分区(Apache/MySQL/PHP)

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve