Interface 用可靠的事实工作。获取接口未激活

Interface 用可靠的事实工作。获取接口未激活,interface,ansible,ip,Interface,Ansible,Ip,我需要为新接口配置IP地址 为此,我想获得服务器上所有可用的接口,并找出哪一个是非活动的 我首先使用ansible_interfaces变量来发现存在哪些接口。这取决于现有的接口(例如:eth0和lo),我将可以通过ansible_eth0和ansible_lo访问信息 稍后我需要做的是查看每个接口的“active”参数,因此获取将该参数设置为“false”的接口的名称,然后使用它修改文件 例如: 服务器有3个接口ens3ens7和loens7是incativa,我将使用它配置一个新的IP地址。

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

为此,我想获得服务器上所有可用的接口,并找出哪一个是非活动的

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

稍后我需要做的是查看每个接口的“active”参数,因此获取将该参数设置为“false”的接口的名称,然后使用它修改文件

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

如何使用Ansible执行此操作?

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

  - 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 }}"


这将选择非活动的乙醚卡