使用aws ssm运行时,sed命令失败。 ssm重新格式化sed命令并破坏语法

时间:2019-07-22 17:35:16

标签: amazon-web-services amazon-ec2 sed amazon-cloudformation ssm

当我使用cloudformation创建ecs服务时,我试图在ec2实例上的httpd配置文件中添加几行。这个想法是,在cloudformation模板中,我正在启动一个临时ec2实例,然后在模板的userdata部分中,我将ssm send-command传递给目标实例,并且此ssm命令将在上执行sed命令目标实例的apache配置文件。

我编写了一个sed命令,该命令在apache配置文件上运行时可以在我的osx机器上正常运行,但是当我在测试实例上通过ssm运行sed命令时,ssm重新格式化该命令,摆脱了必要的换行符并且命令失败。我将在执行时发布原始的sed命令(没有我要插入的实际文本),ssm命令以及ssm命令的输出。希望有人能帮忙!

原始的sed命令(请注意'a \'之后的必要换行符):

sed '/LocationMatch>/a\
...
The text I am trying to insert. 
...
' community.conf > newcommunity.conf

我要执行的ssm命令:

aws ssm send-command \
    --document-name "AWS-RunShellScript"\
    --parameters commands=['cd /etc/httpd/conf.d',"'sed \'/LocationMatch>/ a\ 
...
The text I am trying to insert.
...
\' community.conf > newcommunity.conf'"] --targets "Key=instanceids,Values=i-09975a463d07eeabd" --region us-west-2

执行命令后的ssm json响应(请注意命令的重新格式化,我尝试复制和粘贴ssm命令的新格式,由于在a \之后没有换行,因此无法在本地计算机上执行) :

{
    "Command": {
        "Comment": "", 
        "Status": "Pending", 
        "MaxErrors": "0", 
        "Parameters": {
            "commands": [
                "cd /etc/httpd/conf.d", 
                "sed '/LocationMatch>/ a\\ \n...\nThe text I am trying to insert.\n...' community.conf > newcommunity.conf" 
            ]
        }, 
        "ExpiresAfter": 1563821978.17, 
        "ServiceRole": "", 
        "DocumentName": "AWS-RunShellScript", 
        "TargetCount": 0, 
        "OutputS3BucketName": "", 
        "NotificationConfig": {
            "NotificationArn": "", 
            "NotificationEvents": [], 
            "NotificationType": ""
        }, 
        "CompletedCount": 0, 
        "Targets": [
            {
                "Values": [
                    "i-09975a463d07eeabd"
                ], 
                "Key": "instanceids"
            }
        ], 
        "StatusDetails": "Pending", 
        "ErrorCount": 0, 
        "OutputS3KeyPrefix": "", 
        "RequestedDateTime": 1563814778.17, 
        "CommandId": "11a18055-917f-4a02-a07d-943fada5fe66", 
        "InstanceIds": [], 
        "MaxConcurrency": "50"
    }
}

使用ssm执行后,命令输出错误:

"StandardErrorContent": "sed: -e expression #1, char 25: comments don't accept any addresses\nfailed to run commands: exit status 1", 

0 个答案:

没有答案