cisco.ios.ios_interfaces 模块 – 配置接口的资源模块。

注意

此模块是 cisco.ios 集合 (版本 9.0.3) 的一部分。

如果您使用的是 ansible 包,则可能已经安装了此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用: ansible-galaxy collection install cisco.ios

要在playbook中使用它,请指定: cisco.ios.ios_interfaces

cisco.ios 1.0.0 中的新增功能

概要

  • 此模块管理Cisco IOS网络设备的接口属性。

参数

参数

注释

config

列表 / 元素=字典

接口选项的字典

description

字符串

接口描述。

duplex

字符串

接口链路状态。仅适用于以太网接口,可以是半双工、全双工或自动状态(自动协商双工)。

选项

  • "full"

  • "half"

  • "auto"

enabled

布尔值

接口的管理状态。

将值设置为 true 以在管理上启用接口,或设置为 false 以禁用它。

选项

  • false

  • true ← (默认)

mode

字符串

管理接口的二层或三层状态。

对于二层设备模式,Layer2 添加 switchport 命令(默认影响幂等性)。

对于二层设备模式,Layer3 添加 no switchport 命令。

对于三层设备模式,Layer3/2 没有影响,应用命令会失败。

选项

  • "layer2"

  • "layer3"

mtu

整数

特定接口的MTU。仅适用于以太网接口。

请参考厂商文档以了解有效值。

name

字符串 / 必填

接口的全名,例如 GigabitEthernet0/2、loopback999。

speed

字符串

接口链路速度。仅适用于以太网接口。

template

字符串

IOS模板名称。

running_config

字符串

此选项仅与 state parsed 一起使用。

此选项的值应是从 IOS 设备执行命令 **show running-config | section ^interface** 接收到的输出。

state parsedrunning_config 选项读取配置并将其转换为 Ansible 结构化数据(根据资源模块的 argspec),然后该值将返回结果中的 parsed 键。

state

字符串

配置应保留的状态

状态 renderedgatheredparsed 不会对设备进行任何更改。

状态 renderedconfig 选项中的配置转换为特定于平台的 CLI 命令,这些命令将返回结果中的 rendered 键。对于状态 rendered,不需要与远程主机的活动连接。

状态 gathered 将从设备获取运行配置并将其转换为结构化数据(格式根据资源模块 argspec),并将值返回结果中的 gathered 键。

状态 parsedrunning_config 选项读取配置并将其转换为 JSON 格式(根据资源模块参数),并将值返回结果中的 parsed 键。running_config 选项的值应与在设备上执行命令 show running-config | include ip route|ipv6 route 的输出格式相同。对于状态 parsed,不需要与远程主机的活动连接。

状态 purged 会从运行配置中删除任务中指定的虚拟/逻辑接口。

选项

  • "merged" ← (默认)

  • "replaced"

  • "overridden"

  • "deleted"

  • "rendered"

  • "gathered"

  • "purged"

  • "parsed"

备注

注意

示例

# Using merged

# Before state:
# -------------
#
# Router#sh running-config | section interface
# interface Loopback888
#  no ip address
# interface Loopback999
#  no ip address
# interface GigabitEthernet1
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  description Configured and Merged by Ansible Network
#  ip address dhcp
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  no ip address
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

- name: Merge provided configuration with device configuration
  cisco.ios.ios_interfaces:
    config:
      - name: GigabitEthernet2
        description: Configured and Merged by Ansible Network
        enabled: true
      - name: GigabitEthernet3
        description: Configured and Merged by Ansible Network
        mtu: 3800
        enabled: false
        speed: 100
        duplex: full
    state: merged

# Task Output
# -----------
#
# before:
# - enabled: true
#   name: GigabitEthernet1
# - description: Configured and Merged by Ansible Network
#   enabled: true
#   name: GigabitEthernet2
#   speed: '1000'
# - description: Configured and Merged by Ansible Network
#   enabled: false
#   mtu: 3800
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: true
#   name: Loopback888
# - enabled: true
#   name: Loopback999
# commands:
# - interface GigabitEthernet3
# - description Configured and Merged by Ansible Network
# - speed 100
# - mtu 3800
# - duplex full
# - shutdown
# after:
# - enabled: true
#   name: GigabitEthernet1
# - description: Configured and Merged by Ansible Network
#   enabled: true
#   name: GigabitEthernet2
#   speed: '1000'
# - description: Configured and Merged by Ansible Network
#   enabled: true
#   mtu: 2800
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: true
#   name: Loopback888
# - enabled: true
#   name: Loopback999

# After state:
# ------------
#
# Router#show running-config | section ^interface
# interface Loopback888
#  no ip address
# interface Loopback999
#  no ip address
# interface GigabitEthernet1
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  description Configured and Merged by Ansible Network
#  ip address dhcp
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Merged by Ansible Network
#  mtu 3800
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

# Using merged - with mode attribute

# Before state:
# -------------
#
# vios#show running-config | section ^interface
# interface GigabitEthernet1
#  description Configured by Ansible
# interface GigabitEthernet2
#  description This is test
# interface GigabitEthernet3
#  description This is test
#  no switchport

- name: Merge provided configuration with device configuration
  cisco.ios.ios_interfaces:
    config:
      - name: GigabitEthernet2
        description: Configured and Merged by Ansible Network
        enabled: true
        mode: layer2
      - name: GigabitEthernet3
        description: Configured and Merged by Ansible Network
        mode: layer3
    state: merged

# Task Output
# -----------
#
# before:
# - enabled: true
#   name: GigabitEthernet1
# - description: Configured and Merged by Ansible Network
#   name: GigabitEthernet2
# - description: Configured and Merged by Ansible Network
#   name: GigabitEthernet3
# commands:
# - interface GigabitEthernet2
# - description Configured and Merged by Ansible Network
# - switchport
# - interface GigabitEthernet3
# - description Configured and Merged by Ansible Network
# after:
# - enabled: true
#   name: GigabitEthernet1
# - description: Configured and Merged by Ansible Network
#   enabled: true
#   name: GigabitEthernet2
# - description: Configured and Merged by Ansible Network
#   name: GigabitEthernet3
#   mode: layer3

# After state:
# ------------
#
# vios#show running-config | section ^interface
# interface GigabitEthernet1
#  description Configured by Ansible
# interface GigabitEthernet2
#  description Configured and Merged by Ansible Network
# interface GigabitEthernet3
#  description Configured and Merged by Ansible Network
#  no switchport

# Using replaced

# Before state:
# -------------
#
# vios#show running-config | section ^interface
# interface Loopback888
#  no ip address
# interface Loopback999
#  no ip address
# interface GigabitEthernet1
#  description Management interface do not change
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  ip address dhcp
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  no ip address
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto
# interface Vlan50
#  ip address dhcp hostname testHostname

- name: Replaces device configuration of listed interfaces with provided configuration
  cisco.ios.ios_interfaces:
    config:
      - name: GigabitEthernet3
        description: Configured and Replaced by Ansible Network
        enabled: false
        speed: 1000
    state: replaced

# Task Output
# -----------
#
# before:
# - description: Management interface do not change
#   enabled: true
#   name: GigabitEthernet1
# - enabled: true
#   name: GigabitEthernet2
#   speed: '1000'
# - enabled: true
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: true
#   name: Loopback888
# - enabled: true
#   name: Loopback999
# - enabled: true
#   name: Vlan50
# commands:
# - interface GigabitEthernet3
# - description Configured and Replaced by Ansible Network
# - shutdown
# after:
# - description: Management interface do not change
#   enabled: true
#   name: GigabitEthernet1
# - enabled: true
#   name: GigabitEthernet2
#   speed: '1000'
# - description: Configured and Replaced by Ansible Network
#   enabled: false
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: true
#   name: Loopback888
# - enabled: true
#   name: Loopback999
# - enabled: true
#   name: Vlan50

# After state:
# -------------
#
# vios#show running-config | section ^interface
# interface Loopback888
#  no ip address
# interface Loopback999
#  no ip address
# interface GigabitEthernet1
#  description Management interface do not change
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  ip address dhcp
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Replaced by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto
# interface Vlan50
#  ip address dhcp hostname testHostname

# Using overridden

# Before state:
# -------------
#
# vios#show running-config | section ^interface
# interface Loopback888
#  no ip address
# interface Loopback999
#  no ip address
# interface GigabitEthernet1
#  description Management interface do not change
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  ip address dhcp
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Replaced by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto
# interface Vlan50
#  ip address dhcp hostname testHostname

- name: Override device configuration of all interfaces with provided configuration
  cisco.ios.ios_interfaces:
    config:
      - description: Management interface do not change
        enabled: true
        name: GigabitEthernet1
      - name: GigabitEthernet2
        description: Configured and Overridden by Ansible Network
        speed: 10000
      - name: GigabitEthernet3
        description: Configured and Overridden by Ansible Network
        enabled: false
    state: overridden

# Task Output
# -----------
#
# before:
# - description: Management interface do not change
#   enabled: true
#   name: GigabitEthernet1
# - enabled: true
#   name: GigabitEthernet2
#   speed: '1000'
# - description: Configured and Replaced by Ansible Network
#   enabled: false
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: true
#   name: Loopback888
# - enabled: true
#   name: Loopback999
# - enabled: true
#   name: Vlan50
# commands:
# - interface loopback888
# - shutdown
# - interface loopback999
# - shutdown
# - interface Vlan50
# - shutdown
# - interface GigabitEthernet2
# - description Configured and Overridden by Ansible Network
# - speed 10000
# - interface GigabitEthernet3
# - description Configured and Overridden by Ansible Network
# - no speed 1000
# after:
# - description: Management interface do not change
#   enabled: true
#   name: GigabitEthernet1
# - description: Configured and Overridden by Ansible Network
#   enabled: true
#   name: GigabitEthernet2
#   speed: '10000'
# - description: Configured and Overridden by Ansible Network
#   enabled: false
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: false
#   name: Loopback888
# - enabled: false
#   name: Loopback999
# - enabled: false
#   name: Vlan50

# After state:
# -------------
#
# vios#show running-config | section ^interface
# interface Loopback888
#  no ip address
#  shutdown
# interface Loopback999
#  no ip address
#  shutdown
# interface GigabitEthernet1
#  description Management interface do not change
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  description Configured and Overridden by Ansible Network
#  ip address dhcp
#  speed 10000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto
# interface Vlan50
#  ip address dhcp hostname testHostname
#  shutdown

# Using Deleted

# Before state:
# -------------
#
# vios#show running-config | section ^interface
# interface Loopback888
#  no ip address
#  shutdown
# interface Loopback999
#  no ip address
#  shutdown
# interface GigabitEthernet1
#  description Management interface do not change
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  description Configured and Overridden by Ansible Network
#  ip address dhcp
#  speed 10000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto
# interface Vlan50
#  ip address dhcp hostname testHostname
#  shutdown

- name: "Delete interface attributes (Note: This won't delete the interface itself)"
  cisco.ios.ios_interfaces:
    config:
      - name: GigabitEthernet2
    state: deleted

# Task Output
# -----------
#
# before:
# - description: Management interface do not change
#   enabled: true
#   name: GigabitEthernet1
# - description: Configured and Overridden by Ansible Network
#   enabled: true
#   name: GigabitEthernet2
#   speed: '10000'
# - description: Configured and Overridden by Ansible Network
#   enabled: false
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: false
#   name: Loopback888
# - enabled: false
#   name: Loopback999
# - enabled: false
#   name: Vlan50
# commands:
# - interface GigabitEthernet2
# - no description Configured and Overridden by Ansible Network
# - no speed 10000
# - shutdown
# after:
# - description: Management interface do not change
#   enabled: true
#   name: GigabitEthernet1
# - enabled: false
#   name: GigabitEthernet2
#   speed: '1000'
# - description: Configured and Overridden by Ansible Network
#   enabled: false
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: false
#   name: Loopback888
# - enabled: false
#   name: Loopback999
# - enabled: false
#   name: Vlan50

# After state:
# -------------
#
# vios#show running-config | section ^interface
# interface Loopback888
#  no ip address
#  shutdown
# interface Loopback999
#  no ip address
#  shutdown
# interface GigabitEthernet1
#  description Management interface do not change
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  ip address dhcp
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto
# interface Vlan50
#  ip address dhcp hostname testHostname
#  shutdown

# Using Purged

# Before state:
# -------------
#
# vios#show running-config | section ^interface
# interface Loopback888
#  no ip address
#  shutdown
# interface Loopback999
#  no ip address
#  shutdown
# interface GigabitEthernet1
#  description Management interface do not change
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  ip address dhcp
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto
# interface Vlan50
#  ip address dhcp hostname testHostname
#  shutdown

- name: "Purge given interfaces (Note: This will delete the interface itself)"
  cisco.ios.ios_interfaces:
    config:
      - name: Loopback888
      - name: Vlan50
    state: purged

# Task Output
# -----------
#
# before:
# - description: Management interface do not change
#   enabled: true
#   name: GigabitEthernet1
# - enabled: false
#   name: GigabitEthernet2
#   speed: '1000'
# - description: Configured and Overridden by Ansible Network
#   enabled: false
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: false
#   name: Loopback888
# - enabled: false
#   name: Loopback999
# - enabled: false
#   name: Vlan50
# commands:
# - no interface loopback888
# - no interface Vlan50
# after:
# - description: Management interface do not change
#   enabled: true
#   name: GigabitEthernet1
# - enabled: false
#   name: GigabitEthernet2
#   speed: '1000'
# - description: Configured and Overridden by Ansible Network
#   enabled: false
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: false
#   name: Loopback999

# After state:
# -------------
#
# vios#show running-config | section ^interface
# interface Loopback999
#  no ip address
#  shutdown
# interface GigabitEthernet1
#  description Management interface do not change
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  ip address dhcp
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

# Using gathered

# Before state:
# -------------
#
# vios#sh running-config | section ^interface
# interface Loopback999
#  no ip address
#  shutdown
# interface GigabitEthernet1
#  description Management interface do not change
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  ip address dhcp
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

- name: Gather facts of interfaces
  cisco.ios.ios_interfaces:
    config:
    state: gathered

# Task Output
# -----------
#
# gathered:
# - description: Management interface do not change
#   enabled: true
#   name: GigabitEthernet1
# - enabled: false
#   name: GigabitEthernet2
#   speed: '1000'
# - description: Configured and Overridden by Ansible Network
#   enabled: false
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: false
#   name: Loopback999

# Using rendered

- name: Render the commands for provided configuration
  cisco.ios.ios_interfaces:
    config:
      - name: GigabitEthernet1
        description: Configured by Ansible-Network
        mtu: 110
        enabled: true
        duplex: half
      - name: GigabitEthernet2
        description: Configured by Ansible-Network
        mtu: 2800
        enabled: false
        speed: 100
        duplex: full
    state: rendered

# Task Output
# -----------
#
# rendered:
# - interface GigabitEthernet1
# - description Configured by Ansible-Network
# - mtu 110
# - duplex half
# - no shutdown
# - interface GigabitEthernet2
# - description Configured by Ansible-Network
# - speed 100
# - mtu 2800
# - duplex full
# - shutdown

# Using parsed

# File: parsed.cfg
# ----------------
#
# interface Loopback999
#  no ip address
#  shutdown
# interface GigabitEthernet1
#  description Management interface do not change
#  ip address dhcp
#  negotiation auto
# interface GigabitEthernet2
#  ip address dhcp
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

- name: Parse the provided configuration
  cisco.ios.ios_interfaces:
    running_config: "{{ lookup('file', 'parsed.cfg') }}"
    state: parsed

# Task Output
# -----------
#
# parsed:
# - description: Management interface do not change
#   enabled: true
#   name: GigabitEthernet1
# - enabled: false
#   name: GigabitEthernet2
#   speed: '1000'
# - description: Configured and Overridden by Ansible Network
#   enabled: false
#   name: GigabitEthernet3
#   speed: '1000'
# - enabled: false
#   name: GigabitEthernet4
# - enabled: false
#   name: Loopback999

返回值

常见的返回值已在 此处 记录,以下是此模块特有的字段

描述

after

字典

模块执行后的结果配置。

返回:已更改时

示例: "此 输出 始终 模块 argspec 格式 相同。\n"

之前

字典

模块执行之前的配置。

返回:statemergedreplacedoverriddendeletedpurged

示例: "此 输出 始终 模块 argspec 格式 相同。\n"

命令

列表 / 元素=字符串

推送到远程设备的命令集。

返回:statemergedreplacedoverriddendeletedpurged

示例: ["interface GigabitEthernet2", "speed 1200", "mtu 1800"]

收集的

列表 / 元素=字符串

从远程设备收集的关于网络资源的结构化数据事实。

返回:stategathered

示例: ["此 输出 始终 模块 argspec 格式 相同。\n"]

解析的

列表 / 元素=字符串

根据模块 argspec 将 running_config 选项中提供的设备原生配置解析为结构化数据。

返回:stateparsed

示例: ["此 输出 始终 模块 argspec 格式 相同。\n"]

渲染的

列表 / 元素=字符串

以设备原生格式(离线)渲染的任务中提供的配置。

返回:staterendered

示例: ["interface GigabitEthernet1", "description Interface description", "shutdown"]

作者

  • Sumit Jaiswal (@justjais)

  • Sagar Paul (@KB-perByte)