如何仅通过命令搭建Windows FTP服务器及自动化配置Win10虚拟机FTP站点?
关于Windows系统通过命令行/Ansible自动化搭建FTP站点的方案
1. 能否仅通过命令在Windows系统上搭建FTP服务器?
当然可以!Windows自带的命令行工具(比如PowerShell命令、appcmd.exe)完全能完成从功能启用到位站点配置的全流程,全程不需要依赖GUI操作。
2. Ansible自动化配置Windows 10 FTP站点的完整方案
你提到的功能启用部分已经覆盖了Server和Win10两种场景,接下来重点解决FTP站点的自动化配置——这部分可以通过Ansible的win_shell模块调用PowerShell或appcmd命令实现,以下是具体步骤和示例:
步骤1:确认FTP相关功能已启用(优化你的现有代码)
针对Windows 10,推荐用win_shell调用PowerShell命令的方案,因为win_feature模块主要适配Server版本,同时补充依赖的IIS基础服务:
- name: Enable FTP server features on Windows 10 win_shell: | Enable-WindowsOptionalFeature -Online -FeatureName "IIS-FTPServer","IIS-FTPSvc","IIS-WebServer","IIS-WebServerManagementTools" -All
注:额外启用
IIS-WebServer是因为FTP站点依赖IIS的基础服务,管理工具则方便后续排查(可选)
步骤2:创建FTP站点目录并配置权限
先通过Ansible的win_file和win_acl模块创建站点根目录,确保IIS进程有读写权限:
- name: Create FTP site root directory win_file: path: C:\inetpub\ftproot state: directory - name: Set permissions for FTP root directory win_acl: path: C:\inetpub\ftproot user: IIS_IUSRS rights: Read,Write type: allow state: present
步骤3:通过appcmd创建并配置FTP站点
appcmd.exe是IIS的命令行管理工具,能直接操作FTP站点,用Ansible的win_shell调用即可:
- name: Create FTP site via appcmd win_shell: | appcmd add site /name:"MyAutomatedFTP" /physicalPath:C:\inetpub\ftproot /bindings:"ftp://*:21" - name: Configure FTP site authentication & permissions win_shell: | # 启用匿名访问(按需选择) appcmd set config "MyAutomatedFTP" /section:system.ftpServer/security/authentication/anonymousAuthentication /enabled:true # 启用基本身份验证(可选,适合需要账号密码的场景) appcmd set config "MyAutomatedFTP" /section:system.ftpServer/security/authentication/basicAuthentication /enabled:true # 设置所有用户的读写权限 appcmd set config "MyAutomatedFTP" /section:system.ftpServer/security/authorization /+"[accessType='Allow',users='*',permissions='Read,Write']"
步骤4:启动FTP站点并设置自动启动
最后确保站点运行,并且开机自启:
- name: Start FTP site win_shell: appcmd start site "MyAutomatedFTP" - name: Set FTP site to start automatically win_shell: appcmd set site "MyAutomatedFTP" /serverAutoStart:true
额外补充:被动模式FTP配置(按需添加)
如果需要支持被动模式FTP,还需配置端口范围并添加防火墙规则:
- name: Configure FTP passive port range win_shell: appcmd set config -section:system.ftpServer/firewallSupport /lowDataChannelPort:50000 /highDataChannelPort:50100 /commit:apphost - name: Allow FTP passive ports in Windows Firewall win_firewall_rule: name: FTP Server (Passive Ports) localport: 50000-50100 action: allow direction: in protocol: tcp state: present enabled: yes
所有命令都可以先单独在PowerShell中测试,确认没问题后再集成到Ansible playbook里。
内容的提问来源于stack exchange,提问作者Maspital




