【问题标题】:user data scripts fails without giving reason用户数据脚本在没有给出原因的情况下失败
【发布时间】:2022-01-27 05:25:45
【问题描述】:

我正在使用 Web 控制台启动 Amazon Linux 实例 (ami-fb8e9292),将数据粘贴到用户数据框中以在启动时运行脚本。如果我使用 example given by amazon 启动 Web 服务器,它就可以工作。但是当我运行自己的脚本(也是#!/bin/bash 脚本)时,它不会运行。

如果我查看var/log/cloud-init.log,它没有提供有关该主题的有用信息:

May 22 21:06:12 cloud-init[1286]: util.py[DEBUG]: Running command ['/var/lib/cloud/instance/scripts/part-001'] with allowed return codes [0] (shell=True, capture=False)
May 22 21:06:16 cloud-init[1286]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [2]
May 22 21:06:16 cloud-init[1286]: util.py[DEBUG]: Failed running /var/lib/cloud/instance/scripts/part-001 [2]
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/cloudinit/util.py", line 637, in runparts
    subp([exe_path], capture=False, shell=True)
  File "/usr/lib/python2.6/site-packages/cloudinit/util.py", line 1528, in subp
    cmd=args)
ProcessExecutionError: Unexpected error while running command.
Command: ['/var/lib/cloud/instance/scripts/part-001']
Exit code: 2
Reason: -
Stdout: ''
Stderr: ''

如果我 ssh 进入实例和 sudo su 并直接执行 shell 脚本:

/var/lib/cloud/instance/scripts/part-001

然后它运行良好。此外,如果我模拟 cloud-init 运行它的方式,它也可以工作:

python
>>> import cloudinit.util
>>> cloudinit.util.runparts("/var/lib/cloud/instance/scripts/")

使用其中任何一种方法,如果我故意在脚本中引入错误,则会产生错误消息。如何调试有用的调试输出的选择性缺失?

【问题讨论】:

  • 我也有这个问题。我得到一个退出代码 1。
  • 我也有同样的问题。你解决了吗?
  • 不。我刚刚停止使用 Amazon Linux。我认为他们自己的定制将是支持最好的 AMI,但它似乎是支持最差的一个。

标签: amazon-web-services amazon-ec2


【解决方案1】:

我不确定这是否适用于每个人,但我遇到了这个问题并且能够通过更改我的第一行来解决它:

#!/bin/bash -e -v

仅此:

#!/bin/bash

当然,现在我的脚本失败了,我不知道它走了多远,但至少我通过了它而不是运行它。 :)

【讨论】:

  • 您可以使用以下命令重新添加:set -v -e #verbose 并在出现任何错误时退出,或者:set -x #debug
  • 我在#!/bin/bash -xe遇到了同样的问题
【解决方案2】:

考虑在/var/log/cloud-init-output.log 中搜索“Failed”、“ERROR”、“WARNING”或“/var/lib/cloud/instance/scripts/”等关键字,而不是/var/log/cloud-init.log - 在大多数情况下,这些关键字包含非常清晰的内容错误消息。

例如 - 运行错误的命令将在 /var/log/cloud-init-output.log 中产生以下错误:

/var/lib/cloud/instance/scripts/part-001: line 10: vncpasswd: command not found
cp: cannot stat '/lib/systemd/system/vncserver@.service': No such file or directory
sed: can't read /etc/systemd/system/vncserver@.service: No such file or directory
Failed to execute operation: No such file or directory
Failed to start vncserver@:1.service: Unit not found.
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Cleaning repos: amzn2-core amzn2extra-docker amzn2extra-epel

/var/log/cloud-init.log 结束时,您将收到一条安静的常规错误消息:

Aug 31 15:14:00 cloud-init[3532]: util.py[DEBUG]: Failed running /var/lib/cloud/instance/scripts/part-001 [1]
    Traceback (most recent call last):
      File "/usr/lib/python2.7/site-packages/cloudinit/util.py", line 910, in runparts
        subp(prefix + [exe_path], capture=False, shell=True)
      File "/usr/lib/python2.7/site-packages/cloudinit/util.py", line 2105, in subp
        cmd=args)
    ProcessExecutionError: Unexpected error while running command.
    Command: ['/var/lib/cloud/instance/scripts/part-001']
    Exit code: 1
    Reason: -
    Stdout: -
    Stderr: -
    cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)

(*) 尝试grep 只是相关的错误消息:

grep -C 10 '<search-keyword>' cloud-init-output.log

【讨论】:

    【解决方案3】:

    我遇到了类似的问题,我能够解决它。我意识到不会为 sudo 设置环境变量 EC2_HOME。我在使用 aws cli 的配置集中做了很多事情,为了使这些工作正常工作,需要设置 EC2_HOME。因此,我进入并删除了我的配置集和 UserData 中的任何地方的 sudo。 早些时候,当我遇到这个问题时,我的 UserData 看起来像:

    "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
                                    "#!/bin/bash\n",
                                    "sudo yum update -y aws-cfn-bootstrap\n",
    
                                    "# Install the files and packages and run the commands from the metadata\n",
                                    "sudo /opt/aws/bin/cfn-init -v --access-key ", { "Ref" : "IAMUserAccessKey" }, " --secret-key ", { "Ref" : "SecretAccessKey" },  
                                    "         --stack ", { "Ref" : "AWS::StackName" },
                                    "         --resource NAT2 ",
                                    "         --configsets config ",
                                    "         --region ", { "Ref" : "AWS::Region" }, "\n"
                            ]]}}
    

    更改后的我的 UserData 如下所示:

    "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
                                    "#!/bin/bash -xe\n",
                                    "yum update -y aws-cfn-bootstrap\n",
    
                                    "# Install the files and packages and run the commands from the metadata\n",
                                    "/opt/aws/bin/cfn-init -v --access-key ", { "Ref" : "IAMUserAccessKey" }, " --secret-key ", { "Ref" : "SecretAccessKey" },  
                                    "         --stack ", { "Ref" : "AWS::StackName" },
                                    "         --resource NAT2 ",
                                    "         --configsets config ",
                                    "         --region ", { "Ref" : "AWS::Region" }, "\n"
                            ]]}}
    

    同样,我删除了我在配置集中执行的所有 sudo 调用

    【讨论】:

      【解决方案4】:

      希望它能减少某人的调试时间。 我的/var/log/cloud-init-output.log 中没有任何明确的错误消息,只是这样:

      2021-04-07 10:36:57,748 - cc_scripts_user.py[警告]:无法运行模块脚本用户(/var/lib/cloud/instance/scripts 中的脚本) 2021-04-07 10:36:57,748 - util.py [警告]:运行模块脚本用户() 失败

      经过一番调查,我意识到原因是 shebang 字符串中的拼写错误:#!?bin/bash 而不是 #!/bin/bash

      【讨论】:

        【解决方案5】:

        在我的情况下,cloudinit 无法启动脚本,因为 userdata 必须以

        #!bin/bash
        

        前面没有空格! 不错的 AWS 错误,有很多时间进行故障排除:)

        【讨论】:

        • 这是已经回答过的相同答案。考虑改为对原始答案发表评论。
        猜你喜欢
        • 1970-01-01
        • 2020-09-16
        • 1970-01-01
        • 1970-01-01
        • 2014-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多