• 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 CI/CD sử dụng Ansible cấu hình static route

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

  • LAB CI/CD sử dụng Ansible cấu hình static route



    Click image for larger version

Name:	image.png
Views:	22
Size:	28.5 KB
ID:	427771

    Yêu cầu:
    1, Cấu hình SSH cho thiết bị , đảm bảo máy chạy Ansible kết nối được đến với Router 1 và Router 2.
    Lưu ý: cổng e0/0 của mỗi thiết bị cấu hình địa chỉ ip động ( dhcp ).
    2, Tạo file inventory để kết nối đến thiết bị.
    3, Học viên sử dụng host_vars hoặc group_vars để lưu trữ các giá trị như IP, netmask , …
    4, Viết file playbook cấu hình các yêu cầu sau :
    • Cấu hình IP các cổng như hình vẽ.
    • Cấu hình static route để 2 PC A và PC B thấy nhau ( sử dụng module ios_static_route ).
    • Ping kiểm tra kết nối thành công từ e0/1 của R2 đến R1 ( sử dụng module ios_ping ).
    5, Viết file .gitlab-ci.yml thực hiện tự động tiến trình khi có thay đổi cấu hình.
    6, Học viên đặt IP cho PC A và PC B , đảm bảo ping thành công từ PC A đến PC B.
    Hướng dẫn thực hiện:

    1.Cấu hình SSH thiết bị:
    *LƯU Ý: Kiểm tra cấu hình IP nhận từ DHCP và SSH. Nếu chưa có cấu hình có thể tham khảo bên dưới.
    enable
    configure terminal
    hostname R-2
    interface e0/0
    ip address dhcp
    no shut
    exit
    username vnpro password vnpro#123
    ip domain-name chiendev89.net
    crypto key generate rsa
    1024
    ip ssh authentication-retries 3
    line vty 0 4
    transport input ssh
    login local
    exit
    enable password vnpro#321
    do wr

    2.Tạo project trên server Gitlab server:
    • Sử dụng file hướng dẫn CI/CD gitlab
    3.Tạo inventory sử dụng host_vars và group_vars
    Tạo file inventory:
    • Khai báo group R chứa các Router
    • Khai báo tên và địa chỉ IP của từng router trong group
    • Khai báo biến cho group R
    nano host
    [R]
    R1 ansible_host=10.215.27.141
    R2 ansible_host=10.215.27.193
    [R:vars]
    ansible_become_password=vnpro#321
    ansible_user=vnpro
    ansible_password=vnpro#123
    ansible_connection=network_cli
    ansible_become="yes"
    ansible_become_method="enable"
    ansible_network_os=ios

    4.Viết file cấu hình:


    Bật interface:
    - name: enable int
    ios_interfaces:
    config:
    - name: "{{ item.name }}"
    enabled: yes
    with_items:
    - { name: Ethernet0/1 }
    - { name: Ethernet0/2 }

    Đặt địa chỉ IP cho Interface:
    - name: set ip
    ios_l3_interfaces:
    config:
    - name: "{{ item.name }}"
    ipv4:
    - address: "{{ item.ip }}"
    state: merged
    with_items:
    - { name: Ethernet0/1 ,ip: 192.168.1.1/24, host: R1 }
    - { name: Ethernet0/2 ,ip: 192.168.12.1/24, host: R1 }
    - { name: Ethernet0/1 ,ip: 192.168.2.1/24, host: R2 }
    - { name: Ethernet0/2 ,ip: 192.168.12.2/24, host: R2 }
    when: inventory_hostname == item.host​​​​​​​

    Cấu hình static route
    Sử dụng module cisco.ios.ios_static_routes
    Cài đặt module cisco.ios
    ansible-galaxy collection install cisco.ios
    Link tham khảo: https://docs.ansible.com/ansible/latest/collections/cisco/ios/index.html

    - name: config static route
    cisco.ios.ios_static_routes:
    config:
    - address_families:
    - afi: ipv4
    routes:
    - dest: "{{ item.prefix }}"
    next_hops:
    - forward_router_address: "{{ item.next_hop }}"
    with_items:
    - { prefix: 192.168.2.0/24 , next_hop: 192.168.12.2 , host: R1 }
    - { prefix: 192.168.1.0/24 , next_hop: 192.168.12.1 , host: R2 }
    when: inventory_hostname == item.host​​​​​​​

    Show cấu hình
    - name: show route
    ios_command:
    commands:
    - show ip route
    register: show_route
    - debug: var=show_route.stdout_lines

    File playbook hoàn chỉnh: setrouter.yml
    ---
    - name: set ip
    hosts: R
    gather_facts: no
    tasks:
    - name: enable int
    ios_interfaces:
    config:
    - name: "{{ item.name }}"
    enabled: yes
    with_items:
    - { name: Ethernet0/1 }
    - { name: Ethernet0/2 }
    - name: set ip
    ios_l3_interfaces:
    config:
    - name: "{{ item.name }}"
    ipv4:
    - address: "{{ item.ip }}"
    state: merged
    with_items:
    - { name: Ethernet0/1 ,ip: 192.168.1.1/24, host: R1 }
    - { name: Ethernet0/2 ,ip: 192.168.12.1/24, host: R1 }
    - { name: Ethernet0/1 ,ip: 192.168.2.1/24, host: R2 }
    - { name: Ethernet0/2 ,ip: 192.168.12.2/24, host: R2 }
    when: inventory_hostname == item.host

    - name: config static route
    cisco.ios.ios_static_routes:
    config:
    - address_families:
    - afi: ipv4
    routes:
    - dest: "{{ item.prefix }}"
    next_hops:
    - forward_router_address: "{{ item.next_hop }}"
    with_items:
    - { prefix: 192.168.2.0/24 , next_hop: 192.168.12.2 , host: R1 }
    - { prefix: 192.168.1.0/24 , next_hop: 192.168.12.1 , host: R2 }
    when: inventory_hostname == item.host
    - name: show route
    ios_command:
    commands:
    - show ip route
    register: show_route
    - debug: var=show_route.stdout_lines

    5.Viết file .gitlab-ci.yml---
    image: hacmieu89/ansible
    stages:
    - run
    job1:
    stage: run
    script:
    - rm -rf /etc/ansible
    - mkdir -pv /etc/ansible
    - cp -rf * /etc/ansible
    - export PATH="/root/.local/bin:$PATH"
    - ansible-galaxy collection install cisco.ios
    - ansible-playbook -i hosts setrouter.yml

    6.Upfile file trong folder lên gitlab:
    ​​​​​​​
    Click image for larger version

Name:	image.png
Views:	32
Size:	27.7 KB
ID:	427770​​
    git add .
    git commit -m "config router"
    git push

    Email : vnpro@vnpro.org
    ---------------------------------------------------------------------------------------------------------------
Trung Tâm Tin Học VnPro
149/1D Ung Văn Khiêm P25 Q.Bình thạnh TPHCM
Tel : (08) 35124257 (5 lines)
Fax: (08) 35124314

Home page: http://www.vnpro.vn
Support Forum: http://www.vnpro.org
- Chuyên đào tạo quản trị mạng và hạ tầng Internet
- Phát hành sách chuyên môn
- Tư vấn và tuyển dụng nhân sự IT
- Tư vấn thiết kế và hỗ trợ kỹ thuật hệ thống mạng

Network channel: http://www.dancisco.com
Blog: http://www.vnpro.org/blog
Working...
X