Ansible을 이용하여 Windows 서버에 파일을 배포하는 방법에 대해서 알아보겠습니다.
Ansible에서 Windows 서버 통신을 위해 Winrm이라는 모듈을 사용하며 Python에 pywinrm 라이브러리를 Install 하는 작업이 필요합니다.
** OS 환경: Ansilbe 서버(Ubuntu 18.04) / Windows 서버(Windows 2019)
1. Winrm 동작 상태 확인
- Windows 서버 접속 후에 WInrm 서비스가 동작 중인지 먼저 확인하겠습니다. - 실행->Services.msc 접속 후 아래와 같이 Winrm 서비스가 실행 중인지 체크해주세요
- Linux 서버와 통신 상태 확인 - https: 5986 / http: 5985 -> HTTPS 작업을 위해서는 Windows 서버에서 별도의 작업이 필요합니다. - 저는 http를 이용하여 작업할 예정이므로 5985 포트 통신이 가능한 지 확인합니다. - 아래와 같이 netstat, nc 명령어를 사용해 확인해줍니다.
Windows Port 상태 확인 Ansilbe 서버에서 Port 통신 확인
2. hosts 파일 작성
- Ansible로 작업을 진행할 Windows 서버 정보를 hosts 파일에 입력해줍니다. - 계정 정보가 들어가므로 vault 파일 형태로 관리하는 것이 좋습니다. - 신규 생성 시 ansible-vault create [파일 명] - 수정 시 ansible-vault edit [파일 명]
hosts 파일 신규 생성 - 위의 명령어를 통해 hosts 파일을 여신 후 아래와 같이 정보를 입력해줍니다. - 저는 winrm 인증 방식은 basic, port는 5985번을 사용합니다.
- 명령어 정리 ## 신규 생성 시 # ansible-vault create /etc/ansilbe/hosts
## vault로 생성된 파일 수정 시 # ansible-vault edit /etc/ansilbe/hosts
* Python3 사용 시 # apt-get install python3-pip # pip3 install pywinrm
- 재 실행
** "basic: the specified credentials were rejected by the server 인증 관련 오류 발생
- 해결 방법 > winrm get winrm/config 설정 상태 확인 ** Auth Basic = true 상태 확인 / AllowUnencrypted = true 상태 확인 > Set-Item -Path WSMan:\localhost\Service\Auth\Basic -Value $true > Set-Item -Path WSMan:\localhost\Service\AllowUnencrypted -Value $true > Enable-PSRemoting -Force > Set-Item WSMan:\localhost\Client\TrustedHosts -Value [Ansible 서버 IP]