Ansible apt module ใช้จัดการ packages บนระบบ Debian/Ubuntu แทนการรัน apt-get หรือ apt command โดยตรง ทำให้ task มี idempotency — รันซ้ำกี่ครั้งก็ไม่เกิดผลข้างเคียง และ Ansible ตรวจสอบสถานะจริงก่อนติดตั้งหรือลบ
บทความนี้อธิบายการใช้ apt module ตั้งแต่ติดตั้ง/ลบ package, อัพเดทระบบ, จัดการ repository, ไปจนถึง pattern ที่ใช้บ่อยสำหรับ server provisioning บน Ubuntu/Debian
ติดตั้ง Package พื้นฐาน
Parameter หลักของ apt module คือ name และ state ซึ่ง state: present หมายถึงติดตั้งถ้ายังไม่มี และ state: absent หมายถึงลบออกถ้ามีอยู่
---
- name: Install packages
hosts: webservers
become: true
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Install specific version
apt:
name: nginx=1.18.0-0ubuntu1
state: present
- name: Remove package
apt:
name: apache2
state: absent
- name: Remove package and its config files
apt:
name: apache2
state: absent
purge: yes
ติดตั้งหลาย Package พร้อมกัน
ส่ง list ของ packages ไปใน name parameter ทีเดียวแทนการเขียน task ซ้ำหลายครั้ง ประหยัดเวลาเพราะ apt รันครั้งเดียว
---
- name: Setup development environment
hosts: devservers
become: true
tasks:
- name: Install build tools
apt:
name:
- build-essential
- git
- curl
- wget
- vim
- htop
- tree
state: present
update_cache: yes # รัน apt-get update ก่อนติดตั้ง
- name: Remove unwanted packages
apt:
name:
- snapd
- popularity-contest
- ubuntu-advantage-tools
state: absent
update_cache และ cache_valid_time
update_cache: yes รัน apt-get update ก่อนติดตั้ง ส่วน cache_valid_time กำหนดเวลาหน่วย seconds ว่าจะ skip update ถ้า cache ยังไม่เก่าเกินกำหนด ช่วยลดเวลาเมื่อรัน Playbook ซ้ำ
---
- name: Install with cache management
hosts: all
become: true
tasks:
- name: Update cache if older than 1 hour
apt:
update_cache: yes
cache_valid_time: 3600 # 3600 seconds = 1 ชั่วโมง
- name: Install packages after update
apt:
name:
- nginx
- certbot
state: present
update_cache: yes
cache_valid_time: 3600
อัพเกรด Packages
upgrade parameter ใช้สำหรับอัพเกรด packages ทั้งหมด หรือเฉพาะที่อัพเกรดได้โดยไม่ลบ dependency เก่า
---
- name: System update tasks
hosts: all
become: true
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Upgrade all packages (safe)
apt:
upgrade: safe # อัพเกรดเฉพาะที่ไม่ต้องลบ package อื่น
- name: Full upgrade (dist-upgrade)
apt:
upgrade: dist # เทียบกับ apt-get dist-upgrade
- name: Install security updates only
apt:
upgrade: safe
default_release: "{{ ansible_distribution_release }}-security"
จัดการ Repository ด้วย apt_repository
module apt_repository เพิ่มหรือลบ apt repository ก่อนติดตั้ง packages จาก third-party sources
---
- name: Add repositories
hosts: all
become: true
tasks:
- name: Add nginx official repository
apt_repository:
repo: "deb http://nginx.org/packages/ubuntu {{ ansible_distribution_release }} nginx"
state: present
filename: nginx-official
- name: Add Docker repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
state: present
filename: docker
- name: Remove PPA
apt_repository:
repo: "ppa:ondrej/php"
state: absent
จัดการ GPG Key ด้วย apt_key
Repository จาก third-party มักต้องการ GPG key สำหรับ verification ใช้ module apt_key เพิ่ม key ก่อนเพิ่ม repository
---
- name: Add Docker with GPG key
hosts: all
become: true
tasks:
- name: Install required packages
apt:
name:
- apt-transport-https
- ca-certificates
- gnupg
- lsb-release
state: present
update_cache: yes
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
state: present
filename: docker
- name: Install Docker
apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
state: present
update_cache: yes
Autoremove และ Clean
หลังอัพเกรดหรือลบ packages มักมี dependency ที่ไม่ต้องการเหลืออยู่ ใช้ autoremove และ autoclean เพื่อทำความสะอาด
---
- name: Clean up system
hosts: all
become: true
tasks:
- name: Remove unused dependencies
apt:
autoremove: yes
- name: Remove cached package files
apt:
autoclean: yes
- name: Full cleanup in one task
apt:
autoremove: yes
autoclean: yes
update_cache: yes
Pattern: Server Provisioning สมบูรณ์
ตัวอย่าง Playbook สำหรับ provision Ubuntu web server ตั้งแต่ต้น รวม update, install, และ cleanup ในลำดับที่ถูกต้อง
---
- name: Provision Ubuntu Web Server
hosts: webservers
become: true
vars:
web_packages:
- nginx
- certbot
- python3-certbot-nginx
monitoring_packages:
- htop
- iotop
- nethogs
- ncdu
tasks:
- name: Update apt cache
apt:
update_cache: yes
cache_valid_time: 3600
- name: Upgrade security patches
apt:
upgrade: safe
- name: Install web server packages
apt:
name: "{{ web_packages }}"
state: present
- name: Install monitoring tools
apt:
name: "{{ monitoring_packages }}"
state: present
- name: Remove unused packages
apt:
autoremove: yes
autoclean: yes
- name: Check if reboot required
stat:
path: /var/run/reboot-required
register: reboot_required
- name: Reboot if kernel was updated
reboot:
reboot_timeout: 300
when: reboot_required.stat.exists
สรุป
apt module เป็นวิธีที่ถูกต้องสำหรับจัดการ packages บน Debian/Ubuntu ใน Ansible Pattern ที่ควรจำ: ใช้ update_cache: yes พร้อม cache_valid_time เสมอเพื่อป้องกัน apt cache เก่าแต่ไม่ต้อง update ทุกครั้ง, ส่ง package list เป็น array แทนการเขียน task ซ้ำ, และใส่ autoremove: yes ใน cleanup task หลัง upgrade
สำหรับ packages จาก third-party repository ลำดับที่ถูกต้องคือ: เพิ่ม GPG key ด้วย apt_key → เพิ่ม repository ด้วย apt_repository → รัน update_cache → ติดตั้ง package ด้วย apt

