[Ansible] Phần 2 - Playbook

1058
15-07-2017
[Ansible] Phần 2 - Playbook

Phần 1 Ansible - Cấu hình và cài đặtBizfly Cloud giới thiệu một số command để thực thi một task trên hệ thống. Ansible hỗ trợ người sử dụng nhiều hơn bằng cách đưa vào khái niệm Playbook. 

Về Playbook

Playbook thông thường là những tấm bảng ghi sơ đồ vị trí, chiến thuật di chuyển của từng cầu thủ mà các HLV sử dụng để truyền đạt cho cầu thủ (nếu bạn xem đá bóng). Ví dụ như Công Phượng đá hộ công, Huy Toàn đột phá cánh trái hay Quế Ngọc Hải đá hậu vệ.

Ansible sử dụng khái niệm Playbook

Ansible sử dụng khái niệm Playbook cũng mang ý nghĩa tương tự. Việc triển khai hệ thống mới cũng tương tự như sơ đồ chiến thuật, các group [webservers], [databases] cũng như tuyến tiền đạo, hậu vệ, các dịch vụ cũng như kết nối tới các thành phần khác nhau trong hệ thống của từng server cũng giống nhiệm vụ của một cầu thủ trên sân.

Ansible Playbook được viết theo cấu trúc YAML, một cú pháp dễ đọc, dễ viết hơn so với JSON, XML. Dưới đây là ví dụ về YAML:

--- # An employee record name: Example Developer   job: Developer   skill: Elite   employed: True   foods:       - Apple     - Orange     - Strawberry     - Mango languages:       ruby: Elite     python: Elite     dotnet: Lam 

Xem thêm về YAML syntax. Bắt đầu file yaml bằng ---. Sử dụng indent là 2 space.

Ví dụ

Đây là file webservers.yml, 1 playbook cho dịch vụ apache. Playbook này có 1 play, dùng cho hệ điều hành Redhat, CentOS

--- - hosts: webservers   vars:     http_port: 80     max_clients: 200   tasks:   - name: ensure apache is at the latest version     yum: pkg=httpd state=latest   - name: write the apache config file     template: src=/srv/httpd.j2 dest=/etc/httpd.conf     notify:     - restart apache   - name: ensure apache is running (and enable it at boot)     service: name=httpd state=started enabled=yes   handlers:     - name: restart apache       service: name=httpd state=restarted 

Giải thích:

  • hosts: xác định đối tượng sẽ thực thi playbook này.
  • vars: các biến dùn trong play, trong ví dụ này các biến sẽ được dùng để cấu hình apache
  • tasks: liệt kê các task cần thực hiện
  • name: tên của task
  • yum, template, service: các module sử dụng
  • notify: giống như trigger, để gọi đến 1 task khác khi task hiện tại thực hiện thành công.
  • handlers: khai báo các task notify

1 play gồm danh sách các task được thực thi theo thứ tự từ trên xuống. Nếu xảy ra lỗi ở task nào thì host đang thực thi sẽ bị dừng lại mà không ảnh hưởng đến các host khác.

Có thể viết tách dòng với các tham số truyền vào module

  - name: ensure apache is at the latest version     yum:       pkg: httpd       state: latest 

Chạy playbook:

$ ansible-playbook webserver.yml  

Ví dụ 1 playbook có nhiều play:

--- - hosts: webservers   tasks:   - name: ensure apache is at the latest version     yum: pkg=httpd state=latest   - name: write the apache config file     template: src=/srv/httpd.j2 dest=/etc/httpd.conf  - hosts: databases   tasks:   - name: ensure postgresql is at the latest version     yum: name=postgresql state=latest   - name: ensure that postgresql is started     service: name=postgresql state=running 

Cấu trúc 1 playbooks chuẩn

production                # inventory file for production servers   stage                     # inventory file for stage environment  group_vars/      group1                 # here we assign variables to particular groups    group2                 # "" host_vars/      hostname1              # if systems need specific variables, put them here    hostname2              # ""  library/                  # if any custom modules, put them here (optional)   filter_plugins/           # if any custom filter plugins, put them here (optional)  site.yml                  # master playbook   webservers.yml            # playbook for webserver tier   dbservers.yml             # playbook for dbserver tier  roles/       common/               # this hierarchy represents a "role"         tasks/            #             main.yml      #  <-- tasks file can include smaller files if warranted         handlers/         #             main.yml      #  <-- handlers file         templates/        #  <-- files for use with the template resource             ntp.conf.j2   #  <------- templates end in .j2         files/            #             bar.txt       #  <-- files for use with the copy resource             foo.sh        #  <-- script files for use with the script resource         vars/             #             main.yml      #  <-- variables associated with this role         defaults/         #             main.yml      #  <-- default lower priority variables for this role         meta/             #             main.yml      #  <-- role dependencies      webtier/              # same kind of structure as "common" was above, done for the webtier role     monitoring/           # ""     fooapp/               # "" 

Trong đó:

  • production: giống file /etc/ansible/hosts, liệt kê group, host
  • group_vars/*: đặt các biến chung cho cùng 1 nhóm, ví dụ [webservers] có biến listen_port: 80
  • host_vars/*: đặt các biến riêng cho từng host
  • roles/*: đặt các role, ví dụ các host trong [webservers] gọi đến role webtier

Xem thêm ansible best practices tại link sau: http://docs.ansible.com/ansible/latest/playbooks_best_practices.html

Lợi ích:

  • Tách biệt các hệ thống, 1 hệ thống là 1 project riêng biệt
  • Việc tách role giúp dễ dàng quản lý, phát triển
  • Tái sử dụng tốt, chỉ cần sửa thông tin host, vars
Ví dụ:
lamp_simple/   ├── group_vars │   ├── all │   └── dbservers ├── hosts ├── LICENSE.md ├── README.md ├── roles │   ├── common │   │   ├── handlers │   │   │   └── main.yml │   │   ├── tasks │   │   │   └── main.yml │   │   └── templates │   │       └── ntp.conf.j2 │   ├── db │   │   ├── handlers │   │   │   └── main.yml │   │   ├── tasks │   │   │   └── main.yml │   │   └── templates │   │       └── my.cnf.j2 │   └── web │       ├── handlers │       │   └── main.yml │       ├── tasks │       │   ├── copy_code.yml │       │   ├── install_httpd.yml │       │   └── main.yml │       └── templates │           └── index.php.j2 └── site.yml 

Bạn có thể xem thêm các ví dụ khác tại đây: https://github.com/ansible/ansible-examples

>> Tìm hiểu thêm: [AWK] Phần 1 - Tổng quan

BizFly Cloud là nhà cung cấp dịch vụ điện toán đám mây với chi phí thấp, được vận hành bởi VCCorp.

BizFly Cloud là một trong 4 doanh nghiệp nòng cốt trong "Chiến dịch thúc đẩy chuyển đổi số bằng công nghệ điện toán đám mây Việt Nam" của Bộ TT&TT; đáp ứng đầy đủ toàn bộ tiêu chí, chỉ tiêu kỹ thuật của nền tảng điện toán đám mây phục vụ Chính phủ điện tử/chính quyền điện tử.

Độc giả quan tâm đến các giải pháp của BizFly Cloud có thể truy cập tại đây.

DÙNG THỬ MIỄN PHÍ và NHẬN ƯU ĐÃI 3 THÁNG tại: Manage.bizflycloud

SHARE