Monday, October 7, 2024

[Ansible] Lesson 17 – cisco.ios.ios_l2_interfaces module – Resource Module to configure L2 interfaces

-

cisco.ios.ios_l3_interfaces là một trong các module trong Ansible Core Network Platform và thuộc nhóm các module dùng để quản lý các tài nguyên mạng. Module này cho phép cấu hình các interface lớp 3 trên thiết bị mạng Cisco IOS.

Các tính năng chính của cisco.ios.ios_l3_interfaces module bao gồm:

  • Tạo hoặc xóa các interface lớp 3 trên thiết bị mạng Cisco IOS.
  • Cấu hình các thuộc tính cho các interface lớp 3 như địa chỉ IP, subnet mask, default gateway, authentication, access control list (ACL), …

Module cisco.ios.ios_l3_interfaces sử dụng kết nối SSH để truy cập vào thiết bị Cisco IOS và thực hiện các thao tác cấu hình. Các thông tin xác thực được cung cấp thông qua biến ansible_connection, ansible_user, và ansible_password.

Để biết thêm chi tiết về cách sử dụng cisco.ios.ios_l3_interfaces module, có thể tham khảo tài liệu chính thức của Ansible.

Đây là danh sách các tham số của module cisco.ios.ios_l3_interfaces:

  • aggregate: cho phép bạn tổng hợp tất cả các giao diện vào một cấu hình và gửi cho thiết bị. Giá trị mặc định là ‘no’.
  • description: mô tả tên cho giao diện. Văn bản tùy chọn.
  • duplex: chế độ song song hoặc đơn lẻ cho giao diện. Các giá trị có thể là ‘auto’, ‘full’ hoặc ‘half’. Giá trị mặc định là ‘auto’.
  • ipv4: thông tin về địa chỉ IPv4 cho giao diện.
  • ipv6: thông tin về địa chỉ IPv6 cho giao diện.
  • mtu: đặt kích thước gói tin tối đa (MTU) cho giao diện. Giá trị mặc định là 1500.
  • name: tên giao diện.
  • provider: kết nối đến thiết bị Cisco IOS sử dụng các tham số nhà cung cấp như ‘host’, ‘port’, ‘username’, ‘password’ và ‘transport’.
  • shutdown: chuyển đổi giao diện giữa ‘shutdown’ và ‘no shutdown’. Giá trị mặc định là ‘no’.
  • speed: tốc độ cho giao diện. Các giá trị có thể là ‘auto’, ’10’, ‘100’, ‘1000’, ‘2500’, ‘5000’, ‘10000’, ‘25000’, ‘40000’, ‘50000’, ‘100000’. Giá trị mặc định là ‘auto’.
  • state: trạng thái của giao diện. Các giá trị có thể là ‘present’ hoặc ‘absent’. Giá trị mặc định là ‘present’.

Để đặt IP cho 3 interface và thêm mô tả (description), chúng ta có thể sử dụng module cisco.ios.ios_l3_interfaces. Đầu tiên, ta sẽ tạo 2 file YAML để chứa các biến và các task cần thiết.

Trong thư mục vars, tạo file interfaces.yml và đặt các biến cần thiết cho các interface cần đặt IP như sau:

---
interfaces:
  - name: GigabitEthernet1
    description: Server interface
    ipv4_address: 192.0.2.1
    ipv4_netmask: 255.255.255.0
  - name: GigabitEthernet2
    description: Workstation interface
    ipv4_address: 192.0.2.2
    ipv4_netmask: 255.255.255.0
  - name: GigabitEthernet3
    description: Printer interface
    ipv4_address: 192.0.2.3
    ipv4_netmask: 255.255.255.0

Sau đó, trong thư mục tasks, tạo file configure_interfaces.yml và sử dụng module cisco.ios.ios_l3_interfaces để đặt IP và mô tả cho các interface như sau:

---
- name: Configure interfaces
  hosts: cisco_ios
  gather_facts: no
  vars_files:
    - vars/interfaces.yml
  tasks:
    - name: Set interface IP and description
      cisco.ios.ios_l3_interfaces:
        name: "{{ item.name }}"
        description: "{{ item.description }}"
        ipv4:
          address: "{{ item.ipv4_address }}"
          netmask: "{{ item.ipv4_netmask }}"
      with_items: "{{ interfaces }}"

Cuối cùng, để chạy playbook, ta sử dụng lệnh:

ansible-playbook -i inventory.ini configure_interfaces.yml

Trong đó, inventory.ini là file chứa các thông tin về host được cấu hình.

Tham khảo https://docs.ansible.com/ansible/latest/collections/cisco/ios/ios_l3_interfaces_module.html#ansible-collections-cisco-ios-ios-l3-interfaces-module

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,956FansLike
256FollowersFollow
223SubscribersSubscribe
spot_img

Related Stories