Ansible 管理windwos服务器(一)
不廢話了,開始吧
Ansible管理Windwos需要在windows上執行Powershell腳本,并且Powershell腳本還有版本要求有必要條件:
必要條件:
(1) 必須開啟以及配置Powershell
https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1
(2)Powershell版本必須3.0 or higer
如果您的系統是window7或者Windows Server 2008,那么PowerShell 2.0已經內置了,可以升級為3.0,4.0
如果您的系統是Windows 8 或者Windows server 2012,那么PowerShell 3.0已經內置了,可以升級為4.0
如果您的系統為Windows 8.1或者Windows server 2012 R2,那默認已經是4.0了。
查看Powershell版本,powershell 界面下輸入 $psversiontabl
- Ansible控制機配置
在Ansible控制機上執行下面指令安裝pywinrm模塊 pip install "pywinrm>=0.1.1" 我這里已經安裝過,版本是0.2.2
[圖片上傳失敗...(image-93b72f-1534605118005)]
- 被管windows主機配置
開啟以及配置Powershell
Ansible 官方提供初始化腳本
https://github.com/ansible/ansible/blob/devel/examples/scripts/ConfigureRemotingForAnsible.ps1
腳本主要完成如下操作:
注意:如果提示系統中禁止執行腳本,可以在Powershell 命令行界面輸入 set-ExecutionPolicy RemoteSigned 然后輸入Y,在執行腳本就不會報。
在計算機上運行winrm服務
powershell 3.0中執行
winrm qc
開啟后,需要查看配置是否已經開啟
3.3 主要是下圖Auth中Basic設置為true,service中AllowUnencrypted設置為true。
> winrm set winrm/config/service '@{AllowUnencrypted="true"}' > winrm set winrm/config/service/auth '@{Basic="true"}' wKioL1gFldTxKpQEAABQOm0lSnU531.png-wh_50配置ansible控制機
配置方法有兩種:
第一種:
在/etc/ansible/hosts中
[windows] 192.168.1.11 [windows:vars] ansible_ssh_user="Administrator" ansible_ssh_pass="123456" ansible_ssh_port=5986 ansible_connection="winrm" $ ansible windows -m win_ping 192.168.1.11 | SUCCESS => {"changed": false, "ping": "pong" }第二種
在/etc/ansible/hosts中
[windows] 192.168.1.11 ansible_ssh_user="Administrator" ansible_ssh_pass="123456" ansible_ssh_port=5986 ansible_connection="winrm"要注意的是 端口方面ssl即https方式的使用5986,http使用5985。
區別于控制Linux主機,win主機的命令,需要加上win_,具體支持情況請見官網
http://docs.ansible.com/ansible/list_of_windows_modules.html
****簡單測試****
執行cmd命令
重啟的第一種方式
$ ansible windows -m win_shell -a "shutdown -r -t 1"
第二種方式,不加參數等同于第一種方式
$ ansible windows -m win_reboot
未完待續!!!!
總結
以上是生活随笔為你收集整理的Ansible 管理windwos服务器(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据加密和OpenSSL
- 下一篇: 知乎专栏应用客户端源码项目