ติดตั้ง Ansible Collections จาก Galaxy

Ansible Galaxy คือ hub สาธารณะที่รวบรวม Collections และ Roles จาก community — ก่อนใช้ modules จาก collections อย่าง community.postgresql หรือ community.docker ต้องติดตั้งผ่าน ansible-galaxy ก่อนเสมอ

บทความนี้อธิบายคำสั่งติดตั้ง collections จาก Galaxy, การใช้ requirements.yml สำหรับ team projects, การจัดการ collection path และ version, รวมถึงการติดตั้งจากแหล่งอื่นนอกจาก Galaxy เช่น Private Automation Hub และ Git repository

ติดตั้ง Collection พื้นฐาน

คำสั่งพื้นฐานสำหรับติดตั้ง collection จาก Ansible Galaxy — ระบุชื่อ collection ในรูปแบบ namespace.collection_name พร้อม version ที่ต้องการ

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

# ติดตั้งพร้อมระบุ version
ansible-galaxy collection install community.postgresql:3.4.0

# ติดตั้งหลาย collections พร้อมกัน
ansible-galaxy collection install community.postgresql community.docker community.crypto

# ติดตั้งเวอร์ชันล่าสุดที่ตรงตาม constraint
ansible-galaxy collection install "community.general>=9.0.0"

# ตรวจสอบ collections ที่ติดตั้งแล้ว
ansible-galaxy collection list

# ตรวจสอบ version ของ collection เฉพาะ
ansible-galaxy collection list community.postgresql

Collection Install Path

Collections ติดตั้งได้ 2 ที่หลัก — user-level path (default) และ project-local path ใช้ตามสถานการณ์ที่เหมาะสม

# Default install paths ตาม OS
~/.ansible/collections/                    # user-level (default)
/usr/share/ansible/collections/            # system-level

# ตรวจสอบ paths ที่ Ansible ค้นหา collections
ansible-config dump | grep COLLECTIONS_PATH

# ผลลัพธ์ตัวอย่าง:
# COLLECTIONS_PATHS(/etc/ansible/ansible.cfg) = ['/home/user/.ansible/collections', '/usr/share/ansible/collections']

# ติดตั้งไปยัง project-local path (แนะนำสำหรับ team projects)
ansible-galaxy collection install community.postgresql -p ./collections

# โครงสร้างหลังติดตั้ง project-local:
project/
├── ansible.cfg        # ระบุ collections_path = ./collections
├── collections/
│   └── ansible_collections/
│       └── community/
│           └── postgresql/  ← ติดตั้งที่นี่
├── inventory/
└── playbooks/
# ansible.cfg — กำหนด collections path สำหรับ project
[defaults]
collections_path = ./collections:~/.ansible/collections

# ลำดับการค้นหา: project-local ก่อน จากนั้น user-level

requirements.yml — มาตรฐานสำหรับ Team

ใช้ requirements.yml เป็น single source of truth สำหรับ dependencies ทั้ง collections และ roles — ทุกคนในทีมรัน install คำสั่งเดียวเพื่อได้ environment เหมือนกันทุก version

# requirements.yml — รูปแบบครบถ้วน
---
collections:
  # ระบุ version แน่นอน (แนะนำสำหรับ production)
  - 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"

  # ใช้ version constraint (flexible)
  - name: community.general
    version: ">=9.0.0,<10.0.0"

  # ติดตั้งเวอร์ชันล่าสุดเสมอ (ไม่แนะนำสำหรับ production)
  - name: community.mysql

roles:
  - name: geerlingguy.docker
    version: "7.0.0"

  - name: geerlingguy.postgresql
# ติดตั้งจาก requirements.yml
ansible-galaxy install -r requirements.yml

# ติดตั้งเฉพาะ collections
ansible-galaxy collection install -r requirements.yml

# ติดตั้งเฉพาะ roles
ansible-galaxy role install -r requirements.yml

# Force reinstall (แม้มีอยู่แล้ว)
ansible-galaxy collection install -r requirements.yml --force

# ติดตั้งไปยัง project path พร้อมกัน
ansible-galaxy collection install -r requirements.yml -p ./collections

อัพเดตและลบ Collections

การจัดการ version ของ collections ที่ติดตั้งแล้ว — upgrade, downgrade, หรือลบออกตามความต้องการ

# อัพเดต collection เป็นเวอร์ชันล่าสุด
ansible-galaxy collection install community.postgresql --upgrade

# อัพเดตทุก collection ใน requirements.yml
ansible-galaxy collection install -r requirements.yml --upgrade

# ดู collection info และ changelog
ansible-galaxy collection info community.postgresql

# ดู collections ทั้งหมดพร้อม version
ansible-galaxy collection list

# ลบ collection (ทำด้วย rm เพราะไม่มีคำสั่ง uninstall)
rm -rf ~/.ansible/collections/ansible_collections/community/postgresql

# หรือถ้า install ใน project path
rm -rf ./collections/ansible_collections/community/postgresql

ติดตั้งจากแหล่งอื่น

นอกจาก Ansible Galaxy สามารถติดตั้ง collections จาก Private Automation Hub, Git repository, หรือไฟล์ local — เหมาะสำหรับ collections ที่พัฒนาเองหรือ internal use

# ติดตั้งจาก Git repository
ansible-galaxy collection install git+https://github.com/org/my_collection.git

# ระบุ branch หรือ tag
ansible-galaxy collection install git+https://github.com/org/my_collection.git,main
ansible-galaxy collection install git+https://github.com/org/my_collection.git,v1.2.0

# ติดตั้งจากไฟล์ tar.gz (build artifact)
ansible-galaxy collection install /path/to/my_namespace-my_collection-1.0.0.tar.gz

# ติดตั้งจาก URL โดยตรง
ansible-galaxy collection install https://example.com/my_collection-1.0.0.tar.gz

# requirements.yml รองรับหลาย sources
---
collections:
  - name: community.postgresql
    version: "3.4.0"
    source: https://galaxy.ansible.com  # Galaxy (default)

  - name: my_namespace.internal_collection
    source: https://hub.example.com     # Private Automation Hub

  - name: my_namespace.dev_collection
    source: git+https://github.com/org/dev_collection.git
    version: main

Private Automation Hub

Private Automation Hub (Red Hat) หรือ Pulp-based hub ช่วยให้ทีม mirror collections จาก Galaxy มาเก็บไว้ภายใน — ป้องกัน dependencies เปลี่ยนโดยไม่ตั้งใจและลด external network dependency

# ansible.cfg — กำหนด server list สำหรับ Galaxy และ Private Hub
[galaxy]
server_list = automation_hub, release_galaxy

[galaxy_server.automation_hub]
url = https://cloud.redhat.com/api/automation-hub/
auth_url = https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token
token = my_ah_token

[galaxy_server.release_galaxy]
url = https://galaxy.ansible.com/
token = my_galaxy_token

# ลำดับ: Ansible จะค้นหาใน automation_hub ก่อน ถ้าไม่เจอจึงไปที่ release_galaxy
# ช่วยให้ทีมบังคับให้ใช้ collection เวอร์ชันที่ approved แล้วเท่านั้น

Collections ใน CI/CD Pipeline

การติดตั้ง collections ใน CI/CD pipeline ต้องคำนึงถึง speed (cache pip/galaxy) และ reproducibility (lock version ในทุก run)

# GitHub Actions — ติดตั้ง Ansible และ collections
name: Deploy with Ansible
on: [push]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Cache pip
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: pip-${{ hashFiles('requirements.txt') }}

      - name: Install Ansible
        run: pip install ansible-core==2.17.0

      - name: Cache Galaxy collections
        uses: actions/cache@v4
        with:
          path: ~/.ansible/collections
          key: galaxy-${{ hashFiles('requirements.yml') }}
          # key ผูกกับ requirements.yml เพื่อ invalidate cache เมื่อ version เปลี่ยน

      - name: Install Collections
        run: ansible-galaxy collection install -r requirements.yml

      - name: Run Playbook
        run: ansible-playbook -i inventory/production playbooks/deploy.yml

สรุป

การติดตั้ง Ansible Collections จาก Galaxy ทำด้วย ansible-galaxy collection install — ระบุ version เสมอเพื่อ reproducible deployments ใช้ requirements.yml เป็น central dependency file สำหรับทุกคนในทีม และกำหนด collections_path ใน ansible.cfg เพื่อ isolate dependencies ระหว่าง projects

สำหรับ production environments แนะนำ: ระบุ version แน่นอน (ไม่ใช่ latest), ใช้ Private Automation Hub เพื่อ mirror collections ภายใน, และ cache Galaxy downloads ใน CI/CD pipeline เพื่อลด build time