在 Windows 11 上安装 Ansible 的最佳方法是通过 Windows Subsystem for Linux(WSL),特别是使用 Ubuntu 20.04 LTS。这种方法可以充分利用 WSL 提供的 Linux 环境,同时避免直接在 Windows 上运行 Ansible 的兼容性问题。
安装步骤
1. 安装 Windows Subsystem for Linux (WSL)
-
打开 PowerShell 并安装 WSL 打开 PowerShell 并运行以下命令:
powershell wsl --install这将自动安装以下组件:
- Virtual Machine Platform
- Windows Subsystem for Linux
- WSL Kernel
- GUI App Support
- 默认的 Ubuntu 发行版
安装完成后,系统将提示重启。
-
重启计算机 安装完成后,重启计算机以完成 WSL 的安装。
2. 设置 Ubuntu 20.04 LTS
-
启动 Ubuntu 安装完成后,通过“开始”菜单启动 Ubuntu 20.04 LTS。首次启动时,系统会提示您创建一个默认的 UNIX 用户账户和密码。
plaintext Please create a default UNIX user account. The username does not need to match your Windows username. Enter new UNIX username: demo New password: Retype new password: -
更新系统包 登录后,运行以下命令更新系统包:
bash sudo apt-get update sudo apt-get upgrade -y
3. 安装 Ansible
-
安装 Ansible 使用
apt安装 Ansible:bash sudo apt-get install ansible -y -
验证安装 安装完成后,运行以下命令验证 Ansible 是否正确安装:
bash ansible --version输出示例:
plaintext ansible 2.9.6 config file = /etc/ansible/ansible.cfg configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3/dist-packages/ansible executable location = /usr/bin/ansible python version = 3.8.2 (default, Mar 13 2020, 10:14:16) [GCC 9.3.0]
实际演示
1. 安装 WSL
PS C:\Users\user> wsl --install
Installing: Virtual Machine Platform
Virtual Machine Platform has been installed.
Installing: Windows Subsystem for Linux
Windows Subsystem for Linux has been installed.
Downloading: WSL Kernel
Installing: WSL Kernel
WSL Kernel has been installed.
Downloading: GUI App Support
Installing: GUI App Support
GUI App Support has been installed.
Downloading: Ubuntu
The requested operation is successful. Changes will not be effective until the system is rebooted.
2. 设置 Ubuntu 20.04 LTS
Welcome to Ubuntu 20.04 LTS (GNU/Linux 4.4.0-22000-Microsoft x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Mon Jan 10 16:57:36 UTC 2022
System load: 0.52 Users logged in: 0
Usage of /home: unknown IPv4 address for eth0: 10.0.2.15
Memory usage: 73% IPv4 address for eth1: 169.254.235.130
Swap usage: 1% IPv4 address for eth1: 192.168.0.209
Processes: 7
0 updates can be installed immediately.
0 of these updates are security updates.
This message is shown once a day. To disable it please create the
/home/demo/.hushlogin file.
demo@demo:~$ sudo su
[sudo] password for demo:
root@demo:/home/demo# apt-get update
3. 安装 Ansible
root@demo:/home/demo# apt-get install ansible -y
4. 验证安装
root@demo:/home/demo# ansible --version
ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.2 (default, Mar 13 2020, 10:14:16) [GCC 9.3.0]
总结
通过 WSL 在 Windows 11 上安装 Ansible 是一种高效且兼容性良好的方法。您可以通过以下步骤快速完成安装:
- 安装 WSL:使用
wsl --install命令安装 WSL 和默认的 Ubuntu 发行版。 - 设置 Ubuntu:创建默认的 UNIX 用户账户并更新系统包。
- 安装 Ansible:使用
apt安装 Ansible 并验证安装。
通过这些步骤,您可以在 Windows 11 上快速设置一个功能完整的 Ansible 环境。