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

注意

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

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

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

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

cisco.ios 1.0.0 中的新增功能

概要

  • 此模块提供对 Cisco IOS 设备上三层接口的声明式管理。

参数

参数

注释

config

列表 / 元素=字典

三层接口选项的字典

autostate

布尔值

为 VLAN 启用自动状态确定。

选项

  • false

  • true

ipv4

列表 / 元素=字典

为在 *name* 选项中提到的三层接口设置的 IPv4 地址。地址格式为 <ipv4 地址>/<掩码>,掩码是 0-32 范围内的数字,例如 192.168.0.1/24。

address

字符串

配置接口的 IPv4 地址。

dhcp

字典

通过 DHCP 协商的 IP 地址。

client_id

字符串

指定要使用的 client-id。

enable

布尔值

启用 dhcp。

选项

  • false

  • true

hostname

字符串

指定 hostname 选项的值。

dhcp_client

字符串

配置并指定要通过 DHCP ip 使用的 client-id。注意,此选项仅在将 dhcp 配置为 IP 时才有效。

千兆以太网接口号

此选项已弃用,并替换为接受字典作为输入的 dhcp,此属性将在 2023-08-01 后删除。

dhcp_hostname

字符串

配置并指定通过 DHCP ip 的 hostname 选项的值。注意,此选项仅在将 dhcp 配置为 IP 时才有效。

此选项已弃用,并替换为接受字典作为输入的 dhcp,此属性将在 2023-08-01 后删除。

pool

字符串

从本地 DHCP 池自动配置的 IP 地址。

secondary

布尔值

将 IP 地址配置为辅助地址。

选项

  • false

  • true

source_interface

字典

启用 IP 处理,无需显式地址

name

字符串

接口名称

point_to_point

布尔值

启用点对点连接

选项

  • false

  • true

poll

布尔值

启用 IP 连接的主机轮询

选项

  • false

  • true

ipv6

列表 / 元素=字典

为在 *name* 选项中提到的三层接口设置的 IPv6 地址。

地址格式为 <ipv6 地址>/<掩码>,掩码是 0-128 范围内的数字,例如 fd5d:12c9:2201:1::1/64

address

字符串

配置接口的 IPv6 地址。

anycast

布尔值

配置为单播

选项

  • false

  • true

autoconfig

字典

使用自动配置获取地址。

default

布尔值

插入默认路由。

选项

  • false

  • true

enable

布尔值

启用自动配置。

选项

  • false

  • true

cga

布尔值

使用 CGA 接口标识符

选项

  • false

  • true

dhcp

字典

使用 DHCP 获取 ipv6 地址。

enable

布尔值

启用 dhcp。

选项

  • false

  • true

rapid_commit

布尔值

启用快速提交。

选项

  • false

  • true

enable

布尔值

在接口上启用 IPv6

选项

  • false

  • true

eui

布尔值

使用 eui-64 接口标识符

选项

  • false

  • true

布尔值

使用链路本地地址

选项

  • false

  • true

segment_routing

字典

分段路由子模式

default

布尔值

将命令设置为其默认值。

选项

  • false

  • true

enable

布尔值

启用分段路由。

选项

  • false

  • true

ipv6_sr

布尔值

设置 ipv6_sr。

选项

  • false

  • true

mac_address

字符串

手动设置接口 MAC 地址。

name

字符串 / 必需

接口的全名,不包括任何逻辑单元号,即 GigabitEthernet0/1。

running_config

字符串

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

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

state *parsed* 从 running_config 选项读取配置并将其转换为 Ansible 结构化数据,符合资源模块的 argspec,然后该值将返回到结果中的 *parsed* 密钥中。

state

字符串

配置应保留到的状态

状态 *rendered*、*gathered* 和 *parsed* 不会对设备进行任何更改。

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

状态 *gathered* 将从设备获取运行配置,并将其转换为符合资源模块 argspec 格式的结构化数据,并将值返回到结果中的 *gathered* 密钥中。

状态 *parsed* 从 running_config 选项读取配置,并将其转换为符合资源模块参数的 JSON 格式,并将值返回到结果中的 *parsed* 密钥中。running_config 选项的值应与在设备上执行的命令 *show running-config | section ^interface* 的输出格式相同。对于状态 *parsed*,不需要与远程主机的活动连接。

选项

  • "merged" ← (默认)

  • "replaced"

  • "overridden"

  • "deleted"

  • "rendered"

  • "gathered"

  • "parsed"

备注

注意

示例

# Using merged

# Before state:
# -------------
#
# Router#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

- name: Merge provided configuration with device configuration
  cisco.ios.ios_l3_interfaces:
    config:
      - name: GigabitEthernet0/1
        ipv4:
          - address: 192.168.0.1/24
            secondary: true
      - name: GigabitEthernet2
        ipv4:
          - address: 192.168.0.2/24
      - name: GigabitEthernet3
        ipv6:
          - address: fd5d:12c9:2201:1::1/64
      - name: GigabitEthernet3.100
        ipv4:
          - address: 192.168.0.3/24
      - name: Vlan901
        autostate: false
        ipv4:
          - source_interface:
              name: Loopback1
        ipv6:
          - enable: true
    state: merged

# Task Output
# -----------
#
# before:
# - ipv4:
#   - dhcp:
#       enable: true
#   name: GigabitEthernet1
# - ipv4:
#   - dhcp:
#       enable: true
#   name: GigabitEthernet2
# - name: GigabitEthernet3
# - name: GigabitEthernet4
# - name: Loopback999
# commands:
# - interface GigabitEthernet2
# - ip address 192.168.0.2 255.255.255.0
# - interface GigabitEthernet3
# - ipv6 address fd5d:12c9:2201:1::1/64
# - interface GigabitEthernet3.100
# - ip address 192.168.0.3 255.255.255.0
# - interface Vlan901
# - ip unnumbered Loopback1
# - ipv6 enable
# - no autostate
# after:
# - ipv4:
#   - dhcp:
#       enable: true
#   name: GigabitEthernet1
# - ipv4:
#   - address: 192.168.0.2/24
#   name: GigabitEthernet2
# - ipv6:
#   - address: FD5D:12C9:2201:1::1/64
#   name: GigabitEthernet3
# - name: GigabitEthernet3.100
#   ipv4:
#   - address: 192.168.0.3/24
# - name: GigabitEthernet4
# - name: Loopback999
#   ipv4:
#   - source_interface:
#       name: Loopback1
#   ipv6:
#   - enable: true
#   autostate: false
# - name: Vlan901

# After state:
# ------------
#
# Router#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 192.168.0.2 255.255.255.0
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
#  ipv6 address FD5D:12C9:2201:1::1/64
# interface GigabitEthernet3.100
#  ip address 192.168.0.3 255.255.255.0
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto
# interface Vlan901
#  ip unnumbered Loopback1
#  ipv6 enable
#  no autostate


# Using replaced

# Before state:
# -------------
#
# Router#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 192.168.0.2 255.255.255.0
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
#  ipv6 address FD5D:12C9:2201:1::1/64
# interface GigabitEthernet3.100
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

- name: Replaces device configuration of listed interfaces with provided configuration
  cisco.ios.ios_l3_interfaces:
    config:
      - name: GigabitEthernet2
        ipv4:
          - address: 192.168.2.0/24
      - name: GigabitEthernet3
        ipv4:
          - dhcp:
              client_id: GigabitEthernet2
              hostname: test.com
    state: replaced

# Task Output
# -----------
#
# before:
# - ipv4:
#   - dhcp:
#       enable: true
#   name: GigabitEthernet1
# - ipv4:
#   - address: 192.168.0.2/24
#   name: GigabitEthernet2
# - ipv6:
#   - address: FD5D:12C9:2201:1::1/64
#   name: GigabitEthernet3
# - name: GigabitEthernet3.100
# - name: GigabitEthernet4
# - name: Loopback999
# commands:
# - interface GigabitEthernet2
# - ip address 192.168.0.3 255.255.255.0
# - no ip address 192.168.0.2 255.255.255.0
# - interface GigabitEthernet3
# - ip address dhcp client-id GigabitEthernet2 hostname test.com
# - no ipv6 address fd5d:12c9:2201:1::1/64
# after:
# - ipv4:
#   - dhcp:
#       enable: true
#   name: GigabitEthernet1
# - ipv4:
#   - address: 192.168.0.3/24
#   name: GigabitEthernet2
# - ipv4:
#   - dhcp:
#       client_id: GigabitEthernet2
#       enable: true
#       hostname: test.com
#   name: GigabitEthernet3
# - name: GigabitEthernet3.100
# - name: GigabitEthernet4
# - name: Loopback999

# After state:
# ------------
#
# router-ios#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 192.168.0.3 255.255.255.0
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  ip address dhcp client-id GigabitEthernet2 hostname test.com
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3.100
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

# Using overridden

# Before state:
# -------------
#
# router-ios#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 192.168.0.3 255.255.255.0
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  ip address dhcp client-id GigabitEthernet2 hostname test.com
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3.100
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

- name: Override device configuration of all interfaces with provided configuration
  cisco.ios.ios_l3_interfaces:
    config:
      - ipv4:
          - dhcp:
              enable: true
        name: GigabitEthernet1
      - name: GigabitEthernet2
        ipv4:
          - address: 192.168.0.1/24
      - name: GigabitEthernet3
    state: overridden

# Task Output
# -----------
# before:
# - ipv4:
#   - dhcp:
#       enable: true
#   name: GigabitEthernet1
# - ipv4:
#   - address: 192.168.0.3/24
#   name: GigabitEthernet2
# - ipv4:
#   - dhcp:
#       client_id: GigabitEthernet2
#       enable: true
#       hostname: test.com
#   name: GigabitEthernet3
# - name: GigabitEthernet3.100
# - name: GigabitEthernet4
# - name: Loopback999
# commands:
# - interface GigabitEthernet2
# - ip address 192.168.0.1 255.255.255.0
# - no ip address 192.168.0.3 255.255.255.0
# - interface GigabitEthernet3
# - no ip address dhcp client-id GigabitEthernet2 hostname test.com
# after:
# - ipv4:
#   - dhcp:
#       enable: true
#   name: GigabitEthernet1
# - ipv4:
#   - address: 192.168.0.1/24
#   name: GigabitEthernet2
# - name: GigabitEthernet3
# - name: GigabitEthernet3.100
# - name: GigabitEthernet4
# - name: Loopback999

# After state:
# ------------
#
# router-ios#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 192.168.0.1 255.255.255.0
#  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 GigabitEthernet3.100
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

# Using deleted

# Before state:
# -------------
#
# router-ios#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 192.168.0.1 255.255.255.0
#  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 GigabitEthernet3.100
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

- name: "Delete attributes of given interfaces (NOTE: This won't delete the interfaces)"
  cisco.ios.ios_l3_interfaces:
    config:
      - name: GigabitEthernet2
      - name: GigabitEthernet3.100
    state: deleted

# Task Output
# -----------
#
# before:
# - ipv4:
#   - dhcp:
#       enable: true
#   name: GigabitEthernet1
# - ipv4:
#   - address: 192.168.0.1/24
#   name: GigabitEthernet2
# - name: GigabitEthernet3
# - name: GigabitEthernet3.100
# - name: GigabitEthernet4
# - name: Loopback999
# commands:
# - interface GigabitEthernet2
# - no ip address 192.168.0.1 255.255.255.0
# after:
# - ipv4:
#   - dhcp:
#       enable: true
#   name: GigabitEthernet1
# - name: GigabitEthernet2
# - name: GigabitEthernet3
# - name: GigabitEthernet3.100
# - name: GigabitEthernet4
# - name: Loopback999

# After state:
# -------------
#
# router-ios#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
#  no ip address
#  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 GigabitEthernet3.100
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

# Using deleted without config passed, only interface's configuration will be negated

# Before state:
# -------------

# router-ios#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 192.168.0.2 255.255.255.0
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  no ip address
#  shutdown
#  speed 1000
#  no negotiation auto
#  ipv6 address FD5D:12C9:2201:1::1/64
# interface GigabitEthernet3.100
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

- name: "Delete L3 config of all interfaces"
  cisco.ios.ios_l3_interfaces:
    state: deleted

# Task Output
# -----------
#
# before:
# - ipv4:
#   - dhcp:
#       enable: true
#   name: GigabitEthernet1
# - ipv4:
#   - address: 192.168.0.2/24
#   name: GigabitEthernet2
# - ipv6:
#   - address: FD5D:12C9:2201:1::1/64
#   name: GigabitEthernet3
# - name: GigabitEthernet3.100
# - name: GigabitEthernet4
# - name: Loopback999
# commands:
# - interface GigabitEthernet1
# - no ip address dhcp
# - interface GigabitEthernet2
# - no ip address 192.168.0.2 255.255.255.0
# - interface GigabitEthernet3
# - no ipv6 address fd5d:12c9:2201:1::1/64
# after:
# - name: GigabitEthernet1
# - name: GigabitEthernet2
# - name: GigabitEthernet3
# - name: GigabitEthernet3.100
# - name: GigabitEthernet4
# - name: Loopback999

# After state:
# -------------
#
# interface Loopback999
#  no ip address
#  shutdown
# interface GigabitEthernet1
#  description Management interface do not change
#  negotiation auto
# interface GigabitEthernet2
#  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 GigabitEthernet3.100
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

# Using gathered

# Before state:
# -------------
#
# Router#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 192.168.0.3 255.255.255.0
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3
#  description Configured and Overridden by Ansible Network
#  ip address dhcp client-id GigabitEthernet2 hostname test.com
#  shutdown
#  speed 1000
#  no negotiation auto
# interface GigabitEthernet3.100
# interface GigabitEthernet4
#  no ip address
#  shutdown
#  negotiation auto

- name: Gather facts for l3 interfaces
  cisco.ios.ios_l3_interfaces:
    state: gathered

# Task Output
# -----------
#
# gathered:
# - ipv4:
#   - dhcp:
#       enable: true
#   name: GigabitEthernet1
# - ipv4:
#   - address: 192.168.0.3/24
#   name: GigabitEthernet2
# - ipv4:
#   - dhcp:
#       client_id: GigabitEthernet2
#       enable: true
#       hostname: test.com
#   name: GigabitEthernet3
# - name: GigabitEthernet3.100
# - name: GigabitEthernet4
# - name: Loopback999

# Using rendered

- name: Render the commands for provided configuration
  cisco.ios.ios_l3_interfaces:
    config:
      - name: GigabitEthernet1
        ipv4:
          - dhcp:
              client_id: GigabitEthernet0/0
              hostname: test.com
      - name: GigabitEthernet2
        ipv4:
          - address: 198.51.100.1/24
            secondary: true
          - address: 198.51.100.2/24
        ipv6:
          - address: 2001:db8:0:3::/64
    state: rendered

# Task Output
# -----------
#
# rendered:
# - interface GigabitEthernet1
# - ip address dhcp client-id GigabitEthernet0/0 hostname test.com
# - interface GigabitEthernet2
# - ip address 198.51.100.1 255.255.255.0 secondary
# - ip address 198.51.100.2 255.255.255.0
# - ipv6 address 2001:db8:0:3::/64

# Using parsed

# File: parsed.cfg
# ----------------
#
# interface GigabitEthernet0/1
#  ip address dhcp client-id GigabitEthernet 0/0 hostname test.com
# interface GigabitEthernet0/2
#  ip address 198.51.100.1 255.255.255.0
#  ip address 198.51.100.2 255.255.255.0 secondary
#  ipv6 address 2001:db8:0:3::/64

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

# Task Output
# -----------
#
# parsed:
# - ipv4:
#   - dhcp:
#       client_id: GigabitEthernet0/0
#       hostname: test.com
#   name: GigabitEthernet0/1
# - ipv4:
#   - address: 198.51.100.1/24
#     secondary: true
#   - address: 198.51.100.2/24
#   ipv6:
#   - address: 2001:db8:0:3::/64
#   name: GigabitEthernet0/2

返回值

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

描述

after

字典

模块执行后的最终配置。

返回值:发生更改时

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

before

字典

模块执行前的配置。

返回值:当 state 为 mergedreplacedoverriddendeletedpurged

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

commands

列表 / 元素=字符串

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

返回值:当 state 为 mergedreplacedoverriddendeletedpurged

示例: ["ip address 192.168.0.3 255.255.255.0", "ipv6 address dhcp rapid-commit", "ipv6 address fd5d:12c9:2201:1::1/64 anycast"]

gathered

列表 / 元素=字符串

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

返回值:当 state 为 gathered

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

parsed

列表 / 元素=字符串

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

返回值:当 state 为 parsed

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

rendered

列表 / 元素=字符串

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

返回值:当 state 为 rendered

示例: ["ipv6 address FD5D:12C9:2201:1::1/64", "ip address 192.168.0.3 255.255.255.0", "ip address autoconfig"]

作者

  • Sagar Paul (@KB-perByte)

  • Sumit Jaiswal (@justjais)