Windows Chef Workstation引导Ubuntu 22.04节点遇换行符错误求助
Windows下Chef Workstation 0.42 Bootstrap Ubuntu 22.04节点换行符错误解决
问题场景
在Windows系统安装的Chef Workstation 0.42中,执行knife bootstrap命令连接Ubuntu 22.04节点时,因Windows与Unix换行符格式差异(CRLF vs LF),导致bootstrap脚本无法正常执行,出现$'\r': command not found等语法错误。
执行命令
knife bootstrap u-wks01.antmind.org -N u-wks01 -ssh-user test --connection-password 'XXXXXXX' -U test --use-sudo-password -V -V
错误信息
DEBUG: [SSH] test@u-wks01.antmind.org<{:user_known_hosts_file=>"/dev/null", :port=>22, :compression=>false, :compression_level=>0, :keepalive=>true, :keepalive_interval=>60, :timeout=>60, :auth_methods=>["none", "password", "keyboard-interactive"], :keys_only=>nil, :keys=>[], :password=>"<hidden>", :forward_agent=>nil, :non_interactive=>true, :verify_host_key=>:accept_new}> (sh /tmp/chef_GXlNnZ/bootstrap.sh) [u-wks01.antmind.org] bash: line 1: $'\r': command not found [u-wks01.antmind.org] bash: line 2: $'\r': command not found [u-wks01.antmind.org] bash: line 3: $'\r': command not found [u-wks01.antmind.org] bash: line 4: $'\r': command not found [u-wks01.antmind.org] bash: -c: line 15: syntax error near unexpected token `$'{\r'' 'ash: -c: line 15: `exists() { [u-wks01.antmind.org] ERROR: The following error occurred on u-wks01.antmind.org: ERROR: DEBUG: [SSH] test@u-wks01.antmind.org<{:user_known_hosts_file=>"/dev/null", :port=>22, :compression=>false, :compression_level=>0, :keepalive=>true, :keepalive_interval=>60, :timeout=>60, :auth_methods=>["none", "password", "keyboard-interactive"], :keys_only=>nil, :keys=>[], :password=>"<hidden>", :forward_agent=>nil, :non_interactive=>true, :verify_host_key=>:accept_new}> (rm -f "/tmp/chef_GXlNnZ/bootstrap.sh")
已尝试操作
- 将Ubuntu节点test用户默认shell从dash改为bash
- 用VSCode将chef-full.erb文件换行符从CRLF改为LF
- 复制模板并将初始行从
sh -c ''改为bash -c '' - 使用dos2unix转换.erb文件
解决方案
1. 确认并修改正确的Chef模板文件路径
Windows上Chef的bootstrap模板默认路径为:
C:\opscode\chef-workstation\embedded\lib\ruby\gems\[ruby版本]\gems\chef-[chef版本]\lib\chef\bootstrap\templates\bootstrap\chef-full.erb
例如实际路径可能为:
C:\opscode\chef-workstation\embedded\lib\ruby\gems\3.0.0\gems\chef-17.10.0\lib\chef\bootstrap\templates\bootstrap\chef-full.erb
确保修改的是该路径下的模板文件,而非本地副本。修改后用VSCode设置换行符为LF并保存。
2. 强制远程端清除换行符再执行脚本
在bootstrap命令中添加--bootstrap-command参数,让远程节点先清除脚本中的\r字符再执行:
knife bootstrap u-wks01.antmind.org -N u-wks01 -ssh-user test --connection-password 'XXXXXXX' -U test --use-sudo-password -V -V --bootstrap-command "bash -c 'tr -d \"\r\" < /tmp/chef_*/bootstrap.sh | bash'"
该命令会读取临时bootstrap脚本,用tr命令删除所有\r字符后,再交给bash执行。
3. 使用自定义LF格式的bootstrap脚本
创建本地bootstrap脚本文件(确保保存为LF换行符),基于官方chef-full模板修改后,用--bootstrap-script参数指定:
knife bootstrap u-wks01.antmind.org -N u-wks01 -ssh-user test --connection-password 'XXXXXXX' -U test --use-sudo-password -V -V --bootstrap-script ./my-bootstrap.sh
4. 修改Chef配置强制LF输出
在knife.rb配置文件中添加以下内容,强制Chef渲染模板时使用LF换行符:
Chef::Config[:bootstrap_template] = "chef-full" # 覆盖模板渲染的换行符处理逻辑 Erubis::Engine.class_eval do def add_text(src) src.gsub!(/\r\n?/, "\n") super end end
保存配置后重新执行bootstrap命令。
内容的提问来源于stack exchange,提问作者apnow
相关产品推荐
相关产品推荐

