【问题标题】:Work with Ansible facts. Get interface not active使用 Ansible 事实。获取界面未激活
【发布时间】:2019-11-14 09:21:56
【问题描述】:

我需要为新接口配置 IP 地址。

为此,我想获取服务器上所有可用的接口,并找出哪个接口处于非活动状态。

我首先使用 ansible_interfaces 变量来发现存在哪些接口。这取决于现有的接口(例如:eth0lo),我可以通过 ansible_eth0ansible_lo

我稍后需要做的是查看其每个接口的 "active" 参数,因此将具有该参数的接口的名称设为 "false" 然后用它来修改文件。

示例: 一个服务器有 3 个接口 ens3 ens7loens7 是 incativa,我将使用它来配置新的 IP 地址。我想要的是获取并保存在一个变量中这个值 ens7 供以后使用。

如何使用 Ansible 做到这一点?

【问题讨论】:

    标签: interface ansible ip


    【解决方案1】:

    使用此answer 作为参考,您可以执行以下任务:

      - set_fact:
          ansible_eth: "{% set ansible_eth = ansible_eth|default([]) + [hostvars[inventory_hostname]['ansible_' + item]] %}{{ ansible_eth|list }}"
        when: hostvars[inventory_hostname]['ansible_' + item]['type'] == 'ether' and hostvars[inventory_hostname]['ansible_' + item]['active'] == false
        with_items:
          - "{{ hostvars[inventory_hostname]['ansible_interfaces'] }}"
    
      - debug:
          msg: "Free device: {{ item.device }}"
        with_items: "{{ ansible_eth }}"
    
    
    

    这将选择非活动的以太卡。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 2014-05-28
      相关资源
      最近更新 更多