Ansible批量遠程管理Windows主機(部署與配置)

一、測試環境介紹

Ansible管理主機:

系統:   CentOS6.8

IP Addr: 172.16.10.22

 

Linux管理服務器需安裝pip、pywinrm插件

 

Windows客戶端主機:

        系統:   Server2008R2 SP1

        IP Addr: 172.16.10.23

 

Windows機器需要安裝或升級powershell4.0以上版本,Server2008R2默認的版本是2.0,因此必須升級至4.0版本。對於Server2012及以上的版本默認是4.0版本,不需要升級。

(經測試安裝powershell3.0版本並不能正常支持Ansible,因此選擇升級至4.0版本)

 

注意:升級powershell需要重新服務器才能生效。


二、Windows Server 2008R2客戶端升級至powershell4.0

配置winrm之前檢查系統版本,以及powershell版本,如果是Server2008R2版本,則需要升級powershell至4.0版本。Server2012R2以上的版本不需要升級powershell

 

升級powershell4.0步驟:

1. 檢查powershell版本

未升級前顯示的是2.0版本

圖片.png


2. 下載並安裝Microsoft .NET Framework 4.5

下載地址:

https://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe

 

3. 下載並安裝powershell4.0(Windows Management Framework 4.0 )

下載地址:

https://download.microsoft.com/download/3/D/6/3D61D262-8549-4769-A660-230B67E15B25/Windows6.1-KB2819745-x64-MultiPkg.msu

 

注意: 先安裝.NET Framework 4.5 ,然後安裝powershell4.0,安裝完成之後重啓windows服務器


4. 升級完powershell4.0後檢查

查看powershell版本

 

打開運行--->輸入powershell啓動powershell

圖片.png

在powershell終端上執行get-host命令可以查看powershell版本

圖片.png

三、Windows客戶端配置winrm,啓用powershell遠程管理

打開powershell終端,按以下步驟執行命令(正常情況不會報錯,如果有報錯,請檢查輸入的命令是否正確,或者手動輸入命令進行配置)

 

1. 查看powershell執行策略

get-executionpolicy

2. 更改powershell執行策略爲remotesigned

set-executionpolicy remotesigned


圖片.png

3. 配置winrm service並啓動服務

winrm quickconfig

4. 查看winrm service啓動監聽狀態

winrm enumerate winrm/config/listener

5. 修改winrm配置,啓用遠程連接認證

winrm set winrm/config/service/auth '@{Basic="true"}'

winrm set winrm/config/service '@{AllowUnencrypted="true"}'


四、Windows客戶端防火牆配置

通過命令winrm enumerate winrm/config/listener檢查winrm服務正確啓動之後

 

添加防火牆信任規則,允許5985端口通過


打開防火牆高級配置,選擇入站規則,在點擊新建規則

圖片.png

填寫信任端口5985

圖片.png

填寫新建規則名稱

圖片.png

五、Ansible服務端配置和測試管理Windows服務器(服務端操作)

1. 添加windows客戶端連接信息

編輯/etc/ansible/hosts,添加客戶端主機信息(ansible服務端的配置)

 

[windows]

172.16.10.23 ansible_ssh_user="Administrator" ansible_ssh_pass="zteict123" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore

2. 測試ping探測windows客戶主機是否存活

執行命令

# ansible 172.16.10.23 -m win_ping

圖片.png

3. 測試文件管理

測試在windows主機執行遠程創建目錄

# ansible 172.16.10.23 -m win_file -a 'dest=c:\config_dir state=directory'

圖片.png

測試將ansible主機上的/etc/hosts文件同步到windows主機的指定目錄下

# ansible 172.16.10.23 -m win_copy -a 'src=/etc/hosts dest=c:\config_dir\hosts.txt'

圖片.png

圖片.png


刪除文件

# ansible 172.16.10.23 -m win_file -a 'dest=c:\config_dir\hosts.txt state=absent'

 

刪除目錄

# ansible 172.16.10.23 -m win_file -a 'dest=c:\config_dir2 state=absent'

3. 測試遠程執行cmd命令

# ansible 172.16.10.23 -m win_shell -a 'ipconfig'

圖片.png

4. 遠程重啓windows服務器

# ansible 172.16.10.23 -m win_reboot

# ansible 172.16.10.23 -m win_shell -a 'shutdown -r -t 0'

5. 測試創建用戶(遠程在windows客戶端上創建用戶)

# ansible 172.16.10.23 -m win_user -a "name=testuser1 passwd=123456"

圖片.png

6. Windows服務管理

Ansible命令格式:

ansible [遠程主機IP地址] -m win_shell -a “net stop|start 服務名”

 

示例:啓動與停止Windows服務器的Spooler服務

圖片.png


此外,Ansible還可以遠程管理Windows的IIS服務、Apache等Web服務,利用Ansible實現批量更新Web應用,如代碼發佈、版本更新等。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章