Ansible Galaxy và Collections
Tới giờ ta dùng module dạng ansible.builtin.dnf mà chưa giải thích phần ansible.builtin. Đó là một collection. Từ Ansible 2.10, collection là đơn vị đóng gói và phân phối trung tâm — gom module, role, plugin lại với nhau. Hiểu collection là kiến thức "nâng cao" bắt buộc với Ansible hiện đại.
Vì sao có collection
Trước 2.10, mọi module nằm chung trong một gói Ansible khổng lồ — khó bảo trì, phát hành chậm, ai cũng phụ thuộc một lịch release. Collection tách nó ra: mỗi nhà cung cấp/cộng đồng đóng gói module + role + plugin của mình thành một collection riêng, phát hành độc lập.
Một collection gom:
<namespace>.<collection>
├── modules/ các module (vd amazon.aws.ec2_instance)
├── roles/ các role
├── plugins/ filter/lookup/callback plugins (Bài 12)
└── playbooks/ playbook mẫu
Ví dụ: amazon.aws (module quản lý AWS), community.general (module cộng đồng đủ loại), ansible.posix (công cụ POSIX). ansible.builtin là collection lõi luôn có sẵn — chứa dnf, copy, service, template...
FQCN: gọi module rõ ràng
FQCN (Fully Qualified Collection Name) là cách gọi đầy đủ: namespace.collection.module. Ví dụ:
ansible.builtin.dnf namespace=ansible, collection=builtin, module=dnf
amazon.aws.ec2_instance tạo EC2
community.general.timezone đặt timezone
Vì sao dùng FQCN thay vì tên ngắn (dnf)?
- Rõ ràng: biết module đến từ collection nào.
- Tránh trùng tên: hai collection có thể có module cùng tên; FQCN chỉ đích danh.
- Là best practice được khuyến nghị (Bài 15). Tên ngắn vẫn chạy (Ansible tự đoán), nhưng FQCN an toàn hơn, nhất là trong role/collection chia sẻ.
ansible-core vs ansible: gói nào chứa gì
Đây là điểm hay nhầm khi cài (Bài 2):
ansible-core— chỉ phần lõi: engine +ansible.builtin. Gọn.ansible(gói đầy đủ) —ansible-corecộng một bộ collection được tuyển chọn (community.general, amazon.aws, ansible.posix...). Đây là lý do sau khipip install ansible, bạn đã có sẵn nhiều collection:
ansible-galaxy collection list
Collection Version
amazon.aws 10.1.2
ansible.posix 2.1.0
community.general 11.4.2
community.general chẳng hạn có tới ~575 module (ansible-doc -l community.general). Nếu cài ansible-core, bạn phải tự cài thêm collection cần.
Cài collection từ Galaxy
Ansible Galaxy (galaxy.ansible.com) là kho cộng đồng chứa collection và role. Cài một collection:
ansible-galaxy collection install community.general
Nhưng cách chuyên nghiệp là khai báo trong requirements.yml (đưa vào Git để tái lập môi trường):
collections:
- name: community.general
- name: amazon.aws
version: ">=10.0.0"
roles:
- name: geerlingguy.docker # role từ Galaxy
Rồi:
ansible-galaxy install -r requirements.yml # cả role lẫn collection
ansible-galaxy collection install -r requirements.yml
Nothing to do. All requested collections are already installed.
requirements.yml là best practice (Bài 15): mọi người trong team cài đúng cùng phiên bản collection/role, môi trường tái lập được — giống package.json/requirements.txt của ngôn ngữ khác.
Doanh nghiệp còn có Automation Hub (collection được Red Hat chứng nhận/hỗ trợ) bên cạnh Galaxy (cộng đồng). Cùng cơ chế, khác nguồn và mức bảo đảm.
Dùng collection trong playbook
Sau khi cài, dùng module/role của collection qua FQCN:
- hosts: localhost
tasks:
- name: Tạo EC2 (module từ amazon.aws)
amazon.aws.ec2_instance:
name: web
instance_type: t2.micro
image_id: ami-xxxx
- name: Đặt timezone (module từ community.general)
community.general.timezone:
name: Asia/Ho_Chi_Minh
Thú vị: nhớ inventory động AWS ở Bài 3 (amazon.aws.aws_ec2) — chính là một plugin từ collection amazon.aws. Và Ansible dùng được để tạo cả hạ tầng cloud (không chỉ cấu hình), qua các module như amazon.aws.ec2_instance.
Tự tạo collection
Như tạo role (Bài 8), bạn scaffold một collection riêng để đóng gói module/role/plugin nội bộ công ty:
ansible-galaxy collection init mycompany.infra
Nó dựng cấu trúc mycompany/infra/ với plugins/, roles/, galaxy.yml (metadata). Build thành file .tar.gz (ansible-galaxy collection build) rồi publish lên Galaxy/Automation Hub hoặc cài nội bộ. Đây là cách chia sẻ tự động hóa ở quy mô tổ chức — gom custom module (Bài 11) và plugin (Bài 12) của bạn vào một collection dùng chung.
🧹 Dọn dẹp
requirements.yml mẫu ở repo nghiadaulau/ansible-series, thư mục 09-collections.
Tổng kết
Collection là đơn vị đóng gói hiện đại của Ansible (từ 2.10): gom module, role, plugin dưới một namespace (amazon.aws, community.general, ansible.builtin). Gọi qua FQCN (namespace.collection.module) cho rõ ràng và tránh trùng — best practice. ansible-core chỉ có lõi; gói ansible kèm sẵn nhiều collection. Cài từ Galaxy bằng ansible-galaxy collection install, khai báo trong requirements.yml để tái lập. Tự tạo collection bằng ansible-galaxy collection init để đóng gói tự động hóa nội bộ.
Trước khi tự viết module/plugin (Bài 11, 12), ta giải quyết một nhu cầu thực tế mọi dự án đều có: quản lý bí mật (mật khẩu, khóa API) an toàn. Bài 10: Ansible Vault.