Quản lý và triển khai tự động hạ tầng mạng dùng Ansible
Sơ đồ mạng:
Mô tả:
Các bước thực hiện:
Cấu hình SSH Router
Cấu hình SSH Switch
Truy cập vào Ansible Server bằng trình duyệt web 
Giao diện chính của Ansible Server

Từ giao diện chính, vào Credential để thiết lập mã SSH

Sau khi vào Credentials, bấm vào icon '+' để thêm Username/Password SSH
Để thêm thiết bị vào Inventory, từ giao diện chính vào Inventory để tiến hành thêm thiết bị
Giao diện Inventory

Bấm vào icon '+' [IMG]file:///C:/Users/pmkdr/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png[/IMG] -> inventory để thêm thiết bị vào

Ở mục [1] nhập tên Inventory để quản lý, mục [2] chọn nhóm cho Inventory ở đây mặc định để default. Mục [3] khai báo các thông tin cơ bản của các thiết bị
Giải thích:
Sau khi khai báo xong vào mục [4] để khai báo IP và Enable Password của thiết bị

Ở đây chúng ta khai báo như sau:
Giải thích:
Với thiết bị Switch cũng cấu hình tương tự. Sau khi cấu hình xong trong Inventory/Devices sẽ có 2 mục thiết bị như sau.

Tiếp theo, để tạo Playbook trong project ta vào mục project

Bấm vào icon '+' để thêm Project mới vào

Ở đây cần điền vào các thông số sau:
Vào Job templates để tạo các tác vụ thực hiện Playbook

Bấm vào icon '+' -> Job templates để thêm Job mới [IMG]file:///C:/Users/pmkdr/AppData/Local/Temp/msohtmlclip1/01/clip_image002.png[/IMG]
Ở đây cần điền các mục sau:
Nội dụng file vlan.yml
Nội dung file router.yml
Khi chạy file vlan.yml thành công
Vì Router không khởi tạo Vlan được nên báo lỗi
Kết quả khi chạy file Router.yml
Khi có chỉnh sửa trên Github và muốn đồng bộ ngay với Ansible Server ta vào Project nhấn vào icon như dưới đây
Các bài viết liên quan:
Sơ đồ mạng:

Mô tả:
- Sơ đồ gồm 1 Router, 1 Switch và 1 PC được kết nối vào Ansible Server. Yêu cầu các thiết bị ping thông tới Ansible Server.
- Máy tính của học viên có thể truy cập vào Ansible Server thông qua trình duyệt web.
- Học viên truy cập vào Ansible Server để:
- Thêm thiết bị vào Inventory.
- Thêm credential để xác thực thiết bị.
- Thêm Playbook vào project/jobs để đẩy cấu hình xuống thiết bị.
- Chạy thử playbook thêm vlan cho Switch và thiết lập cổng cho Router.
Các bước thực hiện:
Cấu hình SSH Router
Code:
R# configure terminal R(config)# username admin password 123 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
Code:
SW# configure terminal 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

Giao diện chính của Ansible Server

Từ giao diện chính, vào Credential để thiết lập mã SSH

Sau khi vào Credentials, bấm vào icon '+' để thêm Username/Password SSH

- Name: đặt tên để quản lý mật khẩu SSH
- Credential Type: vì phương thức xác thực là SSH nên chúng ta chọn “Machine”
- Username/Password: tên đăng nhập và mật khẩu SSH của thiết bị
Để thêm thiết bị vào Inventory, từ giao diện chính vào Inventory để tiến hành thêm thiết bị

Giao diện Inventory

Bấm vào icon '+' [IMG]file:///C:/Users/pmkdr/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png[/IMG] -> inventory để thêm thiết bị vào

Ở mục [1] nhập tên Inventory để quản lý, mục [2] chọn nhóm cho Inventory ở đây mặc định để default. Mục [3] khai báo các thông tin cơ bản của các thiết bị
Code:
--- ansible_connection: network_cli ansible_become: 'yes' ansible_become_method: enable ansible_network_os: ios
- 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
Sau khi khai báo xong vào mục [4] để khai báo IP và Enable Password của thiết bị

Ở đây chúng ta khai báo như sau:
- Host name: tên host để quản lý
- Variable: ( lưu ý: bắt đầu với dấu ‘---'
Code:
--- ansible_host: '192.168.1.30 ansible_become_password: '123'
- Ansible_host: địa chỉ IP của thiết bị dùng để SSH
- Ansible_become_password: là enable password của thiết bị
Với thiết bị Switch cũng cấu hình tương tự. Sau khi cấu hình xong trong Inventory/Devices sẽ có 2 mục thiết bị như sau.

Tiếp theo, để tạo Playbook trong project ta vào mục project

Bấm vào icon '+' để thêm Project mới vào

Ở đây cần điền vào các thông số sau:
- Name: tên để quản lý các Project
- SCM type: vì Playbook để trên github nên chúng ta sẽ chọn Git
- SCM URL: địa chỉ URL của github chứa các Playbook
- Tích vào Update Revision on launch để update các project trước khi thực thi các job
Vào Job templates để tạo các tác vụ thực hiện Playbook

Bấm vào icon '+' -> Job templates để thêm Job mới [IMG]file:///C:/Users/pmkdr/AppData/Local/Temp/msohtmlclip1/01/clip_image002.png[/IMG]

Ở đây cần điền các mục sau:
- Name: tên Job để quản lý
- Job type: để thực thi file Playbook chúng ta chọn Run
- Inventory: chọn thiết bị để thực thi Playbook
- Project: chọn Project chứa các Playbook để thực thi
- Playbook: các Playbook được chứa trong Github có đuôi yml

Nội dụng file vlan.yml
Code:
--- - name: tao vlan hosts: SW gather_facts: no tasks: - name: Create vlan ios_vlan: vlan_id: "{{ item.number }}" name: "{{ item.name }}" state: present with_items: - { number: 11, name: "phong_nhan_su" } - { number: 20, name: "phong_ke_toan" } - { number: 30, name: "phong_kinh_doanh" } - name: Show vlan ios_command: commands: - show vlan brief register: show_vlan - debug: var=show_vlan.stdout_lines
Code:
--- - 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/0 IPv4 address ios_l3_interface: name: fastEthernet0/0 ipv4: 192.168.10.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 - 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
- Credentials: chọn tài khoản và mật khẩu xác thực SSH cho thiết bị
Khi chạy file vlan.yml thành công

Vì Router không khởi tạo Vlan được nên báo lỗi
Kết quả khi chạy file Router.yml

Khi có chỉnh sửa trên Github và muốn đồng bộ ngay với Ansible Server ta vào Project nhấn vào icon như dưới đây

Các bài viết liên quan: