Monday, October 7, 2024

[Ansible] Lesson 11 – Thực hành cấu hình cơ bản sử dụng Module ios_config Cisco

-

1. Cài đặt các thành phần hỗ trợ.

Module ios_command là một trong những module của Ansible Network Platform để quản lý các thiết bị mạng Cisco IOS. Module này được cung cấp bởi Ansible, không cần phải cài đặt thêm.

Tuy nhiên, để sử dụng được module này, bạn cần cài đặt các thư viện yêu cầu, bao gồm:

  • ansible
  • ansible-core
  • cisco-ios-stdlib

Bạn có thể cài đặt các thư viện này bằng pip, ví dụ:

pip install ansible ansible-core cisco-ios-stdlib

Sau khi cài đặt, module ios_command sẽ có thể sử dụng được.

2. Cấu hình cơ bản.

Để tiến hành khai báo và cấu hình hostname, thời gian, username, no ip-domain lookup cho 2 thiết bị Cisco 3750, ta có thể sử dụng Ansible và module ios_command để thực hiện. Đầu tiên, ta cần tạo file inventory chứa thông tin về các thiết bị mà ta muốn thực hiện cấu hình. Ví dụ, file inventory.yml như sau:

all:
  hosts:
    switch1:
      ansible_host: 192.168.1.1
      ansible_user: cisco
      ansible_password: cisco
      ansible_connection: network_cli
      ansible_network_os: ios
    switch2:
      ansible_host: 192.168.1.2
      ansible_user: cisco
      ansible_password: cisco
      ansible_connection: network_cli
      ansible_network_os: ios

Trong đó, ta đã khai báo thông tin về 2 thiết bị switch1switch2, bao gồm địa chỉ IP, username, password và các thông số kết nối.

Tiếp theo, ta tạo file playbook config.yml để thực hiện cấu hình trên các thiết bị:

- name: Configure switches
  hosts: all
  gather_facts: no
  tasks:
    - name: Set hostname
      ios_command:
        commands:
          - hostname {{ inventory_hostname }}
      register: output

    - name: Set time zone
      ios_command:
        commands:
          - clock timezone GMT 0
      register: output

    - name: Set username
      ios_command:
        commands:
          - username admin privilege 15 secret cisco
      register: output

    - name: Disable ip-domain lookup
      ios_command:
        commands:
          - no ip domain-lookup
      register: output

Trong playbook này, ta sử dụng module ios_command để thực hiện các lệnh cấu hình trên thiết bị, bao gồm đặt hostname, đặt timezone, tạo user và vô hiệu hóa tính năng ip-domain lookup.

Cuối cùng, để thực hiện playbook trên các thiết bị, ta chạy lệnh:

ansible-playbook -i inventory.yml config.yml

Sau khi thực hiện thành công, các thiết bị sẽ đã được cấu hình như mong muốn.

Nếu bạn chỉ muốn cấu hình cho 1 thiết bị trong file inventory của bạn, bạn có thể sử dụng option -l hoặc --limit khi chạy playbook để chỉ định tên hoặc pattern cho host đó. Ví dụ, nếu tên host của thiết bị switch1 là “switch1”, bạn có thể chạy lệnh:

ansible-playbook -i inventory_file playbook.yml -l switch1

Lưu ý rằng nếu bạn chạy playbook mà không chỉ định bất kỳ giới hạn nào, playbook sẽ được thực thi trên tất cả các host trong inventory của bạn.

3. Đặt IP cho các interface.

Giả sử ta muốn đặt địa chỉ IP cho interface VLAN1 và interface GigabitEthernet1/0/1 trên switch1 và đặt địa chỉ IP cho interface VLAN1 và interface GigabitEthernet1/0/2 trên switch2. Ta có thể sử dụng module ios_config để cấu hình cho các interface này. Dưới đây là ví dụ cấu hình:

- name: Config IP address for interfaces on switches
  hosts: switch1, switch2
  gather_facts: no
  vars:
    vlan1_ip: 192.168.1.1
    ge1_0_1_ip: 10.0.0.1
    ge1_0_2_ip: 10.0.0.2
  tasks:
    - name: Configure IP address for VLAN1 interface
      ios_config:
        lines:
          - ip address {{ vlan1_ip }}/24
        parents: interface Vlan1
      delegate_to: localhost

    - name: Configure IP address for GigabitEthernet1/0/1 interface
      ios_config:
        lines:
          - ip address {{ ge1_0_1_ip }}/24
        parents: interface GigabitEthernet1/0/1
      delegate_to: localhost

    - name: Configure IP address for GigabitEthernet1/0/2 interface
      ios_config:
        lines:
          - ip address {{ ge1_0_2_ip }}/24
        parents: interface GigabitEthernet1/0/2
      delegate_to: localhost

Ansible hỗ trợ tách các biến và cấu hình ra thành các file riêng biệt để dễ quản lý và duy trì.

Bạn có thể tạo một thư mục riêng để chứa các file biến và cấu hình. Sau đó, trong playbook của bạn, sử dụng module như vars_filesinclude_vars để load các biến và cấu hình từ các file tương ứng.

Ví dụ:

- name: Load variables from file
  vars_files:
    - vars/main.yml

- name: Load configuration from file
  include_vars:
    file: config.yml
    name: config

Trong đó, vars_files được sử dụng để load các biến từ file vars/main.yml. include_vars được sử dụng để load cấu hình từ file config.yml và đặt tên cho các biến được load là config.

Lưu ý rằng, trong include_vars, nếu không chỉ định tên biến (name), Ansible sẽ tự động tạo ra các biến với tên dựa trên tên file. Ví dụ, nếu bạn chỉ định file: config.yml, Ansible sẽ tạo ra các biến có tên config.

Để tách biến trong ví dụ trên, chúng ta có thể tạo một file riêng để lưu các biến:

  • Tạo một file mới với tên vars.yml.
  • Di chuyển các biến từ file playbook sang file vars.yml. Ví dụ:
# vars.yml
---
ios_user: admin
ios_pass: password
  • Trong playbook, thay thế các biến bằng {{ variable_name }}. Ví dụ:
# playbook.yml
---
- name: Configure Cisco IOS devices
  hosts: cisco
  gather_facts: no

  vars_files:
    - vars.yml

  tasks:
    - name: Configure hostname and time
      ios_config:
        lines:
          - hostname {{ ios_hostname }}
          - clock timezone {{ ios_timezone }}
        provider:
          host: "{{ inventory_hostname }}"
          username: "{{ ios_user }}"
          password: "{{ ios_pass }}"
          authorize: yes
          auth_pass: "{{ ios_pass }}"
  • Tiếp theo, tạo một file riêng để lưu các config:
# cisco_config.yml
---
hostname: cisco-switch
timezone: EST

interfaces:
  - name: GigabitEthernet1/0/1
    ip: 192.168.1.1
    subnet: 255.255.255.0

  - name: GigabitEthernet1/0/2
    ip: 192.168.2.1
    subnet: 255.255.255.0
  • Thay thế các giá trị config bằng {{ variable_name }}:
# playbook.yml
---
- name: Configure Cisco IOS devices
  hosts: cisco
  gather_facts: no

  vars_files:
    - vars.yml

  tasks:
    - name: Configure hostname and time
      ios_config:
        lines:
          - hostname {{ cisco_config.hostname }}
          - clock timezone {{ cisco_config.timezone }}
        provider:
          host: "{{ inventory_hostname }}"
          username: "{{ ios_user }}"
          password: "{{ ios_pass }}"
          authorize: yes
          auth_pass: "{{ ios_pass }}"

    - name: Configure interfaces
      ios_config:
        lines:
          - interface {{ item.name }}
          - ip address {{ item.ip }} {{ item.subnet }}
        provider:
          host: "{{ inventory_hostname }}"
          username: "{{ ios_user }}"
          password: "{{ ios_pass }}"
          authorize: yes
          auth_pass: "{{ ios_pass }}"
      loop: "{{ cisco_config.interfaces }}"

Lưu ý rằng trong ví dụ trên, chúng ta sử dụng cisco_config.hostname để truy cập giá trị hostname trong file cisco_config.yml. Tương tự, chúng ta sử dụng item.name, item.ip, và item.subnet để truy cập các giá trị trong danh sách interfaces.

4. Tạo Vlan.

4.1. Ví dụ sử dụng biến và config chung file.

Để tạo các VLAN trên thiết bị Cisco 3750, bạn có thể sử dụng module ios_vlan của Ansible. Sau đó, bạn chỉ cần thêm các task tương ứng với từng VLAN cần tạo.

Ví dụ, để tạo VLAN 101 với tên là Devops và địa chỉ IP là 172.16.1.254/24, bạn có thể sử dụng task như sau:

- name: Create VLAN 101 - Devops
  ios_vlan:
    vlan_id: 101
    name: Devops
  register: result

- name: Configure VLAN 101 IP address
  ios_ip_interface:
    name: Vlan101
    ip: 172.16.1.254
    mask: 255.255.255.0
    state: present
  when: result.changed

Tương tự, để tạo VLAN 102 và VLAN 103, bạn có thể sử dụng các task sau:

- name: Create VLAN 102 - System Admin
  ios_vlan:
    vlan_id: 102
    name: "System Admin"
  register: result

- name: Configure VLAN 102 IP address
  ios_ip_interface:
    name: Vlan102
    ip: 172.16.2.254
    mask: 255.255.255.0
    state: present
  when: result.changed

- name: Create VLAN 103 - IT Helpdesk
  ios_vlan:
    vlan_id: 103
    name: "IT Helpdesk"
  register: result

- name: Configure VLAN 103 IP address
  ios_ip_interface:
    name: Vlan103
    ip: 172.16.3.254
    mask: 255.255.255.0
    state: present
  when: result.changed

Lưu ý rằng để sử dụng module ios_vlanios_ip_interface, bạn cần cài đặt cisco.ios collection.

4.2. Ví dụ sử dụng biến và config khác file.

Ví dụ sau đây minh họa cách tách biến và cấu hình thành các file riêng biệt để cấu hình cho thiết bị IOS của Cisco. Các biến được lưu trữ trong file vars.yml, trong khi các file cấu hình VLAN được lưu trữ trong thư mục configs/.

Nội dung của file vars.yml:

---
hostname: switch01
wan_interface_ip_address: 192.168.1.1
wan_interface_net: 255.255.255.0
vlan_configs:
  - id: 101
    name: Devops
    ip_address: 172.16.1.254
    netmask: 255.255.255.0
  - id: 102
    name: System Admin
    ip_address: 172.16.2.254
    netmask: 255.255.255.0
  - id: 103
    name: IT Helpdesk
    ip_address: 172.16.3.254
    netmask: 255.255.255.0

Nội dung của file playbook site.yml:

---
- name: Set hostname
  ios_command:
    commands:
      - hostname {{ hostname }}

- name: Configure WAN interface
  ios_config:
    lines:
      - interface GigabitEthernet1
      - ip address {{ wan_interface_ip_address }} {{ wan_interface_net }}
      - no shutdown

- name: Configure VLANs
  include_vars:
    file: vars.yml
    name: vars
  loop: "{{ vars.vlan_configs }}"
  ios_config:
    lines:
      - vlan {{ item.id }}
      - name {{ item.name }}
      - exit
  loop_control:
    loop_var: vlan

- name: Configure VLAN interfaces
  include_vars:
    file: vars.yml
    name: vars
  loop: "{{ vars.vlan_configs }}"
  ios_config:
    lines:
      - interface Vlan{{ item.id }}
      - ip address {{ item.ip_address }} {{ item.netmask }}
      - no shutdown
      - exit
  loop_control:
    loop_var: vlan

Khi chạy playbook này, Ansible sẽ sử dụng biến được định nghĩa trong file vars.yml và cấu hình thiết bị IOS theo các file cấu hình VLAN được lưu trữ trong thư mục configs/. Các file cấu hình VLAN có thể được đặt tên theo id của VLAN, ví dụ: vlan101.cfg, vlan102.cfg, vlan103.cfg.

5. Static route.

Đây là một ví dụ về cấu hình các định tuyến tĩnh trên thiết bị Cisco IOS, với các mạng siteA, siteB, siteC được chỉ định đến địa chỉ IP gateway là 192.168.1.1:

File biến:

# vars.yml
---
gateway_ip: "192.168.1.1"
site_routes:
  - network: "192.168.101.0"
    netmask: "255.255.255.0"
    name: "siteA"
  - network: "192.168.102.0"
    netmask: "255.255.255.0"
    name: "siteB"
  - network: "192.168.103.0"
    netmask: "255.255.255.0"
    name: "siteC"

File cấu hình playbook:

# configure_routes.yml
---
- name: Configure static routes
  hosts: cisco_ios
  gather_facts: no
  vars_files:
    - vars.yml
  tasks:
    - name: Configure default route
      ios_config:
        lines:
          - ip route 0.0.0.0 0.0.0.0 {{ gateway_ip }} name internet
      become: yes

    - name: Configure site routes
      ios_config:
        lines:
          - ip route {{ item.network }} {{ item.netmask }} {{ gateway_ip }} name {{ item.name }}
      with_items: "{{ site_routes }}"
      become: yes

Ở đây, ta đặt biến gateway_ip cho địa chỉ IP gateway mặc định. Ta cũng định nghĩa một danh sách site_routes, chứa thông tin về mạng của các site và tên tương ứng của chúng.

Trong playbook, ta sử dụng vars_files để load biến từ file vars.yml. Sau đó, ta sử dụng ios_config module để cấu hình các định tuyến tĩnh. Các cấu hình được chỉ định trong lines, và với with_items, ta lặp lại các đối tượng trong danh sách site_routes.

Chú ý rằng trong các lệnh cấu hình, ta sử dụng cú pháp Jinja để sử dụng các giá trị của biến.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,956FansLike
256FollowersFollow
223SubscribersSubscribe
spot_img

Related Stories