Ansible Galaxy: ค้นหา Download Reusable Roles จาก Community

Ansible Galaxy คือ repository สาธารณะที่รวบรวม Roles และ Collections จาก community นับพันรายการ — แทนที่จะเขียน role สำหรับ install nginx, postgresql หรือ redis เอง สามารถดาวน์โหลด role ที่ผ่านการทดสอบมาแล้วจาก Galaxy ได้ทันที ทำให้ประหยัดเวลาและได้ role ที่รองรับหลาย OS

บทความนี้ครอบคลุมการค้นหาและประเมิน roles บน galaxy.ansible.com, การติดตั้งด้วย ansible-galaxy install, การใช้ requirements.yml สำหรับ project dependencies, ความแตกต่างระหว่าง Roles และ Collections, การจัดการ Galaxy roles ที่ติดตั้งแล้ว และ best practices สำหรับการใช้ Galaxy ใน production

Ansible Galaxy คืออะไร

galaxy.ansible.com คือ platform ที่ community นำ roles และ collections มาแบ่งปัน — แบ่งเป็น 2 ประเภทหลัก:

# 2 ประเภทหลักบน Ansible Galaxy

# 1. Roles — task collections สำหรับ configure/install software เดียว
#    ตัวอย่าง: geerlingguy.nginx, geerlingguy.mysql, geerlingguy.redis
#    ใช้ใน: roles: section ของ Playbook

# 2. Collections — bundle ของ modules, plugins, roles และ playbooks
#    ตัวอย่าง: community.general, ansible.posix, community.postgresql
#    ใช้ใน: modules ใน tasks (community.postgresql.postgresql_db)

# ติดตั้งด้วยคำสั่งต่างกัน:
ansible-galaxy role install geerlingguy.nginx         # ติดตั้ง role
ansible-galaxy collection install community.general   # ติดตั้ง collection

ค้นหา Role บน Galaxy

ค้นหา roles ได้ 2 วิธี — ผ่าน website galaxy.ansible.com หรือ CLI ด้วย ansible-galaxy search

# ค้นหาด้วย CLI
ansible-galaxy search nginx
ansible-galaxy search nginx --author geerlingguy
ansible-galaxy search postgresql --platforms Ubuntu

# ดูรายละเอียด role
ansible-galaxy info geerlingguy.nginx

# Output ตัวอย่าง:
# Role: geerlingguy.nginx
# Description: Nginx installation for Linux
# Author: geerlingguy
# Downloads: 12,000,000+
# Stars: 1,200+
# Min Ansible Version: 2.8
# Platforms: Ubuntu, Debian, RedHat, CentOS, Fedora

ประเมินคุณภาพ Role ก่อนใช้งาน

ไม่ใช่ทุก role บน Galaxy มีคุณภาพเท่ากัน — ควรตรวจสอบปัจจัยเหล่านี้ก่อนเลือกใช้ใน project:

# ปัจจัยที่ควรตรวจสอบก่อนใช้ Galaxy role

# 1. Downloads & Stars — บ่งบอก popularity และ community trust
#    ควรมี: downloads > 100,000 หรือ stars > 100 สำหรับ popular software

# 2. Last Commit Date — บ่งบอก active maintenance
#    ควร: updated ภายใน 1-2 ปีที่ผ่านมา

# 3. Platform Support — รองรับ OS ที่ต้องการ
#    ตรวจสอบ: platforms list ใน meta/main.yml

# 4. Ansible Version Compatibility
#    ตรวจสอบ: min_ansible_version ใน meta/main.yml

# 5. CI/CD Status — มี automated tests หรือไม่
#    ดีกว่าถ้ามี: GitHub Actions หรือ Molecule tests

# 6. GitHub Issues — มี open bugs ที่เกี่ยวกับ version ที่ใช้หรือไม่

# Roles ที่แนะนำสำหรับ infrastructure ทั่วไป:
# geerlingguy.nginx, geerlingguy.apache, geerlingguy.mysql
# geerlingguy.postgresql, geerlingguy.redis, geerlingguy.docker
# geerlingguy.java, geerlingguy.nodejs, geerlingguy.certbot

ติดตั้ง Roles ด้วย ansible-galaxy install

ติดตั้ง role เดียวหรือหลาย roles พร้อมกัน — roles จะถูกติดตั้งใน ~/.ansible/roles เป็น default หรือระบุ path เฉพาะได้

# ติดตั้ง role เดียว
ansible-galaxy install geerlingguy.nginx

# ติดตั้ง version เฉพาะ (version pinning)
ansible-galaxy install geerlingguy.nginx,3.2.0

# ติดตั้งหลาย roles พร้อมกัน
ansible-galaxy install geerlingguy.nginx geerlingguy.postgresql geerlingguy.redis

# ติดตั้งลงใน project directory แทน default path
ansible-galaxy install geerlingguy.nginx -p roles/

# ติดตั้งใหม่ทับของเดิม
ansible-galaxy install geerlingguy.nginx --force

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

# ลบ role
ansible-galaxy remove geerlingguy.nginx

requirements.yml — จัดการ Dependencies แบบ Reproducible

requirements.yml คือไฟล์ที่ list roles และ collections ที่ project ต้องการ — commit ไว้ใน git เพื่อให้ทีมทุกคนใช้ versions เดียวกัน และรัน ansible-galaxy install -r requirements.yml เพื่อ install ครั้งเดียว

# requirements.yml — complete example
---
roles:
  # ระบุ version เสมอเพื่อ reproducible deployments
  - name: geerlingguy.nginx
    version: "3.2.0"

  - name: geerlingguy.postgresql
    version: "3.4.0"

  - name: geerlingguy.redis
    version: "1.9.0"

  - name: geerlingguy.certbot
    version: "5.1.0"

  # ติดตั้งจาก GitHub repo โดยตรง (สำหรับ private/custom roles)
  - name: myteam.common
    src: https://github.com/myteam/ansible-role-common
    version: "v1.2.0"
    scm: git

  # ติดตั้งจาก local path (สำหรับ internal roles)
  - name: internal_security
    src: /opt/ansible-roles/security

collections:
  # Collections สำหรับ modules เพิ่มเติม
  - name: community.general
    version: ">=7.0.0"

  - name: ansible.posix
    version: "1.5.4"

  - name: community.postgresql
    version: "3.2.0"

  - name: community.docker
    version: "3.4.0"
# คำสั่งติดตั้งจาก requirements.yml

# ติดตั้งทุก roles และ collections
ansible-galaxy install -r requirements.yml

# ติดตั้ง roles เท่านั้น
ansible-galaxy role install -r requirements.yml

# ติดตั้ง collections เท่านั้น
ansible-galaxy collection install -r requirements.yml

# ติดตั้งลงใน project directory
ansible-galaxy install -r requirements.yml -p roles/

# Force reinstall ทุกตัว
ansible-galaxy install -r requirements.yml --force

Ansible Collections — Module Bundle

Collections คือ packaging format ที่รวม modules, plugins, roles และ playbooks ไว้ด้วยกัน — ทำให้ใช้ modules เพิ่มเติมที่ไม่มีใน Ansible core ได้ เช่น community.postgresql สำหรับ manage PostgreSQL databases โดยเฉพาะ

# ตัวอย่างการใช้ Collection modules ใน tasks

# community.postgresql — manage PostgreSQL
- name: Create database
  community.postgresql.postgresql_db:
    name: myapp_db
    encoding: UTF-8
    state: present

- name: Create user
  community.postgresql.postgresql_user:
    name: myapp_user
    password: "{{ db_password }}"
    priv: "myapp_db.*:ALL"

# community.docker — manage Docker containers
- name: Pull image
  community.docker.docker_image:
    name: nginx:latest
    source: pull

- name: Run container
  community.docker.docker_container:
    name: mynginx
    image: nginx:latest
    ports:
      - "80:80"
    state: started

# ansible.posix — POSIX-specific modules
- name: Mount filesystem
  ansible.posix.mount:
    path: /data
    src: /dev/sdb1
    fstype: ext4
    state: mounted

ใช้ Galaxy Role ใน Playbook

หลัง install roles แล้ว ใช้งานใน Playbook เหมือน local roles ทุกอย่าง — override defaults ของ Galaxy role ผ่าน vars ใน Playbook หรือ group_vars ตาม environment

---
- name: Setup LEMP stack using Galaxy roles
  hosts: webservers
  become: true

  vars:
    # geerlingguy.nginx settings
    nginx_worker_processes: auto
    nginx_worker_connections: 1024
    nginx_remove_default_vhost: true
    nginx_vhosts:
      - listen: "80"
        server_name: "{{ inventory_hostname }}"
        root: /var/www/html
        index: index.php index.html
        extra_parameters: |
          location ~ \.php$ {
              fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
              include fastcgi_params;
          }

    # geerlingguy.mysql settings
    mysql_root_password: "{{ vault_mysql_root_password }}"
    mysql_databases:
      - name: appdb
        encoding: utf8mb4
    mysql_users:
      - name: appuser
        password: "{{ vault_appuser_password }}"
        priv: "appdb.*:ALL"

    # geerlingguy.php settings
    php_version: "8.2"
    php_packages:
      - php8.2-fpm
      - php8.2-mysql
      - php8.2-curl
      - php8.2-gd

  roles:
    - role: geerlingguy.mysql
    - role: geerlingguy.php
    - role: geerlingguy.nginx

ansible.cfg — กำหนด Roles Path

กำหนด roles path ใน ansible.cfg เพื่อให้ Ansible หา roles ได้ถูก path — รองรับหลาย paths คั่นด้วย :

# ansible.cfg
[defaults]
# Ansible หา roles จาก paths เหล่านี้ตามลำดับ
roles_path = roles:~/.ansible/roles:/usr/share/ansible/roles

# Galaxy settings
galaxy_server = https://galaxy.ansible.com

[galaxy]
# Default timeout สำหรับ galaxy API calls
server_timeout = 60
# project structure แนะนำ
myproject/
├── ansible.cfg           # กำหนด roles_path = roles
├── requirements.yml      # Galaxy dependencies (commit ใน git)
├── site.yml              # Main playbook
├── inventory/
│   ├── production/
│   └── staging/
├── group_vars/
│   ├── all.yml
│   └── webservers.yml
├── host_vars/
└── roles/                # local roles (Galaxy roles ติดตั้งที่นี่ด้วย -p roles/)
    └── myapp/

สรุป

Ansible Galaxy เป็น ecosystem ที่ช่วยประหยัดเวลาพัฒนา role สำหรับ infrastructure ทั่วไปได้อย่างมาก — ก่อนเขียน role เองควรตรวจสอบ Galaxy ก่อนเสมอ โดยเฉพาะ roles ของ geerlingguy ที่ดูแลและทดสอบอย่างดีและรองรับ OS หลัก

Pattern ที่ควรจำ: ใช้ requirements.yml เสมอและ pin version ทุก role เพื่อ reproducible deployments, commit requirements.yml ไว้ใน git พร้อม Playbook, ติดตั้ง Galaxy roles ลงใน roles/ directory ของ project แทน ~/.ansible/roles เพื่อ isolation, และเลือก role โดยดู downloads, stars, last update และ platform support ก่อนใช้งาน