Ansible là công cụ tự động hóa mã nguồn mở, dùng để cấu hình hệ thống, triển khai ứng dụng, và quản lý cơ sở hạ tầng qua SSH mà không cần agent.
Lệnh cơ bản (Basic Commands)
Lệnh |
Mô tả |
Ví dụ |
ansible [host-group] -m [module] -a "[args]" |
Chạy lệnh ad-hoc trên nhóm host |
ansible webservers -m ping |
ansible [host-group] -m shell -a "[command]" |
Thực thi lệnh shell |
ansible all -m shell -a "uptime" |
ansible-playbook [playbook.yml] |
Chạy playbook |
ansible-playbook site.yml |
ansible-playbook [playbook.yml] --syntax-check |
Kiểm tra cú pháp playbook |
– |
ansible-playbook [playbook.yml] -C |
Chạy thử (dry run) mà không áp dụng thay đổi |
– |
ansible-inventory --list |
Hiển thị danh sách inventory |
– |
ansible-doc -l |
Liệt kê tất cả module |
– |
ansible-doc [module] |
Xem tài liệu của module |
ansible-doc file |
Inventory (Quản lý host)
File Inventory cơ bản (ansible/hosts)
[webservers]
web1.example.com ansible_user=admin
web2.example.com ansible_user=admin ansible_port=2222
[dbservers]
db1.example.com ansible_user=dbadmin
[all:vars]
ansible_python_interpreter=/usr/bin/python3
Lệnh Inventory nâng cao
Lệnh |
Mô tả |
ansible [pattern] -i [inventory] |
Chỉ định file inventory tùy chỉnh (ví dụ: -i hosts.yml ) |
ansible all -m ping --limit [subset] |
Giới hạn chạy trên tập con host (ví dụ: --limit webservers ) |
Playbook cơ bản (Basic Playbook)
Ví dụ Playbook
---
- name: Cài đặt và cấu hình Nginx
hosts: webservers
become: yes
tasks:
- name: Cài đặt Nginx
apt:
name: nginx
state: present
- name: Đảm bảo Nginx đang chạy
service:
name: nginx
state: started
enabled: yes
Module phổ biến (Common Modules)
Module |
Mô tả |
Ví dụ |
file |
Quản lý file/thư mục |
file: path=/etc/test state=directory mode=0755 |
copy |
Sao chép file từ local sang remote |
copy: src=config.conf dest=/etc/app/config.conf |
package |
Cài đặt gói phần mềm (đa nền tảng) |
package: name=vim state=present |
service |
Quản lý dịch vụ |
service: name=apache2 state=restarted |
template |
Sao chép file với biến Jinja2 |
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf |
command |
Chạy lệnh không qua shell |
command: whoami |
Biến (Variables)
Định nghĩa biến
---
- hosts: all
vars:
app_port: 8080
app_user: "webadmin"
tasks:
- name: Sử dụng biến
debug:
msg: "Cổng ứng dụng là {{ app_port }}"
Nguồn biến
Nguồn |
Mô tả |
Inventory |
Biến trong file inventory (ví dụ: ansible_user ) |
Playbook |
Biến trong vars hoặc vars_files |
Facts |
Thông tin hệ thống tự động thu thập (ví dụ: {{ ansible_os_family }} ) |
Extra Vars |
Biến truyền qua dòng lệnh (-e "key=value" ) |
Playbook nâng cao (Advanced Playbook)
Điều kiện (Conditionals)
- name: Cài gói theo OS
hosts: all
tasks:
- name: Cài Apache trên Debian
apt:
name: apache2
state: present
when: ansible_os_family == "Debian"
- name: Cài httpd trên RedHat
yum:
name: httpd
state: present
when: ansible_os_family == "RedHat"
Vòng lặp (Loops)
- name: Cài nhiều gói
hosts: webservers
tasks:
- name: Cài danh sách gói
package:
name: "{{ item }}"
state: present
loop:
- nginx
- curl
- vim
Xử lý lỗi (Error Handling)
- name: Thử cài gói
hosts: all
tasks:
- name: Cài gói không tồn tại
package:
name: nonexistent-package
state: present
ignore_errors: yes
- name: Thông báo nếu thất bại
debug:
msg: "Cài gói thất bại nhưng tiếp tục"
Role (Tổ chức Playbook)
Cấu trúc Role
roles/
webserver/
tasks/
main.yml
templates/
nginx.conf.j2
vars/
main.yml
Sử dụng Role
---
- hosts: webservers
roles:
- webserver
Lệnh nâng cao (Advanced Commands)
Lệnh |
Mô tả |
Ví dụ |
ansible-playbook [playbook] --vault-password-file [file] |
Chạy playbook với file mật khẩu vault |
--vault-password-file ~/.vault_pass |
ansible-vault encrypt [file] |
Mã hóa file chứa dữ liệu nhạy cảm |
ansible-vault encrypt secrets.yml |
ansible-playbook [playbook] --tags [tag] |
Chạy task theo tag |
--tags "install" |
ansible-playbook [playbook] --skip-tags [tag] |
Bỏ qua task có tag |
--skip-tags "debug" |
Mẹo và xử lý sự cố
- Kiểm tra kết nối:
ansible all -m ping
.
- Debug: Thêm
-vvv
vào lệnh để xem chi tiết lỗi.
- Tối ưu: Dùng
--forks [n]
để chạy song song trên nhiều host.
- Vault: Mã hóa biến nhạy cảm bằng
ansible-vault
.
Tải xuống cheat sheet