본문 바로가기

리눅스

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

728x90

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

Ansible Pull 모드는 중앙 관리 서버에서 명령을 푸시하는 대신 각 클라이언트가 자신의 구성 정보를 풀(pull)하여 적용하는 방식입니다.

Git 저장소 준비

Ansible pull 모드는 Git에서 플레이북을 가져오므로 Git 서버 또는 GitHub, GitLab 등의 저장소에 플레이북을 올립니다.

 

로컬에서 Git 저장소를 생성합니다.

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

플레이북 작성

nginx-playbook.yml을 작성하여 NGINX를 설치하는 작업을 정의합니다.

vim nginx-playbook.yml
- hosts: localhost
  become: yes
  tasks:
    - name: Ensure NGINX is installed
      package:
        name: nginx
        state: present

    - name: Start and enable NGINX service
      service:
        name: nginx
        state: started
        enabled: yes

플레이북을 Git에 추가합니다.

git add .
git commit -m "Add NGINX installation playbook"
git remote add origin https://github.com/anti1346/ansible-pull.git
git push -u origin main

Ansible 설치

클라이언트 서버에서 Ansible을 설치합니다.

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

Ansible 구성 파일 ansible.cfg에 인터프리터 경로를 추가합니다.

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

Ansible Pull 실행

클라이언트에서 Ansible Pull을 실행하여 NGINX를 설치합니다.

sudo ansible-pull \
-U <YOUR_GIT_REPO_URL> \
-d /etc/ansible/pull \
-i localhost \
-C main \
nginx-playbook.yml

명령어 설명

  • sudo : 관리자 권한으로 실행(필요 시 사용)
  • ansible-pull : Ansible Pull 모드 실행
  • -U : Git 리포지토리 URL 지정
  • -d : Playbook을 다운로드할 디렉토리(/etc/ansible/pull)
  • -i : 인벤토리 파일(localhost로 현재 호스트 지정)
  • -C main : main 브랜치에서 최신 코드를 가져옴
  • nginx-playbook.yml : 실행할 Playbook 파일 이름
ansible-pull -U <YOUR_GIT_REPO_URL> \
-d /etc/ansible/pull \
-i ${HOSTNAME} \
nginx-playbook.yml
Starting Ansible Pull at 2025-02-04 21:09:56
/usr/bin/ansible-pull -U <YOUR_GIT_REPO_URL> -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

스케줄링(cron 설정)

주기적으로 Ansible Pull을 실행하여 변경 사항을 자동 적용할 수 있습니다.

crontab -e
*/5 * * * * /usr/bin/ansible-pull -U <YOUR_GIT_REPO_URL> nginx-playbook.yml > /var/log/ansible-pull.log 2>&1

 

참고URL

- Ansible Community Documentation : ansible-pull

 

728x90