Thứ Ba, 21 tháng 10, 2025

Ansible Cheat Sheet

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ệnhMô tảVí dụ
ansible [host-group] -m [module] -a "[args]"Chạy lệnh ad-hoc trên nhóm hostansible webservers -m ping
ansible [host-group] -m shell -a "[command]"Thực thi lệnh shellansible all -m shell -a "uptime"
ansible-playbook [playbook.yml]Chạy playbookansible-playbook site.yml
ansible-playbook [playbook.yml] --syntax-checkKiểm tra cú pháp playbook
ansible-playbook [playbook.yml] -CChạy thử (dry run) mà không áp dụng thay đổi
ansible-inventory --listHiển thị danh sách inventory
ansible-doc -lLiệt kê tất cả module
ansible-doc [module]Xem tài liệu của moduleansible-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ệnhMô 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)

ModuleMô tảVí dụ
fileQuản lý file/thư mụcfile: path=/etc/test state=directory mode=0755
copySao chép file từ local sang remotecopy: src=config.conf dest=/etc/app/config.conf
packageCài đặt gói phần mềm (đa nền tảng)package: name=vim state=present
serviceQuản lý dịch vụservice: name=apache2 state=restarted
templateSao chép file với biến Jinja2template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
commandChạy lệnh không qua shellcommand: 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ồnMô tả
InventoryBiến trong file inventory (ví dụ: ansible_user)
PlaybookBiến trong vars hoặc vars_files
FactsThông tin hệ thống tự động thu thập (ví dụ: {{ ansible_os_family }})
Extra VarsBiế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ệnhMô 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ảmansible-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ốiansible 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.
=============================
Website không chứa bất kỳ quảng cáo nào, mọi đóng góp để duy trì phát triển cho website (donation) xin vui lòng gửi về STK 90.2142.8888 - Ngân hàng Vietcombank Thăng Long - TRAN VAN BINH
=============================
Nếu bạn không muốn bị AI thay thế và tiết kiệm 3-5 NĂM trên con đường trở thành DBA chuyên nghiệp hay làm chủ Database thì hãy đăng ký ngay KHOÁ HỌC ORACLE DATABASE A-Z ENTERPRISE, được Coaching trực tiếp từ tôi với toàn bộ bí kíp thực chiến, thủ tục, quy trình của gần 20 năm kinh nghiệm (mà bạn sẽ KHÔNG THỂ tìm kiếm trên Internet/Google) từ đó giúp bạn dễ dàng quản trị mọi hệ thống Core tại Việt Nam và trên thế giới, đỗ OCP.
- CÁCH ĐĂNG KÝ: Gõ (.) hoặc để lại số điện thoại hoặc inbox https://m.me/tranvanbinh.vn hoặc Hotline/Zalo 090.29.12.888
- Chi tiết tham khảo:
https://bit.ly/oaz_w
=============================
2 khóa học online qua video giúp bạn nhanh chóng có những kiến thức nền tảng về Linux, Oracle, học mọi nơi, chỉ cần có Internet/4G:
- Oracle cơ bản: https://bit.ly/admin_1200
- Linux: https://bit.ly/linux_1200
=============================
KẾT NỐI VỚI CHUYÊN GIA TRẦN VĂN BÌNH:
📧 Mail: binhoracle@gmail.com
☎️ Mobile/Zalo: 0902912888
👨 Facebook: https://www.facebook.com/BinhOracleMaster
👨 Inbox Messenger: https://m.me/101036604657441 (profile)
👨 Fanpage: https://www.facebook.com/tranvanbinh.vn
👨 Inbox Fanpage: https://m.me/tranvanbinh.vn
👨👩 Group FB: https://www.facebook.com/groups/DBAVietNam
👨 Website: https://www.tranvanbinh.vn
👨 Blogger: https://tranvanbinhmaster.blogspot.com
🎬 Youtube: https://www.youtube.com/@binhguru
👨 Tiktok: https://www.tiktok.com/@binhguru
👨 Linkin: https://www.linkedin.com/in/binhoracle
👨 Twitter: https://twitter.com/binhguru
👨 Podcast: https://www.podbean.com/pu/pbblog-eskre-5f82d6
👨 Địa chỉ: Tòa nhà Sun Square - 21 Lê Đức Thọ - Phường Mỹ Đình 1 - Quận Nam Từ Liêm - TP.Hà Nội

=============================
cơ sở dữ liệu, cơ sở dữ liệu quốc gia, database, AI, trí tuệ nhân tạo, artificial intelligence, machine learning, deep learning, LLM, ChatGPT, DeepSeek, Grok, oracle tutorial, học oracle database, Tự học Oracle, Tài liệu Oracle 12c tiếng Việt, Hướng dẫn sử dụng Oracle Database, Oracle SQL cơ bản, Oracle SQL là gì, Khóa học Oracle Hà Nội, Học chứng chỉ Oracle ở đầu, Khóa học Oracle online,sql tutorial, khóa học pl/sql tutorial, học dba, học dba ở việt nam, khóa học dba, khóa học dba sql, tài liệu học dba oracle, Khóa học Oracle online, học oracle sql, học oracle ở đâu tphcm, học oracle bắt đầu từ đâu, học oracle ở hà nội, oracle database tutorial, oracle database 12c, oracle database là gì, oracle database 11g, oracle download, oracle database 19c/21c/23c/23ai, oracle dba tutorial, oracle tunning, sql tunning , oracle 12c, oracle multitenant, Container Databases (CDB), Pluggable Databases (PDB), oracle cloud, oracle security, oracle fga, audit_trail,oracle RAC, ASM, oracle dataguard, oracle goldengate, mview, oracle exadata, oracle oca, oracle ocp, oracle ocm , oracle weblogic, postgresql tutorial, mysql tutorial, mariadb tutorial, ms sql server tutorial, nosql, mongodb tutorial, oci, cloud, middleware tutorial, docker, k8s, micro service, hoc solaris tutorial, hoc linux tutorial, hoc aix tutorial, unix tutorial, securecrt, xshell, mobaxterm, putty

ĐỌC NHIỀU

Trần Văn Bình - Oracle Database Master