Ansible Automation เริ่มต้น Config Management แบบมือโปร IT General

Ansible Automation เริ่มต้น Config Management แบบมือโปร

📅 1997-11-11 | โดย อ.บอม กิตติทัศน์ เจริญพนาสิทธิ์ — SiamCafe.net Since 1997

📑 สารบัญ

Ansible คืออะไร

Ansible เป็นเครื่องมือ automation ที่ใช้จัดการ server หลายร้อยเครื่องพร้อมกัน เขียนด้วย YAML อ่านง่าย ไม่ต้องติดตั้ง agent บน server ปลายทาง ใช้ SSH เชื่อมต่อ

ทำไมต้อง Ansible

วิธีเดิมAnsible
SSH เข้าทีละเครื่องรันคำสั่งพร้อมกัน 100 เครื่อง
จำ config ไม่ได้เขียนเป็น code (IaC)
ทำซ้ำไม่เหมือนเดิมIdempotent — รันกี่ครั้งก็ผลเหมือนเดิม
ไม่มี audit trailGit version control

🎬 วิดีโอแนะนำ

ดูวิดีโอเพิ่มเติมเกี่ยวกับAnsible Automation เริ่มต้น Config Manag:

ติดตั้ง

# Ubuntu/Debian
sudo apt update
sudo apt install ansible -y

# macOS
brew install ansible

# pip (ทุก OS)
pip install ansible

# ตรวจสอบ
ansible --version

Inventory — รายชื่อ Server

# inventory.yml
all:
  children:
    webservers:
      hosts:
        web1:
          ansible_host: 192.168.1.10
        web2:
          ansible_host: 192.168.1.11
      vars:
        ansible_user: ubuntu
        ansible_ssh_private_key_file: ~/.ssh/id_rsa

    databases:
      hosts:
        db1:
          ansible_host: 192.168.1.20
      vars:
        ansible_user: root

Playbook แรก

# setup-webserver.yml
---
- name: Setup Web Servers
  hosts: webservers
  become: yes  # sudo

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

    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Start Nginx
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Copy website config
      template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/sites-available/default
      notify: Restart Nginx

  handlers:
    - name: Restart Nginx
      service:
        name: nginx
        state: restarted
# รัน playbook
ansible-playbook -i inventory.yml setup-webserver.yml

# Dry run (ทดสอบก่อน ไม่เปลี่ยนแปลงจริง)
ansible-playbook -i inventory.yml setup-webserver.yml --check

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

ตัวอย่างจริง: Deploy Application

# deploy-app.yml
---
- name: Deploy Python App
  hosts: webservers
  become: yes
  vars:
    app_dir: /opt/myapp
    app_repo: https://github.com/myorg/myapp.git
    app_branch: main

  tasks:
    - name: Install Python dependencies
      apt:
        name:
          - python3
          - python3-pip
          - python3-venv
          - git
        state: present

    - name: Clone/update repository
      git:
        repo: "{{ app_repo }}"
        dest: "{{ app_dir }}"
        version: "{{ app_branch }}"
        force: yes

    - name: Install pip requirements
      pip:
        requirements: "{{ app_dir }}/requirements.txt"
        virtualenv: "{{ app_dir }}/venv"

    - name: Copy systemd service
      template:
        src: templates/myapp.service.j2
        dest: /etc/systemd/system/myapp.service
      notify: Restart App

    - name: Enable and start app
      systemd:
        name: myapp
        state: started
        enabled: yes
        daemon_reload: yes

  handlers:
    - name: Restart App
      systemd:
        name: myapp
        state: restarted

Ansible เป็นเครื่องมือที่ทุก SysAdmin และ DevOps ต้องรู้ เรียนรู้ง่าย (YAML) ไม่ต้องติดตั้ง agent ใช้ได้ทั้ง Linux, Windows, Network devices

💡 แนะนำ: iCafeFX ระบบเทรดอัตโนมัติ

💡 แนะนำ: CafeFX Panel

❓ FAQ

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

Terraform สร้าง infrastructure (VM, network, storage) Ansible จัดการ configuration ภายใน server ใช้คู่กัน: Terraform สร้าง VM → Ansible ติดตั้ง software

Ansible กับ Chef/Puppet เลือกอะไร?

Ansible ง่ายที่สุด ไม่ต้อง agent เขียน YAML Chef/Puppet ซับซ้อนกว่า ต้องติดตั้ง agent แต่เหมาะกับองค์กรใหญ่ที่มี server หลายพันเครื่อง

Ansible ใช้กับ Windows ได้ไหม?

ได้ ใช้ WinRM แทน SSH มี module สำหรับ Windows เช่น win_service, win_package, win_feature แต่ Linux support ดีกว่า