Ansible连接windows机器&docker容器

拖拖拖,,,这个ansible多种连接方式6月就写了部分总结,终于来搞完它了。。。

ansible有多种连接方式,一般大多使用OpenSSH,类似的还有Paramiko SSH(基于python)、smart、local,这几种比较简单。重点讲一下WinRM和Docker连接,主要是因为官网对于windows连接讲的不清楚,国内的资料也不多。

WinRM连接

1、系统/软件信息

Windows OS: windows7 64bit
Linux OS:CentOS 7.3
Ansible version: 2.3
Powershell version:3.0

官网要求powershell升级到3.0,注意一下。

2、windows设置

1)以管理员身份运行PowerShell,执行下列命令设置允许执行脚本。

PS C:\Windows\system32> set-executionpolicy remotesigned

执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 http://go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y] 是(Y)  [N] 否(N)  [S] 挂起(S)  [?] 帮助 (默认值为“Y”): y

2)下载ConfigureRemotingForAnsible.ps1脚本。
Ansible官网提供链接:https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1

3)使用ansible官网提供的四个命令执行ps脚本,每个命令的注释写在#后面:

#Pass the -CertValidityDays option to customize the expiration date of the generated certificate:
PS C:\Windows\system32> powershell.exe -File 'E:\ansible\ConfigureRemotingForAnsible.ps1' -CertValidityDays 100

#Pass the -EnableCredSSP switch to enable CredSSP as an authentication option:
PS C:\Windows\system32> powershell.exe -File 'E:\ansible\ConfigureRemotingForAnsible.ps1'  -EnableCredSSP


cfg               : http://schemas.microsoft.com/wbem/wsman/1/config/service/auth
lang              : zh-CN
Basic             : true
Kerberos          : true
Negotiate         : true
Certificate       : false
CredSSP           : true
CbtHardeningLevel : Relaxed
#Pass the -ForceNewSSLCert switch to force a new SSL certificate to be attached to an already existing winrm listener. (Avoids SSL winrm errors on syspreped Windows images after the CN changes):
PS C:\Windows\system32> powershell.exe -File 'E:\ansible\ConfigureRemotingForAnsible.ps1' -ForceNewSSLCert
Self-signed SSL certificate generated; thumbprint: A6CFC074C16F424B1C5E0EC70F153CD4E097A0B1

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                : zh-CN
Address             : http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
ReferenceParameters : ReferenceParameters
PS C:\Windows\system32> powershell.exe -File 'E:\ansible\ConfigureRemotingForAnsible.ps1' -SkipNetworkProfileCheck

4)使用管理员权限运行cmd,配置winrm服务的加密方式为允许非加密。

C:\Windows\system32>winrm set winrm/config/service @{AllowUnencrypted="true"}
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 = true
    Auth
        Basic = true
        Kerberos = true
        Negotiate = true
        Certificate = false
        CredSSP = true
        CbtHardeningLevel = Relaxed
    DefaultPorts
        HTTP = 5985
        HTTPS = 5986
    IPv4Filter = *
    IPv6Filter = *
    EnableCompatibilityHttpListener = false
    EnableCompatibilityHttpsListener = false
    CertificateThumbprint
    AllowRemoteAccess = true

3、Linux设置
1)安装pywinrm

# yum install -y python-pip
# pip install "pywinrm>=0.2.2"

Tips:不能联网的话,可以在有网络的机器把pkg下载好,再pip装到不能联网的机器上。

2)在hosts中配置windows信息,使用win_ping模块连接。

[root@ansible ~]# cat /etc/ansible/hosts
[windows]
wannengPC ansible_host=windows_server_ip

[windows:vars]
ansible_user=Administrator
ansible_password=windows_Administrator_password
ansible_connection=winrm
ansible_port=5985

这边注意一下,最好使用Administrator用户,若设为administrator组的其他用户,可能会认证失败(嗯,亲测失败了)。

[root@ansible ~]# ansible -m win_ping windows
wannengPC | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

以上,是我使用ansible连接windows的步骤,其中遇到不少错误,参考链接也放在下面。
http://docs.ansible.com/ansible/latest/intro_windows.html
http://www.torutk.com/projects/swe/wiki/Ansible%E3%82%92Windows%E4%B8%8A%E3%81%AEWSL%E3%81%AB%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB
https://github.com/ansible/ansible/issues/16478
https://github.com/diyan/pywinrm/issues/114

Docker连接

docker连接比较简单,跟着官网来就OK。

1、先装好docker,然后启动服务。

[root@ansible ~]# systemctl start docker
[root@ansible ~]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
10.11.12.13:44381/centos   7                   67591570dd29        7 months ago        191.8 MB
[root@ansible ~]# docker run -it 67591570dd29 /bin/bash
[root@ed725094b6d7 /]#
[root@ansible ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ed725094b6d7        67591570dd29        "/bin/bash"         50 seconds ago      Up 48 seconds                           grave_mccarthy

2、设置ansible hosts

[root@ansible ~]# cat /etc/ansible/hosts
[docker]
docker1

[docker:vars]
ansible_host=grave_mccarthy
ansible_user=root
ansible_connection=docker

注意这边的ansible_host要与docker容器的Name一致。

[root@ansible ~]# ansible -m ping docker
docker1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

以上是docker的连接,基本没难点。

最近在搞saltstack,估计也会总结一下我遇到的errors,从连接方式就感觉saltstack和ansible的不同。saltstack比ansible功能要多很多,当然也复杂一点,具体之后再写总结。

猜你喜欢

转载自blog.csdn.net/wn_hello/article/details/76096112