1. 程式人生 > >Ansible管理windows

Ansible管理windows

ansible windows系統 批量管理 自動化運維

Ansible管理windows 安裝步驟

一、系統要求
1,管理機必須是Linux系統且需預安裝Python Winrm 模塊(本次使用Linux版本是CentOS Linux release 7.3.1611 (Core))。
2,底層通信認證一些基於Kerberos ,Windows使用的連接工具為PowerShell而非SSH,我這裏測試的Windows版本是win7-32bit專業版。
3,遠程主機PowerShell版本為3.0+,Management Framework版本為3.0+。
4,需要安裝自動設置Windows遠端管理,英文全稱WS-Management(WinRM)

二、在服務器端安裝ansible和相關的模塊
安裝前準備的工作:
1,[root@Centos7_3 ~]# yum -y install python-pip python-devel
解決沒有安裝python-pip故障
首先下載並安裝setuptools
[root@Centos7_3 ~]# wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py
[root@Centos7_3 ~]# sudo python ez_setup.py --insecure
[root@Centos7_3 ~]# wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
[root@Centos7_3 ~]#tar -xf pip-9.0.1.tar.gz
[root@Centos7_3 ~]#cd pip-8.0.0
[root@Centos7_3 ~]#python setup.py install
[root@Centos7_3 ~]#yum install gcc glibc-devel zlib-devel
[root@Centos7_3 ~]#yum install rpm-build openssl-devel -y
2,正式安裝ansible
[root@Centos7_3 ~]#yum -y install ansible
[root@Centos7_3 ~]# ansible --version
三、在windows端先安裝.NET3.0及以上版本。
技術分享圖片
四、檢查你的powershell 版本是不是3.0不是請升級到3.0版本。
技術分享圖片
如果不是可以用腳本進行升級;
https://github.com/cchurch/ansible/blob/devel/examples/scripts/upgrade_to_ps3.ps1
具體做法是,打開上述鏈接,下載保存在TXT文本後改文件名為upgrade_to_ps3.ps1技術分享圖片
然後使用powershell來運行腳本來升級,升級後重新啟動windows系統。

五、設置你的powershell可以遠程執行,在註冊表改屬性值。具體目錄如下。
1,修改註冊表信息:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\ScriptedDiagnostics
技術分享圖片
2,自動設置Windows遠端管理(WS-Management,WinRM)下載補丁腳本:
https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 ;具體做法是,打開上述鏈接,下載保存在TXT文本後改文件名為ConfigureRemotingForAnsible.ps1具體操作步驟和上面的類似;
3,往往在上一步操作過程中有很多問題,你還需要在Poweshell下面配置winrm:

winrm set winrm/config/service ‘@{AllowUnencrypted="true"}‘
winrm set winrm/config/service/auth ‘@{Basic="true"}‘
技術分享圖片
六、在Linux配置host文件
1,[root@Centos7_3 ~]#vim /etc/ansible/hosts
在最後一行添加一個windows模塊主機組
[windows]
192.168.0.36 ansible_ssh_user="Root" ansible_ssh_pass="123456" ansible_ssh_port=5985 ansible_connection="winrm"
2,在執行命令測試網絡連通性
[root@Centos7_3 ansible]# ansible windows -m win_ping
技術分享圖片
3,測試上傳一個WinRAR文件到windows客戶端
[root@Centos7_3 ansible]# ansible windows -m win_copy -a ‘src=/data/winrar_x64_5.40.0.0.exe dest=D:\ISO\’
技術分享圖片
七、我在測試的時候遇到2個小問題就一個是配置winrm時候需要時專用網絡,如果你的計算機不是專用網絡請改為一下域網路和家庭網絡只要不是公共網絡就可以哦。
技術分享圖片
還有就是使用ansible windows -m win_ping 連接客戶端提示連接被拒絕,這裏我是改變了端口來解決的,在編輯vim /etc/ansible/hosts文件下的端口,原來是5986把它改成5985即可使用。
技術分享圖片

Ansible管理windows