본문 바로가기
IT 끄적이기

윈도우 PC와 클라우드 서버에서 Ansible 설치 및 활용하기

by 미르아 2025. 1. 8.
728x90

윈도우 PC에서 WinRM 설정하기

Ansible은 Windows와 통신하기 위해 WinRM(Windows Remote Management)을 사용합니다.

아래 단계에 따라 WinRM을 설정합니다.

WinRM 활성화

winrm quickconfig

이 명령을 실행하면 WinRM 서비스가 시작되고, 기본 설정이 적용됩니다.

인증 방식 설정

winrm set winrm/config/service/auth @{Basic="true"}
winrm set winrm/config/service @{AllowUnencrypted="true"}

위 명령은 Basic 인증과 암호화되지 않은 데이터를 허용하도록 WinRM을 설정합니다.

방화벽 규칙 추가

5985 (HTTP) 및 5986 (HTTPS) 포트를 열어야 합니다. 필요에 따라 다른 포트를 사용한다면 해당 포트를 허용하세요.

netsh advfirewall firewall add rule name="WinRM HTTP" dir=in action=allow protocol=TCP localport=5985
netsh advfirewall firewall add rule name="WinRM HTTPS" dir=in action=allow protocol=TCP localport=5986

HTTPS 설정 (옵션)

HTTPS를 사용하려면 인증서가 필요합니다. 셀프 서명 인증서를 생성하거나 도메인 인증서를 사용하세요.

winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Port="5986";Hostname="<your-hostname>";CertificateThumbprint="<your-thumbprint>"}

클라우드 서버에서 Python 가상환경으로 Ansible 설치하기

가상환경을 사용하면 Python 및 관련 패키지를 시스템에 영향을 주지 않고 독립적으로 관리할 수 있습니다.

Python 설치 확인

Ubuntu에서 Python 3.10 이상이 설치되어 있는지 확인합니다.

python3 --version

필요하면 Python 3.10 이상을 설치합니다.

sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install -y python3.10 python3.10-venv python3.10-distutils

가상환경 생성 및 활성화

python3.10 -m venv ansible-env
source ansible-env/bin/activate

Ansible 설치

가상환경 안에서 Ansible을 설치합니다.

pip install --upgrade pip setuptools
pip install ansible

Ansible 버전 확인

ansible --version

출력 결과에 Python 버전과 Ansible 경로가 가상환경을 가리키는지 확인합니다.

Ansible Inventory 파일 생성

Ansible이 관리할 호스트 정보를 작성합니다. inventory 파일을 생성합니다.

Inventory 파일 예제

[windows]
192.168.0.20 ansible_user=Administrator ansible_password=your_password ansible_connection=winrm ansible_port=5986 ansible_winrm_transport=basic
192.168.0.21 ansible_user=Administrator ansible_password=your_password ansible_connection=winrm ansible_port=5986 ansible_winrm_transport=basic
192.168.0.22 ansible_user=Administrator ansible_password=your_password ansible_connection=winrm ansible_port=5986 ansible_winrm_transport=basic

Ansible Ping 테스트

Ansible이 Windows에 정상적으로 연결되는지 확인합니다.

ansible -i inventory windows -m win_ping

정상적으로 연결되었다면 다음과 같은 출력이 나타납니다:

192.168.0.20 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
192.168.0.21 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
192.168.0.22 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

간단한 작업 실행

Windows에서 간단한 작업을 실행하여 Ansible이 정상적으로 동작하는지 확인합니다.

ansible -i inventory windows -m win_shell -a "echo Hello from Ansible"

출력 예시:

192.168.0.20 | CHANGED | rc=0 >>
Hello from Ansible
192.168.0.21 | CHANGED | rc=0 >>
Hello from Ansible
192.168.0.22 | CHANGED | rc=0 >>
Hello from Ansible

참고 사항

  • 보안 강화: 실제 환경에서는 암호화되지 않은 Basic 인증 대신 HTTPS 및 Kerberos를 사용하는 것이 좋습니다.
  • 오류 해결: 만약 연결이 실패하면 WinRM 설정과 방화벽 규칙을 다시 확인하세요.
  • 로깅: Windows 이벤트 뷰어에서 Microsoft-Windows-WinRM 및 PowerShell 로그를 확인하여 문제를 디버깅할 수 있습니다.
728x90