본문 바로가기

리눅스

[draft] Ansible Pull 모드를 사용하여 NGINX를 설치하는 방법

Ansible Pull 모드를 사용하여 NGINX를 설치하는 방법

Ansible Pull 모드는 중앙 관리 서버가 클라이언트에 명령을 Push하는 방식이 아니라 각 클라이언트가 Git 저장소에서 Playbook을 Pull 하여 직접 실행하는 방식입니다.

 

즉,

  • Push 방식 → Control Node → Managed Node
  • Pull 방식 → Managed Node → Git Repository

1. Ansible Pull 동작 구조

Ansible Pull의 기본 흐름은 다음과 같습니다.

Git Repository
      │
      │ (git clone / pull)
      ▼
Client Server
      │
      │ ansible-pull 실행
      ▼
Playbook 실행
      │
      ▼
패키지 설치 / 서비스 구성

2. Git 저장소 준비

Ansible Pull은 Git 저장소에서 Playbook을 가져오기 때문에 먼저 Git Repository를 준비해야 합니다.

 

로컬에서 Git 저장소 생성

mkdir -p ~/ansible-pull
cd ~/ansible-pull
git init

3. NGINX 설치 Playbook 작성

NGINX를 설치하고 서비스를 실행하는 Playbook을 작성합니다.

vim nginx-playbook.yml
---
- name: Install and configure NGINX
  hosts: localhost
  become: yes

  tasks:
    - name: Install NGINX package
      package:
        name: nginx
        state: present

    - name: Ensure NGINX service is started and enabled
      service:
        name: nginx
        state: started
        enabled: yes
  • hosts : localhost 현재 서버에서 실행
  • become : yes root 권한 사용
  • package module : OS에 맞는 패키지 관리 자동 선택
  • service module : 서비스 실행 및 자동 시작

4. Playbook을 Git 저장소에 업로드

Playbook을 Git에 추가하고 원격 저장소에 업로드합니다.

git add .
git commit -m "Add NGINX installation playbook"

GitHub 또는 GitLab 저장소 연결

git remote add origin https://github.com/<USER>/ansible-pull.git
git remote add origin https://github.com/anti1346/ansible-pull.git
git branch -M main
git push -u origin main

5. 클라이언트 서버에 Ansible 설치

Ansible Pull을 실행할 클라이언트 서버에 Ansible과 Git을 설치합니다.

 

Ubuntu/Debian 기준

sudo apt install -y software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt update
sudo apt install -y ansible git

6. Ansible 설정 파일 구성

Python 인터프리터 경로를 지정합니다.

sudo vim /etc/ansible/ansible.cfg
[defaults]
interpreter_python = /usr/bin/python3

7. Ansible Pull 실행

클라이언트 서버에서 다음 명령으로 Playbook을 가져와 실행합니다.

sudo ansible-pull \
  -U https://github.com/<USER>/ansible-pull.git \
  -d /etc/ansible/pull \
  -i localhost, \
  -C main \
  nginx-playbook.yml

옵션 설명

  • ansible-pull : Pull 방식으로 Playbook 실행
  • -U : Git 저장소 URL
  • -d : Playbook 다운로드 디렉토리
  • -i : 인벤토리 설정
  • localhost : 현재 호스트 실행
  • -C : Git 브랜치 지정
  • nginx-playbook.yml : 실행할 Playbook
Starting Ansible Pull at 2025-02-04 21:09:56
/usr/bin/ansible-pull -U https://github.com/<USER>/ansible-pull.git -d /etc/ansible/pull -i localhost nginx-playbook.yml
node177 | SUCCESS => {
    "after": "4b5bf8faf8aa7b94bd568ba37c3a59d2b59b68d5",
    "before": "4b5bf8faf8aa7b94bd568ba37c3a59d2b59b68d5",
    "changed": false,
    "remote_url_changed": false
}

PLAY [Install and configure NGINX] *********************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Install NGINX] ***********************************************************
ok: [localhost]

TASK [Ensure NGINX is started and enabled] *************************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

8. Cron을 이용한 자동 실행

Ansible Pull은 cron과 함께 사용하면 GitOps 방식 자동 구성 관리가 가능합니다.

 

예를 들어 5분마다 Git 저장소 변경 사항을 반영하도록 설정합니다.

crontab -e
*/5 * * * * /usr/bin/ansible-pull -U https://github.com/<USER>/ansible-pull.git \
  -d /etc/ansible/pull -i localhost, nginx-playbook.yml >> /var/log/ansible-pull.log 2>&1

 

참고URL

- Ansible Community Documentation : ansible-pull