Technology

automation คืออะไร

automation คอ อะไร
automation คืออะไร | SiamCafe Blog
2026-04-25· อ. บอม — SiamCafe.net· 9,211 คำ

Automation คืออะไร

Automation หรือระบบอัตโนมัติในบริบทของงาน IT คือการใช้เครื่องมือ Script หรือ Software ทำงานที่เคยต้องใช้คนทำด้วยมือให้ทำงานโดยอัตโนมัติ ตั้งแต่งานง่ายๆอย่างการ Backup ไฟล์ทุกวัน ไปจนถึงงานซับซ้อนอย่างการ Provision Infrastructure บน Cloud แล้ว Deploy Application พร้อม Configure ทุกอย่างโดยไม่ต้องมีคน SSH เข้าไปทำด้วยมือแม้แต่ครั้งเดียว

การทำ Automation ไม่ใช่แค่เรื่องของความเร็ว แต่เป็นเรื่องของ Consistency ด้วย เมื่อทำด้วยมือ คนอาจลืมขั้นตอนบางอย่าง พิมพ์ Command ผิด หรือตั้งค่าไม่เหมือนกันในแต่ละ Server แต่เมื่อเขียนเป็น Script หรือ Code ทุกอย่างจะทำเหมือนกันทุกครั้งไม่ว่าจะรันกี่รอบก็ตาม

ระดับของ Automation ในงาน IT แบ่งได้เป็นหลายระดับตั้งแต่ Shell Script สำหรับงาน Ad-hoc, Configuration Management สำหรับจัดการ Server, Infrastructure as Code สำหรับสร้าง Infrastructure และ CI/CD Pipeline สำหรับ Automate กระบวนการ Software Delivery ทั้งหมด

ระดับที่ 1: Shell Script — จุดเริ่มต้นของ Automation

Shell Script เป็นวิธีง่ายที่สุดในการเริ่มทำ Automation โดยเขียนคำสั่งที่เคยพิมพ์ด้วยมือลงในไฟล์ Script แล้วรันด้วยคำสั่งเดียว

#!/bin/bash
# backup-database.sh — Script สำหรับ Backup Database อัตโนมัติ
set -euo pipefail

# Configuration จาก Environment Variables
DB_HOST=""
DB_NAME=""
DB_USER=""
BACKUP_DIR="/var/backups/database"
RETENTION_DAYS=30
S3_BUCKET=""
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="/_.sql.gz"

# สร้าง Directory ถ้ายังไม่มี
mkdir -p ""

log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}

log "เริ่ม Backup Database: "

# Dump Database พร้อมบีบอัด
pg_dump -h "" -U "" -d "" \
    --format=plain --no-owner --no-privileges | \
    gzip > ""

FILESIZE=$(du -h "" | cut -f1)
log "Backup เสร็จสิ้น:  ()"

# Upload ไปยัง S3
aws s3 cp "" \
    "s3:///database//" \
    --storage-class STANDARD_IA
log "Upload ไปยัง S3 เรียบร้อย"

# ลบ Backup เก่ากว่า 30 วัน
find "" -name "*.sql.gz" -mtime + -delete
log "ลบ Backup เก่ากว่า  วันเรียบร้อย"

# ตรวจสอบว่า Backup ถูกต้อง
gunzip -t "" && log "Integrity Check ผ่าน" || log "ERROR: Backup เสียหาย"

---
# ตั้ง Cron Job ให้รันทุกวันตี 3
echo "0 3 * * * /opt/scripts/backup-database.sh >> /var/log/backup.log 2>&1" | \
    sudo tee /etc/cron.d/database-backup

# ตรวจสอบ Cron Job
sudo crontab -l

Script สำหรับ Server Health Check

#!/bin/bash
# health-check.sh — ตรวจสอบสถานะ Server อัตโนมัติ
set -euo pipefail

THRESHOLD_CPU=85
THRESHOLD_MEM=90
THRESHOLD_DISK=85
ALERT_EMAIL="admin@company.com"

check_cpu() {
    local cpu_usage
    cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print 100 - $8}' | cut -d. -f1)
    if [ "$cpu_usage" -gt "$THRESHOLD_CPU" ]; then
        echo "WARNING: CPU Usage % (เกิน %)"
        return 1
    fi
    echo "OK: CPU Usage %"
}

check_memory() {
    local mem_usage
    mem_usage=$(free | awk '/Mem:/ {printf "%.0f", $3/$2 * 100}')
    if [ "$mem_usage" -gt "$THRESHOLD_MEM" ]; then
        echo "WARNING: Memory Usage % (เกิน %)"
        return 1
    fi
    echo "OK: Memory Usage %"
}

check_disk() {
    local disk_usage
    disk_usage=$(df -h / | awk 'NR==2 {print $5}' | tr -d '%')
    if [ "$disk_usage" -gt "$THRESHOLD_DISK" ]; then
        echo "WARNING: Disk Usage % (เกิน %)"
        return 1
    fi
    echo "OK: Disk Usage %"
}

check_services() {
    local services=("nginx" "postgresql" "redis-server")
    for svc in ""; do
        if systemctl is-active --quiet "$svc"; then
            echo "OK:  is running"
        else
            echo "CRITICAL:  is NOT running"
            systemctl start "$svc" 2>/dev/null && echo "  → Auto-started "
        fi
    done
}

echo "=== Server Health Check $(date) ==="
echo "Hostname: $(hostname)"
echo "Uptime: $(uptime -p)"
echo ""
check_cpu
check_memory
check_disk
check_services

ระดับที่ 2: Ansible — Configuration Management

Ansible เป็นเครื่องมือ Configuration Management ที่ใช้ YAML เขียน Playbook สำหรับจัดการ Server หลายเครื่องพร้อมกัน ไม่ต้องติดตั้ง Agent บน Server ปลายทาง ใช้ SSH เชื่อมต่อเท่านั้น

# inventory.yml — รายชื่อ Server
all:
  children:
    webservers:
      hosts:
        web01:
          ansible_host: 10.0.1.10
        web02:
          ansible_host: 10.0.1.11
    databases:
      hosts:
        db01:
          ansible_host: 10.0.2.10
  vars:
    ansible_user: deploy
    ansible_ssh_private_key_file: ~/.ssh/deploy_key

---
# playbook-webserver.yml — ติดตั้งและตั้งค่า Web Server
- name: Configure Web Servers
  hosts: webservers
  become: true
  vars:
    nginx_worker_processes: auto
    nginx_worker_connections: 1024
    app_port: 3000

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes
        cache_valid_time: 3600

    - name: Install required packages
      apt:
        name:
          - nginx
          - certbot
          - python3-certbot-nginx
          - htop
          - curl
          - jq
        state: present

    - name: Copy Nginx configuration
      template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/sites-available/default
        mode: '0644'
      notify: Reload Nginx

    - name: Enable Nginx site
      file:
        src: /etc/nginx/sites-available/default
        dest: /etc/nginx/sites-enabled/default
        state: link

    - name: Ensure Nginx is running
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Setup firewall rules
      ufw:
        rule: allow
        port: "{{ item }}"
        proto: tcp
      loop:
        - "22"
        - "80"
        - "443"

    - name: Enable UFW
      ufw:
        state: enabled
        policy: deny
        direction: incoming

  handlers:
    - name: Reload Nginx
      service:
        name: nginx
        state: reloaded

---
# รัน Playbook
ansible-playbook -i inventory.yml playbook-webserver.yml

# รันแบบ Dry Run (ตรวจสอบก่อนทำจริง)
ansible-playbook -i inventory.yml playbook-webserver.yml --check --diff

# รันเฉพาะบาง Tag
ansible-playbook -i inventory.yml playbook-webserver.yml --tags "nginx"

ระดับที่ 3: Terraform — Infrastructure as Code

Terraform เป็นเครื่องมือ Infrastructure as Code (IaC) ที่ใช้สร้างและจัดการ Cloud Infrastructure ด้วย Code ทำให้สามารถ Version Control, Review และ Reproduce Infrastructure ได้ทุกเมื่อ

# main.tf — สร้าง Infrastructure บน AWS
terraform {
  required_version = ">= 1.5"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
  backend "s3" {
    bucket = "company-terraform-state"
    key    = "production/terraform.tfstate"
    region = "ap-southeast-1"
  }
}

provider "aws" {
  region = var.aws_region
}

# VPC
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.0"

  name = "-vpc"
  cidr = "10.0.0.0/16"

  azs             = ["a", "b"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24"]

  enable_nat_gateway   = true
  single_nat_gateway   = true
  enable_dns_hostnames = true

  tags = var.common_tags
}

# EC2 Instance
resource "aws_instance" "web" {
  count                  = var.web_instance_count
  ami                    = data.aws_ami.ubuntu.id
  instance_type          = var.web_instance_type
  subnet_id              = module.vpc.private_subnets[count.index % 2]
  vpc_security_group_ids = [aws_security_group.web.id]
  key_name               = aws_key_pair.deploy.key_name

  root_block_device {
    volume_size = 50
    volume_type = "gp3"
    encrypted   = true
  }

  tags = merge(var.common_tags, {
    Name = "-web-"
    Role = "webserver"
  })
}

# RDS Database
resource "aws_db_instance" "main" {
  identifier     = "-db"
  engine         = "postgres"
  engine_version = "16.1"
  instance_class = var.db_instance_class
  
  allocated_storage     = 100
  max_allocated_storage = 500
  storage_encrypted     = true

  db_name  = var.db_name
  username = var.db_username
  password = var.db_password

  db_subnet_group_name   = aws_db_subnet_group.main.name
  vpc_security_group_ids = [aws_security_group.db.id]

  backup_retention_period = 7
  multi_az               = true
  skip_final_snapshot    = false

  tags = var.common_tags
}

# variables.tf
variable "aws_region" {
  default = "ap-southeast-1"
}
variable "project" {
  default = "myapp"
}
variable "web_instance_count" {
  default = 2
}
variable "web_instance_type" {
  default = "t3.medium"
}
variable "db_instance_class" {
  default = "db.t3.medium"
}

---
# Deploy Infrastructure
terraform init
terraform plan -out=tfplan
terraform apply tfplan

# ดู State ปัจจุบัน
terraform state list

# ทำลาย Infrastructure (ระวัง!)
terraform destroy

ระดับที่ 4: CI/CD Pipeline — Automate Software Delivery

CI/CD Pipeline เป็นระดับสูงสุดของ Automation ที่รวมการ Build, Test และ Deploy เข้าด้วยกันเป็น Pipeline อัตโนมัติ ทุกครั้งที่ Push Code ไปยัง Repository Pipeline จะทำงานอัตโนมัติตั้งแต่รัน Unit Test, Build Docker Image, Push ไป Registry แล้ว Deploy ไปยัง Production

# .github/workflows/deploy.yml — GitHub Actions CI/CD Pipeline
name: Build and Deploy
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: }

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

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
          cache: 'pip'

      - name: Install Dependencies
        run: pip install -r requirements.txt -r requirements-dev.txt

      - name: Run Linting
        run: |
          ruff check .
          mypy src/

      - name: Run Tests
        run: pytest tests/ -v --cov=src --cov-report=xml

      - name: Upload Coverage
        uses: codecov/codecov-action@v4
        with:
          file: coverage.xml

  build:
    needs: test
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    permissions:
      contents: read
      packages: write
    outputs:
      image_tag: }

    steps:
      - uses: actions/checkout@v4

      - name: Login to Registry
        uses: docker/login-action@v3
        with:
          registry: }
          username: }
          password: }

      - name: Build and Push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: }/}:}
          cache-from: type=gha
          cache-to: type=gha, mode=max

  deploy:
    needs: build
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    environment: production

    steps:
      - uses: actions/checkout@v4

      - name: Deploy to Kubernetes
        run: |
          kubectl set image deployment/webapp \
            webapp=}/}:} \
            -n production
          kubectl rollout status deployment/webapp -n production --timeout=120s

เปรียบเทียบเครื่องมือ Automation

เครื่องมือประเภทภาษาจุดเด่นเหมาะกับ
Bash ScriptScriptingShellไม่ต้องติดตั้งอะไรเพิ่มงาน Ad-hoc บน Linux
AnsibleConfig ManagementYAMLAgentless ใช้ SSHตั้งค่า Server หลายเครื่อง
TerraformIaCHCLState Management ดีสร้าง Cloud Infrastructure
GitHub ActionsCI/CDYAMLIntegrate กับ GitHubBuild Test Deploy อัตโนมัติ
JenkinsCI/CDGroovyPlugin เยอะองค์กรที่ต้องการ Self-hosted
PuppetConfig ManagementDSLScale ได้ดีมากองค์กรขนาดใหญ่

Automation ในงาน IT คืออะไร

Automation คือการใช้เครื่องมือหรือ Script ทำงานซ้ำๆแทนมนุษย์ เช่น การ Deploy Application การ Configure Server การ Backup ข้อมูล การ Monitor ระบบ ช่วยลดข้อผิดพลาดจาก Human Error เพิ่มความเร็วในการทำงาน และทำให้ได้ผลลัพธ์ที่สม่ำเสมอทุกครั้ง

ควรเริ่มเรียน Automation จากเครื่องมือไหน

ควรเริ่มจาก Bash Script สำหรับ Linux หรือ PowerShell สำหรับ Windows เพราะเป็นพื้นฐานที่ใช้ทุกวัน จากนั้นเรียน Ansible สำหรับจัดการ Server หลายเครื่อง แล้วต่อด้วย Terraform สำหรับสร้าง Cloud Infrastructure และ GitHub Actions สำหรับ CI/CD Pipeline

Ansible กับ Terraform ต่างกันอย่างไร

Ansible เน้น Configuration Management เช่น ติดตั้ง Software ตั้งค่า Config File จัดการ Service บน Server ที่มีอยู่แล้ว ส่วน Terraform เน้น Infrastructure Provisioning เช่น สร้าง VM, VPC, Database บน Cloud ทั้งสองเครื่องมือเสริมกัน ใช้ Terraform สร้าง Infrastructure แล้วใช้ Ansible ตั้งค่า Software บน Server

CI/CD Pipeline คืออะไรและเกี่ยวกับ Automation อย่างไร

CI/CD คือ Continuous Integration/Continuous Delivery เป็น Pipeline อัตโนมัติที่ Build, Test และ Deploy Code ทุกครั้งที่ Push ไปยัง Repository ช่วยให้ Release Software ได้เร็วและมั่นใจว่าผ่านการทดสอบ เป็นการ Automate กระบวนการ Software Delivery ทั้งหมดตั้งแต่ต้นจนจบ

สรุปและแนวทางปฏิบัติ

Automation เป็นทักษะพื้นฐานที่จำเป็นสำหรับทุกู้คืนที่ทำงานด้าน IT ไม่ว่าจะเป็น System Admin, DevOps Engineer หรือ Developer การเริ่มจาก Shell Script ง่ายๆแล้วค่อยๆขยับไปใช้เครื่องมือที่ซับซ้อนขึ้นอย่าง Ansible, Terraform และ CI/CD Pipeline จะช่วยให้ทำงานได้เร็วขึ้น ผิดพลาดน้อยลง และมีเวลาไปทำงานที่มีคุณค่ามากกว่าแทนที่จะนั่งทำงานซ้ำๆทุกวัน หลักการสำคัญคือ Automate Everything ที่ทำมากกว่า 2 ครั้ง เขียนเป็น Code เก็บใน Git และทดสอบก่อนรันจริงทุกครั้ง

📖 บทความที่เกี่ยวข้อง

10 javascript คืออะไรมวตถประสงคเพออะไรอ่านบทความ → Apache Arrow CI CD Automation Pipelineอ่านบทความ → mysql workbench คืออะไรอ่านบทความ → n c คืออะไรอ่านบทความ → modbus tcp ip คืออะไรอ่านบทความ →

📚 ดูบทความทั้งหมด →