• If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.
Xin chào ! Nếu đây là lần đầu tiên bạn đến với diễn đàn, xin vui lòng danh ra một phút bấm vào đây để đăng kí và tham gia thảo luận cùng VnPro.

Announcement

Collapse
No announcement yet.

Lab: Quản lý và triển khai tự động hạ tầng mạng dùng Ansible (Phần 1)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lab: Quản lý và triển khai tự động hạ tầng mạng dùng Ansible (Phần 1)

    Sơ đồ mạng:

    Click image for larger version

Name:	Lab Quản lý và triển khai tự động hạ tầng mạng dùng Ansible 01.jpg
Views:	80
Size:	78.5 KB
ID:	425040

    Mô tả:
    • Sơ đồ gồm 1 Router, 2 Distributed Switch, 2 Access Switch, 2 PC cho 2 Vlan, được kết nối với Ansible Server. Mô hình thực hiện trên Linux OS và các thiết bị ảo.
    • Máy tính của học viên có kết nối mạng bên trong VnPro
    Yêu cầu:
    • Thực hiện các công việc sau:
      • Thêm thiết bị vào file hosts – file lưu IP các thiết bị
      • Tạo playbook YAML để thực hiện các cấu hình tự động cho Router, 2 DS (Distributed Switch), 2 AS(Access Switch).
      • Chạy các playbook trên.
    Các bước thực hiện:
    Cấu hình SSH Router


R# configure terminal
R(config)# username admin password 123
R(config)# ip domain-name vnpro
R(config)# enable password 321
R(config)# crypto key generate rsa
1024
R(config)#line vty 0 4
R(config-line)# password 123
R(config-line)# login local
Cấu hình SSH cho 4 Switch
SW# configure terminal
SW(config)# ip domain-name vnpro
SW(config)# username admin password 123
SW(config)# enable password 321
SW(config)# crypto key generate rsa
1024
SW(config)#line vty 0 4
SW(config-line)# password 123
SW(config-line)# login local
Cài đặt Ansible trên Linux OS:
Chạy 3 dòng lệnh sau:


sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible



Chuyển tới thư mục đã cài ansible và sửa đổi file hosts – file chứa các thiết bị

cd /etc/ansible
sudo nano hosts



ta sẽ thêm các thiết bị vào đây: Thêm IP và password để SSH đến, username và password.

Click image for larger version

Name:	Lab Quản lý và triển khai tự động hạ tầng mạng dùng Ansible 02.jpg
Views:	54
Size:	42.1 KB
ID:	425041

Giải thích:
  • Ansible_host: địa chỉ IP của thiết bị dùng để SSH
  • Ansible_become_password: là enable password của thiết bị
  • Ansible_connection: network_cli là phương thức kết nối đến thiết bị thông qua SSH
  • Ansible_become: ‘yes’ và ansible_become_method: enable là cho phép ansible có thể vào privilege mode trước khi thực thi các task
  • Ansible_network_os: ios ở đây do dùng các thiết bị của Cisco nên khai báo như vậy
Lưu ý: thứ tự khai báo có thể thay đổi mà không ảnh hưởng đến kết quả
Sau khi xong nhấn Ctr + X, sau đó Y và enter để lưu file lại.
Tạo các file Playbook:
Tạo file playbook bằng câu lệnh sau: sudo nano <filename>.yml. Nên tham khảo thêm các viết file YAML
Ví dụ một số lệnh sử dụng trên thiết bị Cisco IOS để viết các nhiệm vụ trong playbook bằng ngôn ngữ YAML, chạy trên Ansible server

Ios_command: thực hiện khi thiết bị ở mode Privileged

tasks:
- name: chạy lên show version
ios_command:
commands: show version

- name: chạy lệnh show version bao gồm IOS
ios_command:
commands: show version
wait_for: result[0] contains IOS

- name: chạy nhiều lệnh
ios_command:
commands:
- show version
- show interfaces

- name: chạy lệnh yêu cầu trả lời prompt
ios_command:
commands:
- command: 'clearcountersGigabitEthernet0/1'
prompt: 'Clear"showinterface"countersonthisinterface\[confirm\]'
answer: 'y'
- command: 'clearcountersGigabitEthernet0/2'
prompt: '[confirm]'
answer: "\r"
Ios_config: thực hiện khi thiết bị ở mode Configuration
- name: cấu hình hostname trong inventory
ios_config:
lines: hostname {{ inventory_hostname }}

- name: cấu hình cổng
ios_config:
lines:
- description test interface
- ip address 172.31.1.1 255.255.255.0
parents: interface Ethernet1

- name: cấu hình ip helper-address trên nhiều cổng
ios_config:
lines:
- ip helper-address 172.26.1.10
- ip helper-address 172.26.3.8
parents: "{{ item }}"
with_items:
- interface Ethernet1
- interface Ethernet2
- interface GigabitEthernet1
Ios_interface: thực hiện trên cổng
- name: cấu hình cổng
ios_interface:
name: GigabitEthernet0/2
description: test-interface
speed: 100
duplex: half
mtu: 512

- name: xóa cổng looback
ios_interface:
name: Loopback9
state: absent

- name: mở cổng(up)
ios_interface:
name: GigabitEthernet0/2
enabled: True

- name: tắt cổng(down)
ios_interface:
name: GigabitEthernet0/2
enabled: False
Ios_vlan: thực hiện trên vlan
- name: Tạo vlan
ios_vlan:
vlan_id: 100
name: test-vlan
state: present

- name: Thêm cổng vào vlan
ios_vlan:
vlan_id: 100
interfaces:
- GigabitEthernet0/0
- GigabitEthernet0/1

- name: Xóa vlan
ios_vlan:
vlan_id: 100
state: absent

- name: Thêm vlan dùng aggregate
ios_vlan:
aggregate:
- { vlan_id: 100, name: test-vlan, interfaces: [GigabitEthernet0/1, GigabitEthernet0/2], delay: 15, state: suspend }
- { vlan_id: 101, name: test-vlan, interfaces: GigabitEthernet0/3 }

File để cấu hình cho Router:
sudo nano R.yml

---
- name: dat ip cho loobpack
hosts: Router # tên của thiết bị trong Ansible server( vì ở đây chỉ có router nên có thể để all)
# các tác vụ
tasks:
- name: Set loopback IPv4 address # Đặt tên task để quản lý
ios_l3_interface: # do cấu hình router nên bắt buộc khai báo như vậy
name: loopback 2 # tên cổng
ipv4: 10.0.0.1/24 # địa chỉ IP của loopback
- name: Set fastEthernet0/1 IPv4 address # đặt IP cho cổng
ios_l3_interface:
name: fastEthernet1/0 # tên cổng
ipv4: 172.16.13.1/24 # địa chỉ IP cho cổng
- name: Set fastEthernet1/0 IPv4 address
ios_l3_interface:
name: fastEthernet0/1
ipv4: 172.16.12.1/24
#Xem lại tất cả các cổng
- name: Show ip
ios_command: # Viết lệnh để gửi ở mode command line của router
commands:
- show ip int brief
register: show_ip # gắn kết quả vào biến show_ip
- name: set OSPF # cài đặt OSPF
ios_config:
parents: # vào mode config của lệnh dưới
- router ospf 1
lines: # các lệnh sẽ thực hiện
- router-id 1.1.1.1
- name: set ip OSPF f0/1
ios_config:
parents:
- int f0/1
lines:
- no shut
- ip ospf 1 area 0
- name: set ip OSPF f1/0
ios_config:
parents:
- int f1/0
lines:
- no shut
- ip ospf 1 area 0
- debug: var=show_ip.stdout_lines # debug: in ra màn hình, đặt biến var(lấy thông tin của các dòng từ show_ip gắn vào),in biến var
File để cấu hình 2 Access Switch

sudo nano AS.yml

---
- name: AS1
hosts: AS1
gather_facts: no
tasks:
- name: tao vlan
ios_vlan:
aggregate:
- { vlan_id : 10 }
- name: show vlan
ios_command:
commands:
- show vlan
register: show_ip
- name: config
ios_config:
lines:
- spanning-tree mode rapid-pvst
- name: chuyen cong e0/1 - e0/2 sang mode trunk
ios_config:
parents:
- int range e0/1-2
lines:
- sw trunk en dot1Q
- sw mode trunk
- name: sw acc vlan 10
ios_config:
parents:
- int e3/1
lines:
- sw acc vlan 10
- name: sw acc vlan 20
ios_config:
parents:
- int e3/2
lines:
- sw acc vlan 20
- name: sw acc vlan 30
ios_config:
parents:
- int e3/3
lines:
- sw acc vlan 30
- debug: var=show_ip.stdout_lines
#
- name: AS2
hosts: AS2
gather_facts: no
tasks:
- name: tao vlan
ios_vlan:
aggregate:
- { vlan_id : 20 }
- name: Show vlan
ios_command:
commands:
- show vlan
register: show_ip
- name: config
ios_config:
lines:
- spanning-tree mode rapid-pvst
- name: chuyen cong e0/1 - e0/2 sang mode trunk
ios_config:
parents:
- int range e0/1-2
lines:
- sw trunk en dot1Q
- sw mode trunk
- name: sw acc vlan 10
ios_config:
parents:
- int e3/1
lines:
- sw acc vlan 10
- name: sw acc vlan 20
ios_config:
parents:
- int e3/2
lines:
- sw acc vlan 20
- name: sw acc vlan 30
ios_config:
parents:
- int e3/3
lines:
- sw acc vlan 30
- debug: var=show_ip.stdout_lines
















Tags:

Working...
X