Ansible Collections: Library ของ Modules Roles Plugins

Ansible Collections คือ packaging format ที่รวม modules, plugins, roles และ playbooks ไว้ในหน่วยเดียว — เปลี่ยนวิธีที่ Ansible community แจกจ่ายและจัดการ content ก่อนหน้านี้ modules ทุกตัวต้องอยู่ใน Ansible core แต่ด้วย Collections สามารถแยก develop, version, และ distribute modules ได้อิสระ

บทความนี้อธิบาย Collections คืออะไร, ความแตกต่างจาก Roles, namespace และ collection name structure, collection ที่สำคัญใน community, วิธีใช้ modules จาก collections ใน playbook, และการเลือก collection ที่เหมาะสมสำหรับแต่ละ use case

Collections คืออะไร

Collections เป็น packaging format ที่ Ansible แนะนำตั้งแต่เวอร์ชัน 2.9 — ใช้แทน roles สำหรับกลุ่ม functionality ที่มีหลาย modules และ plugins เช่น community.postgresql มี modules สำหรับ manage PostgreSQL ครบชุดในที่เดียว

# โครงสร้าง Ansible Collections
# Namespace.Collection_name / ตัวอย่าง: community.general

collection/
├── MANIFEST.json          # metadata ของ collection
├── FILES.json             # checksums
├── README.md
├── docs/                  # documentation
├── galaxy.yml             # collection info (name, version, author)
├── plugins/
│   ├── modules/           # custom modules (.py files)
│   │   ├── my_module.py
│   │   └── another_module.py
│   ├── module_utils/      # shared utilities for modules
│   ├── inventory/         # dynamic inventory plugins
│   ├── callback/          # output formatting plugins
│   ├── filter/            # Jinja2 filter plugins
│   └── lookup/            # lookup plugins
├── roles/                 # roles ที่อยู่ใน collection
│   └── my_role/
└── playbooks/             # sample playbooks

Collections vs Roles — ความแตกต่าง

Roles และ Collections แก้ปัญหาต่างกัน — ใช้ roles สำหรับ task workflows (install/configure software), ใช้ collections เพื่อเพิ่ม modules และ plugins ที่ไม่มีใน Ansible core

# Roles — task workflow สำหรับ configure software
roles/
  nginx/
    tasks/main.yml     # install nginx, config files, start service
    handlers/main.yml
    templates/*.j2

# Collections — เพิ่ม modules/plugins ใหม่
collections/
  community.postgresql/
    plugins/modules/
      postgresql_db.py      # create/drop database
      postgresql_user.py    # manage users
      postgresql_privs.py   # manage privileges

# ตัวอย่างการใช้งานในทางปฏิบัติ:
# ใช้ role: geerlingguy.postgresql  เพื่อ install และ config PostgreSQL server
# ใช้ collection: community.postgresql  เพื่อ manage databases/users ภายใน PostgreSQL

Namespace และ Collection Name

Collections ใช้ naming convention namespace.collection — namespace บ่งบอกถึงผู้ดูแล เช่น ansible.* ดูแลโดย Red Hat/Ansible, community.* ดูแลโดย community

# ตัวอย่าง namespace ที่สำคัญ

# ansible.* — Red Hat/Ansible ดูแล (มาพร้อม ansible-core)
ansible.builtin    # modules พื้นฐาน: copy, template, service, file, etc.
ansible.posix      # POSIX modules: mount, firewalld, selinux
ansible.netcommon  # network modules พื้นฐาน
ansible.utils      # utility modules สำหรับ filtering/validation

# community.* — community ดูแล (ต้อง install เพิ่ม)
community.general     # รวม modules หลากหลาย (cloud, system, db, etc.)
community.postgresql  # PostgreSQL management
community.docker      # Docker container management
community.mysql       # MySQL/MariaDB management
community.mongodb     # MongoDB management
community.crypto      # TLS/SSL certificate management
community.aws         # AWS (ทางเลือกจาก amazon.aws)

# amazon.* — Amazon/AWS ดูแล
amazon.aws            # AWS modules อย่างเป็นทางการ

# kubernetes.*
kubernetes.core       # Kubernetes modules (เดิมคือ community.kubernetes)

# cisco.*, f5networks.*, paloaltonetworks.*
# — vendor-specific network device modules

Collections ที่ใช้บ่อยใน Infrastructure

Collections หลักที่ใช้ใน infrastructure management ประจำวัน — แต่ละ collection มี modules เฉพาะทางที่ Ansible core ไม่มีให้

# community.general — Swiss Army Knife
# มี modules มากที่สุด รวมทุกอย่างที่ไม่มีที่อื่น
- community.general.ufw           # Ubuntu Firewall management
- community.general.snap          # Snap package management
- community.general.htpasswd      # Apache htpasswd files
- community.general.supervisorctl # Supervisor process control
- community.general.discord       # Discord notifications
- community.general.slack         # Slack notifications (ทางเลือก)

# community.postgresql — PostgreSQL
- community.postgresql.postgresql_db      # Create/drop databases
- community.postgresql.postgresql_user    # Manage users/roles
- community.postgresql.postgresql_privs   # Manage privileges
- community.postgresql.postgresql_query   # Run SQL queries

# community.docker — Docker
- community.docker.docker_container   # Manage containers
- community.docker.docker_image       # Pull/build/tag images
- community.docker.docker_network     # Manage networks
- community.docker.docker_volume      # Manage volumes
- community.docker.docker_compose_v2  # Docker Compose

# community.crypto — TLS/Certificates
- community.crypto.openssl_certificate    # Manage SSL certs
- community.crypto.openssl_privatekey    # Generate private keys
- community.crypto.acme_certificate      # Let's Encrypt (ACME)

# ansible.posix — POSIX/Linux
- ansible.posix.firewalld   # FirewallD (RHEL/CentOS)
- ansible.posix.mount       # Mount filesystems
- ansible.posix.selinux     # SELinux management
- ansible.posix.authorized_key  # SSH authorized_keys (ทางเลือก)

ใช้ Collection Modules ใน Playbook

Module ใน collection ต้องเรียกด้วย FQCN (Fully Qualified Collection Name) — ระบุชัดเจนว่า module นั้นมาจาก collection ไหน ป้องกัน name conflict ระหว่าง modules ที่ชื่อเหมือนกันใน collection ต่างกัน

---
# playbook ที่ใช้หลาย collections
- name: Setup web application stack
  hosts: appservers
  become: true

  tasks:
    # community.postgresql — จัดการ database
    - name: Create application database
      community.postgresql.postgresql_db:
        name: myapp
        encoding: UTF-8
        state: present

    - name: Create database user
      community.postgresql.postgresql_user:
        name: myapp_user
        password: "{{ db_password }}"
        state: present

    - name: Grant privileges
      community.postgresql.postgresql_privs:
        database: myapp
        roles: myapp_user
        privs: ALL
        type: database

    # community.crypto — SSL certificate
    - name: Generate SSL private key
      community.crypto.openssl_privatekey:
        path: /etc/ssl/private/myapp.key
        size: 4096

    # ansible.posix — firewall
    - name: Allow HTTPS through firewall
      ansible.posix.firewalld:
        service: https
        permanent: true
        state: enabled
        immediate: true

    # community.general — notifications
    - name: Notify Slack on success
      community.general.slack:
        token: "{{ slack_token }}"
        msg: "Deployment complete on {{ inventory_hostname }}"
        channel: "#deployments"

ansible.builtin — Modules พื้นฐาน

ansible.builtin คือ collection ที่มากับ ansible-core ทุก module พื้นฐานอยู่ใน namespace นี้ — สามารถเรียกแบบ short name (เช่น copy) หรือ FQCN ก็ได้ แต่ใน project จริงแนะนำใช้ FQCN เพื่อความชัดเจน

# ansible.builtin modules ที่ใช้บ่อย

# File operations
ansible.builtin.copy          # copy files (= copy)
ansible.builtin.template      # template files (= template)
ansible.builtin.file          # manage file/directory permissions (= file)
ansible.builtin.stat          # get file info (= stat)
ansible.builtin.lineinfile    # edit file lines (= lineinfile)
ansible.builtin.replace       # replace text in files (= replace)
ansible.builtin.fetch         # fetch files from remote (= fetch)
ansible.builtin.unarchive     # extract archives (= unarchive)

# Package management
ansible.builtin.apt           # Debian/Ubuntu packages (= apt)
ansible.builtin.yum           # RHEL/CentOS packages (= yum)
ansible.builtin.package       # Generic package manager (= package)

# Service management
ansible.builtin.service       # manage services (= service)
ansible.builtin.systemd       # manage systemd (= systemd)

# User management
ansible.builtin.user          # manage users (= user)
ansible.builtin.group         # manage groups (= group)

# Execution
ansible.builtin.command       # run commands (= command)
ansible.builtin.shell         # run shell commands (= shell)
ansible.builtin.script        # run scripts (= script)

เลือก Collection ที่เหมาะสม

บางกรณีมีหลาย collections ที่ทำสิ่งเดียวกันได้ — ต้องเลือกให้เหมาะกับ use case และ requirement

# กรณี: จัดการ MySQL/MariaDB
# ตัวเลือก: community.mysql vs ansible.builtin.command

# ✅ แนะนำ: community.mysql (idempotent, structured)
- name: Create MySQL database
  community.mysql.mysql_db:
    name: mydb
    state: present

# ❌ หลีกเลี่ยง: command/shell (ไม่ idempotent, ต้องเขียน conditions เอง)
- name: Create MySQL database
  ansible.builtin.command: mysql -e "CREATE DATABASE mydb;"
  # ปัญหา: รัน 2 ครั้งจะ error "Database already exists"

# กรณี: Docker management
# ตัวเลือก: community.docker vs ansible.builtin.command

# ✅ แนะนำ: community.docker (ตรวจสอบ state อัตโนมัติ)
- name: Start nginx container
  community.docker.docker_container:
    name: mynginx
    image: nginx:latest
    state: started
    ports:
      - "80:80"

# กรณี: SSL Certificates
# ตัวเลือก: community.crypto vs shell openssl

# ✅ แนะนำ: community.crypto (idempotent, ไม่ overwrite cert ที่ยังใช้ได้)
- name: Generate certificate
  community.crypto.x509_certificate:
    path: /etc/ssl/cert.pem
    privatekey_path: /etc/ssl/key.pem
    provider: selfsigned

Collection ใน requirements.yml

ระบุ collections ที่ project ต้องการใน requirements.yml เสมอ — pin version เพื่อ reproducible deployments และไม่ให้ collection update อัตโนมัติทำให้ playbook พัง

# requirements.yml — collections section
collections:
  - name: community.general
    version: ">=9.0.0"

  - name: community.postgresql
    version: "3.4.0"

  - name: community.docker
    version: "3.10.0"

  - name: community.crypto
    version: "2.19.0"

  - name: ansible.posix
    version: "1.5.4"

  - name: community.mysql
    version: "3.10.0"

# ติดตั้ง collections ทั้งหมด
ansible-galaxy collection install -r requirements.yml

# ติดตั้งเฉพาะ collections (ไม่รวม roles)
ansible-galaxy collection install -r requirements.yml

# ติดตั้ง collection เดี่ยว
ansible-galaxy collection install community.postgresql:3.4.0

# ดู collections ที่ติดตั้งแล้ว
ansible-galaxy collection list

สรุป

Ansible Collections ขยายความสามารถของ Ansible ด้วย modules และ plugins นอกเหนือจาก core — แทนที่จะใช้ command/shell modules สำหรับทุกอย่าง ควรตรวจสอบว่ามี collection module ที่ idempotent และออกแบบมาสำหรับ task นั้นโดยเฉพาะหรือไม่

หลักการเลือก: ใช้ ansible.builtin สำหรับ operations พื้นฐาน (file, package, service, user), ใช้ community.postgresql / community.mysql สำหรับ database management, community.docker สำหรับ container management และ ansible.posix สำหรับ POSIX-specific operations บน Linux — ระบุ collections ทั้งหมดใน requirements.yml พร้อม version ที่ชัดเจนเสมอ