Ansible windows使用

这几天搞ansible 在windows下的使用,还是有一点小坑的,为了避免入坑,方便大家参考。

1,客户端的安装

ansible的安装

客户端除了安装ansible之外,还要安装winrm

yum install -y ansible
easy_install pip
pip install "pywinrm>=0.2.2" --ignore-installed

服务器这边相对要复杂一些1-3主要是2008R2,win10应该不用
(1)安装Framework 3.0+,
下载链接为:http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe。
(2)更改powershell策略为remotesigned
et-executionpolicy remotesigned
(3)升级PowerShell至3.0+, 下脚本保存至本地后,右键选择“使用PowerShell运行”,执行完毕重启系统后,在PowerShell执行Get-Host命令结果如下图所示PowerShell版本为3.0为正常。

# Powershell script to upgrade a PowerShell 2.0 system to PowerShell 3.0
# based on http://occasionalutility.blogspot.com/2013/11/everyday-powershell-part-7-powershell.html
#
# some Ansible modules that may use Powershell 3 features, so systems may need
# to be upgraded.  This may be used by a sample playbook.  Refer to the windows
# documentation on docs.ansible.com for details.
# 
# - hosts: windows
#   tasks:
#     - script: upgrade_to_ps3.ps1

# Get version of OS

# 6.0 is 2008
# 6.1 is 2008 R2
# 6.2 is 2012
# 6.3 is 2012 R2


if ($PSVersionTable.psversion.Major -ge 3)
{
    write-host "Powershell 3 Installed already; You don't need this"
    Exit
}

$powershellpath = "C:\powershell"

function download-file
{
    param ([string]$path, [string]$local)
    $client = new-object system.net.WebClient
    $client.Headers.Add("user-agent", "PowerShell")
    $client.downloadfile($path, $local)
}

if (!(test-path $powershellpath))
{
    New-Item -ItemType directory -Path $powershellpath
}


# .NET Framework 4.0 is necessary.

#if (($PSVersionTable.CLRVersion.Major) -lt 2)
#{
#    $DownloadUrl = "http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe"
#    $FileName = $DownLoadUrl.Split('/')[-1]
#    download-file $downloadurl "$powershellpath\$filename"
#    ."$powershellpath\$filename" /quiet /norestart
#}

#You may need to reboot after the .NET install if so just run the script again.

# If the Operating System is above 6.2, then you already have PowerShell Version > 3
if ([Environment]::OSVersion.Version.Major -gt 6)
{
    write-host "OS is new; upgrade not needed."
    Exit
}


$osminor = [environment]::OSVersion.Version.Minor

$architecture = $ENV:PROCESSOR_ARCHITECTURE

if ($architecture -eq "AMD64")
{
    $architecture = "x64"
}  
else
{
    $architecture = "x86" 
} 

if ($osminor -eq 1)
{
    $DownloadUrl = "http://download.microsoft.com/download/E/7/6/E76850B8-DA6E-4FF5-8CCE-A24FC513FD16/Windows6.1-KB2506143-" + $architecture + ".msu"
}
elseif ($osminor -eq 0)
{
    $DownloadUrl = "http://download.microsoft.com/download/E/7/6/E76850B8-DA6E-4FF5-8CCE-A24FC513FD16/Windows6.0-KB2506146-" + $architecture + ".msu"
}
else
{
    # Nothing to do; In theory this point will never be reached.
    Exit
}

$FileName = $DownLoadUrl.Split('/')[-1]
download-file $downloadurl "$powershellpath\$filename"

Start-Process -FilePath "$powershellpath\$filename" -ArgumentList /quiet

(4)设置Windows远端管理(WS-Management,WinRM),在windows 的命令行模式下运行(用管理员)

winrm enumerate winrm/config/listener#winrm service 默认都是未启用的状态,先查看状态;如无返回信息,则是没有启动;
winrm quickconfig#针对winrm service 进行基础配置:
winrm e winrm/config/listener#查看winrm service listener:
winrm set winrm/config/service/auth @{Basic="true"}#为winrm service 配置auth:
winrm set winrm/config/service @{AllowUnencrypted="true"} #为winrm service 配置加密方式为允许非加密:

2,测试使用

在/etc/ansible/hosts下插入你的数据

vi /etc/ansible/hosts#插入你的服务器数据
192.168.1.111 ansible_ssh_user=user ansible_ssh_pass=pass01 ansible_ssh_port=5986 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore#端口有的上面写5985,我的不行,如果你的5986不行,可以试改成5985

或者按照标准

1,在inventory目录的Inventroy.ini中增加服务器信息
[windows]    
172.16.11.176
2。group_vars/windows.yml 中输入认证信息
windows.yml
ansible_ssh_user: Administrator
ansible_ssh_password: 密码                                                                                                                                
ansible_ssh_port: 5986        
ansible_connection: winrm
ansible_winrm_server_cert_validation: ignore

3,执行你的测试吧

ansible windows -m win_ping
或者建立 一个ping.yml,
ansible-playbook ping.yml
---

- name: test
  hosts: windows
  
  tasks:
    - name: Example from an Ansible Playbook
      win_ping: 
    


你可能感兴趣的:(Ansible windows使用)