使用安装在 WSL 上的 Ansible 设置 Windows 10 工作站

vec*_*cin 5 windows ansible ansible-2.x

我已经在 Windows 10 工作站的 WSL(Linux 的 Windows 子系统)中安装了 Ansible。

我的目标是同时配置 WSL 和 Windows 10 本身。

我能够针对本地主机运行剧本,它通过 SSH 连接和配置 WSL。

但是,我不确定 Ansible 是否可以针对 Windows 主机运行 playbook,以便能够自行设置 Windows(例如,使用 Chocolatey 安装软件包)

这甚至可能吗?或者 Ansible 只能在安装在不同的 Linux 机器上时设置 Windows 节点?

vec*_*cin 6

Great! I was able to connect to my windows host after following those steps.

However, I had to solve two more issues before I was able to run ansible playbooks against both, WSL and windows host:

1. Define connection for WSL

Windows host uses ansible_connection=winrm, but for WSL needs a different connection, I've set ansible_connection=local.

2. Avoid connection var being overriden

The ansible_connection var is overridden. This is because the var name and the host name is the same. This means that you can either run a playbook for WSL or for Windows host but not against both, as they need different connection.

To fix that you can either set hash-behaviour, or set two different host names for localhost under your WSL, /etc/hosts. I've done the second one:

127.0.0.1   wsl.local
127.0.0.1   windows.local
Run Code Online (Sandbox Code Playgroud)

My /etc/ansible/hosts:

[wsl]
wsl.local 

[wsl:vars]
ansible_connection=local

[windows]
windows.local 
[windows:vars]
ansible_port=5985
ansible_connection=winrm
ansible_winrm_transport=basic
ansible_user=<<ansible_user>>
ansible_password=<<ansible_password>>
Run Code Online (Sandbox Code Playgroud)

现在,我可以运行 ansible_playbook,其中包含针对 Windows 主机和 WSL 运行的任务。此处了解有关配置的更多详细信息。


wor*_*dev 5

是的,这是可能的。

  1. 首先,您必须拥有 WSL(您确实拥有)
  2. 接下来您需要安装 Ansible,但需要额外的软件包才能将其与 WinRM 一起使用
    • 安装点子$ apt install python-pip
    • 安装 pip winrm$ pip install pywinrm
    • 安装 xml 解析器$ pip install xmltodict
  3. 您需要设置 WinRM:
    • 您需要将网络设置为专用,因为默认情况下 WinRM 仅适用于专用或域网络。您可以通过提供参数来跳过这一点,Enable-PSRemoting -SkipNetworkProfileCheck但我不建议这样做。
    • 启用 WinRMEnable-PSRemoting在 Windows 的 PowerShell 中运行它。
    • 启用基本身份验证Set-Item -Path WSMan:\localhost\Service\Auth\Basic -Value $true
    • 启用未加密连接Set-Item -Path WSMan:\localhost\Service\AllowUnencrypted -Value $true
  4. 在 WSL 中,将变量添加到您的 playbook 或 group_vars
ansible_port: 5985
ansible_connection: winrm
ansible_winrm_transport: basic
Run Code Online (Sandbox Code Playgroud)
  1. 运行您的剧本时,提供变量ansible_user=your_win_user并/ansible_password=your_win_user_pass或在之前的变量中对它们进行硬编码。

我使用此设置从 WSL 配置我的计算机。你可以在这里看看。希望这可以帮助。

  • 如果对您有帮助,请采纳答案:) 谢谢 (2认同)