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