破題法: Windows 不能 裝 Ansible
既然知道條件了就開始動手吧!
pip3 install pywinrm


ansible win -m win_ping

以上就是 Windows 被 Ansible 操控前所需要的設定
前面有提到 ansible 在 2.8 版後可以走 SSH,沒錯,不過流程很複雜的!要跟的…快點上車啊!
首先要在 windows 安裝 OpenSSH

接著要把 ansible 所在機器的 public key 貼到 Windows 的機器上,才能直接透過 ssh key 登入操作
最後調整一下 inventory 如下圖就可以了: 
其中 ansible_connection 要從 winrm 改成 ssh
另外,預設連過去的 shell 是 cmd,如果要調整成 powershell 要再做一些別的處理如下 playbook
---
- name: Change Default Shell
hosts: winvm
tasks:
- name: set the default shell to PowerShell
win_regedit:
path: HKLM:\SOFTWARE\OpenSSH
name: DefaultShell
data: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
type: string
state: present
假設 playbook 的名稱叫 setup-powershell-default.yml,那指令就會是:
ansible-playbook setup-powershell-default.yml
至於 playbook 是什麼,以後再提吧,,會需要切成 powershell 是因為大部份的 module 都是以 Powershell 實作的
下一篇會先介紹一些好建立測試環境的工具,敬請期待。
參考資料
When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.
評論 2
在較新的 Windows 上安裝 OpenSSH 有著更簡便的做法,可參考 https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse
秀一下 chocolatey 的操作而已 XD
Windows 10 and Windows Server 2019 最新版本應該都是內建有安裝了,只要啟用就好,
比較舊的就要手動安裝了,如 David 提供的連結