본문 바로가기

Automation Tools/Ansible

9. ansible | windows winrm을 이용한 ansible 사용

반응형
250x250
반응형

1. ssh를 사용하는 리눅스 대신 윈도우는 winrm 모듈의 http를 통하여 powershell 플러그인과 통신을 통하여 ansible을 이용할 수 있음.

2. 테스트 환경 : windows server 2012 

3. windows server 2012는 기본적으로 winrm 이 동작중

PS C:\Users\Administrator> Get-Service -Name winrm

Status   Name               DisplayName
------   ----               -----------
Running  winrm              Windows Remote Management (WS-Manag...

4.  winrm listener 정보 확인

 - http default port 5985을 사용

PS C:\Users\Administrator> winrm e winrm/config/Listener
Listener
    Address = *
    Transport = HTTP
    Port = 5985
    Hostname
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint
    ListeningOn = 127.0.0.1, 172.16.10.23, ::1, fe80::5efe:172.16.10.23%13, fe80::394c:1175:f5eb:f525%12

 - https default port 5986 을 사용 할수도 있으며 ansible은 기본적으로 winrm과 통신시 https를 사용

 

5. winrm 설정

 - https default port 5986 사용을 위한 설정

 - 설정 파일은 ps1 파일로 다운받아 설치할 수 있음 : https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1

PS C:\Users\Administrator\Desktop> .\{다운받은 파일}.ps1
Self-signed SSL certificate generated; thumbprint: 84A1D8A7384972A743492D2EB514AF023C3FA53A


wxf                 : http://schemas.xmlsoap.org/ws/2004/09/transfer
a                   : http://schemas.xmlsoap.org/ws/2004/08/addressing
w                   : http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
lang                : ko-KR
Address             : http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
ReferenceParameters : ReferenceParameters

확인됨

 

6. winrm listener 정보 확인

PS C:\Users\Administrator\Desktop> winrm e winrm/config/listener
Listener
    Address = *
    Transport = HTTP
    Port = 5985
    Hostname
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint
    ListeningOn = 127.0.0.1, 172.16.10.23, ::1, fe80::5efe:172.16.10.23%13, fe80::394c:1175:f5eb:f525%12

Listener
    Address = *
    Transport = HTTPS
    Port = 5986
    Hostname = {hostname}
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint = 84A1D8A7384972A743492D2EB514AF023C3FA53A
    ListeningOn = 127.0.0.1, 172.16.10.23, ::1, fe80::5efe:172.16.10.23%13, fe80::394c:1175:f5eb:f525%12

 

7. winrm 모든 설정 확인

PS C:\Users\Administrator\Desktop> winrm get winrm/config
Config
    MaxEnvelopeSizekb = 500
    MaxTimeoutms = 60000
    MaxBatchItems = 32000
    MaxProviderRequests = 4294967295
    Client
        NetworkDelayms = 5000
        URLPrefix = wsman
        AllowUnencrypted = false
        Auth
            Basic = true
            Digest = true
            Kerberos = true
            Negotiate = true
            Certificate = true
            CredSSP = false
        DefaultPorts
            HTTP = 5985
            HTTPS = 5986
        TrustedHosts = 
    Service
        RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)
        MaxConcurrentOperations = 4294967295
        MaxConcurrentOperationsPerUser = 1500
        EnumerationTimeoutms = 240000
        MaxConnections = 300
        MaxPacketRetrievalTimeSeconds = 120
        AllowUnencrypted = false
        Auth
            Basic = true
            Kerberos = true
            Negotiate = true
            Certificate = false
            CredSSP = false
            CbtHardeningLevel = Relaxed
        DefaultPorts
            HTTP = 5985
            HTTPS = 5986
        IPv4Filter = *
        IPv6Filter = *
        EnableCompatibilityHttpListener = false
        EnableCompatibilityHttpsListener = false
        CertificateThumbprint
        AllowRemoteAccess = true
    Winrs
        AllowRemoteShellAccess = true
        IdleTimeout = 7200000
        MaxConcurrentUsers = 10
        MaxShellRunTime = 2147483647
        MaxProcessesPerShell = 25
        MaxMemoryPerShellMB = 1024
        MaxShellsPerUser = 30

 - 테스트

winrm identify -r:https://{windows ip}:5986 -auth:basic -u:user_name -p:password -encoding:utf-8

8. ansible winrm 모률 추가

 - ansible은 python을 이용하므로 winrm python 모듈이 필요함

 - pip로 모듈추가

pip install pywinrm

# 확인
pip list
...
pywinrm 0.4.3
...

 - 오류 : "winrm or requests is not installed: No module named winrm" 와 같이 모듈이 없다고 나오는경우 pydoc으로 한번 더 모듈 확인

# 아래와 같이 확인시 winrm 모듈이 없다면
pydoc modules

# pip 모듈 제거
pip uninstall pywinrm

# 패키지로 설치
yum install python2-winrm

# 모듈 확인
pydoc modules
...
anydbm              htmllib             random              winrm
...

 

9. ansible hosts

 - ansible_port=5986 은 default

vim /etc/ansible/hosts

test    ansible_host=172.16.10.23 ansible_user=administrator ansible_connection=winrm ansible_password="*******" ansible_winrm_server_cert_validation=ignore

 

10. ping test

ansible -i hosts test -m win_ping
test | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

 

11. 참고. 윈도우에 winrm이 http(5985포트) 만 사용하고 있고 ansible이 https로 통신하게 된다면 아래와 같은 credentials reject 오류가 발생할 수 있음.

 "plaintext: the specified credentials were rejected by the server"

test | UNREACHABLE! => {
    "changed": false, 
    "msg": "plaintext: the specified credentials were rejected by the server", 
    "unreachable": true
}

 

12. 참고. 인증된 ssl 인증서가 아닌경우가 내부 통신시에 대부분인 경우 아래와 같은 오류가 발생 할 수 있음.

ansible -i hosts win -m win_ping
test | UNREACHABLE! => {
    "changed": false, 
    "msg": "ssl: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)", 
    "unreachable": true
}

 - ansible hosts 에 "ansible_winrm_server_cert_validation=ignore" 추가 

 

13. 참고. winrm에 기본인증 basic auth 활성화 방법 (default = true)

PS > winrm set winrm/config/service/auth @{Basic="true"}
반응형